#hanis03

1 messages · Page 1 of 1 (latest)

proper reefBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

lilac mantle
#

What's your question?

echo temple
#

In create.php i reciving this json error:

{
error: "calculateOrderAmount(): Argument #1 ($items) must be of type array, null given, called in /data/web/virtuals/314556/virtual/www/subdom/admin/admin/novyshop/platby/create.php on line 26"
}

#

Can you help me?

lilac mantle
#

What do you need help with exactly? The error is telling you the issue: Argument #1 ($items) must be of type array, null given

echo temple
#

Yeah but before when I´m using something like authenticate with Link email, it´s working. But I want using own email form

#

So I don´t know how describe my problem, but if you want, I will send you code

lilac mantle
#

Start here:

error: "calculateOrderAmount(): Argument #1 ($items) must be of type array, null given, called in /data/web/virtuals/314556/virtual/www/subdom/admin/admin/novyshop/platby/create.php on line 26"
}```
#

Go to line 26

#

Figure out why you're passing null to argument 1

#

Use print statements or a debugger to do this

#

We can help with stripe api questions in here, but you really need to debug this code yourself

#

If you ask specific questions, I'm happy to help, but I can't debug your code for you

echo temple
#
try {
    $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);
    
    $paymentIntent = $stripe->paymentIntents->create([
        'amount' => calculateOrderAmount($jsonObj->items),
        'currency' => 'czk',
    'setup_future_usage' => 'off_session',
    'payment_method_types' => ['card'],
    /*'automatic_payment_methods' => [
            'enabled' => true,
        ],*/
    ]);
    $paymentIntent->confirmation_method = 'manual';
    $output = [
        'clientSecret' => $paymentIntent->client_secret,
    ];

    echo json_encode($output);
    $_SESSION['output'] = $output;
} catch (Error $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}

but I before don´t use some $items

#

I don´t have idea how to fix that.

#

😦

lilac mantle
#

You still haven't asked a specific question

#

I don't know what to do with the above code

#

Use print statements or a debugger to debug

echo temple
#

I don´t know how to fix that

lilac mantle
#

We can help integrate the stripe api, but I can't teach you basic debugging skills in here

#

That's something fundamental you need to learn

echo temple
#

I understand that, but yesterday I was texting with your colleague and he wanted to help me solve it together. I don't use $items in any way and I don't know what they have to be there for

lilac mantle
#

What is line 26?

#

I just don't have enough info here

#

And you haven't asked a specific question yet

#

You just asked me to fix it

echo temple
#
        'amount' => calculateOrderAmount($jsonObj->items),

This is on line 26

#

Sorry, I'm using a translator.

lilac mantle
#

Yeah so what the error means then is $jsonObj->items is null when it should be an array.

#

Argument #1 ($items) must be of type array, null given

#

So you need to trace your code back using print statements or a debugger to find out why that is

echo temple
#

However, I don't use $items.

lilac mantle
#

?

#

That's your code

#

$jsonObj->items

#

You reference items clearly there

echo temple
#

Where is in default

lilac mantle
#

But look at checkout.js

#

Items are passed there

#

So in your code, you aren't passing items correctly somewhere

echo temple
#
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
const stripe = Stripe("--own--stripe--code--");

// The items the customer wants to buy
const items = [{ id: "xl-tshirt" }];

let elements;

initialize();
checkStatus();

document
  .querySelector("#payment-form")
  .addEventListener("submit", handleSubmit);

// Fetches a payment intent and captures the client secret
async function initialize() {
  const { clientSecret } = await fetch("create.php", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  }).then((r) => r.json());

  elements = stripe.elements({ clientSecret });


  const paymentElementOptions = {
    layout: "accordion",
  };

  const paymentElement = elements.create("payment", paymentElementOptions);
  paymentElement.mount("#payment-element");
}

async function handleSubmit(e) {
  e.preventDefault();
  setLoading(true);

  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: {
      return_url: "--return--url--",
    },
  });

  if (error.type === "card_error" || error.type === "validation_error") {
    errShowMessage(error.message);
    console.log(error.message);
  } else {
    showMessage("Vyskytla se neočekávaná chyba.");
  }

  setLoading(false);
}


async function checkStatus() {
  const clientSecret = new URLSearchParams(window.location.search).get(
    "payment_intent_client_secret"
  );

  if (!clientSecret) {
    return;
  }

  const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);

  switch (paymentIntent.status) {
    case "succeeded":
      showMessage("Zaplaceno!");
      break;
    case "processing":
      showMessage("Platba je v procesu.");
      break;
    case "requires_payment_method":
      showMessage("Vaše platba nebyla úspěšná, zkuste to prosím znovu.");
      break;
    default:
      showMessage("Vyskytla se chyba.");
      break;
  }
}

function showMessage(messageText) {
  const messageContainer = document.querySelector("#payment-message");

  messageContainer.classList.remove("hidden");
  messageContainer.textContent = messageText;
/*
  setTimeout(function () {
    messageContainer.classList.add("hidden");
    messageContainer.textContent = "";
  }, 5000);*/
}

function errShowMessage(messageText) {
  const messageContainer = document.querySelector("#warnings");
  const containerBox = document.querySelector(".payment-message-err");
  containerBox.classList.remove("hidden");
  messageContainer.textContent = messageText;
}


function setLoading(isLoading) {
  if (isLoading) {
    document.querySelector("#submit").disabled = true;
    document.querySelector("#spinner").classList.remove("hidden");
    document.querySelector("#button-text").classList.add("hidden");
  } else {
    document.querySelector("#submit").disabled = false;
    document.querySelector("#spinner").classList.add("hidden");
    document.querySelector("#button-text").classList.remove("hidden");
  }
}

But I don´t know what I must to do with items here, because before I don´t used and it works

lilac mantle
#

I'm confused

#

What works and what doesn't work

echo temple
#

Previous code, but this not. I used before this code still but i use also "Link", but now I want use without that

lilac mantle
#

So you're saying you haven't changed our sample code at all?

#

And the sample you downloaded is failing without any modifications?

echo temple
lilac mantle
#

What do you mean?

#

Have you changed the sample code in any way?

#

Or you just downloaded it and ran it?

echo temple
#

Download and then change, but I left what was there, only added it

lilac mantle
#

What did you change

#

Specifically

#

I can't help unless I know exactly what you did

echo temple
#

do you want me to send the code?

lilac mantle
#

Yeah. Was it a lot that you changed? If not, just send the part you changed

#

But again, this is something you need to debug yourself

#

These are fundamental debugging skills you need to learn

#

If you ask specific questions I can help

#

But I'm not going to download your code and debug your entire integration myself

echo temple
lilac mantle
#

Ok send me your client side code then so I can take a look

echo temple
#

php like where is html code?

#

or js

#

Sorry. For some reason I can't get to Cloudflare so they can't delete the old data. So I can't find the error.

lilac mantle
#

What error

#

Just sent the .js code

echo temple
#

CODE:

// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
const stripe = Stripe("--stripe--");

// The items the customer wants to buy
const items = [{ id: "xl-tshirt" }];


let elements;

initialize();
checkStatus();

document
  .querySelector("#payment-form")
  .addEventListener("submit", handleSubmit);

// Fetches a payment intent and captures the client secret
async function initialize() {
  const { clientSecret } = await fetch("create.php", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  }).then((r) => r.json());

  elements = stripe.elements({ clientSecret });


  const paymentElementOptions = {
    layout: "accordion",
  };

  const paymentElement = elements.create("payment", paymentElementOptions);
  paymentElement.mount("#payment-element");
}

async function handleSubmit(e) {
  e.preventDefault();
  setLoading(true);

  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: {
      return_url: "--return--",
    },
  });

  if (error.type === "card_error" || error.type === "validation_error") {
    errShowMessage(error.message);
    console.log(error.message);
  } else {
    showMessage("Vyskytla se neočekávaná chyba.");
  }

  setLoading(false);
}


async function checkStatus() {
  const clientSecret = new URLSearchParams(window.location.search).get(
    "payment_intent_client_secret"
  );

  if (!clientSecret) {
    return;
  }

  const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);

  switch (paymentIntent.status) {
    case "succeeded":
      showMessage("Zaplaceno!");
      break;
    case "processing":
      showMessage("Platba je v procesu.");
      break;
    case "requires_payment_method":
      showMessage("Vaše platba nebyla úspěšná, zkuste to prosím znovu.");
      break;
    default:
      showMessage("Vyskytla se chyba.");
      break;
  }
}

function showMessage(messageText) {
  const messageContainer = document.querySelector("#payment-message");

  messageContainer.classList.remove("hidden");
  messageContainer.textContent = messageText;
/*
  setTimeout(function () {
    messageContainer.classList.add("hidden");
    messageContainer.textContent = "";
  }, 5000);*/
}

function errShowMessage(messageText) {
  const messageContainer = document.querySelector("#warnings");
  const containerBox = document.querySelector(".payment-message-err");
  containerBox.classList.remove("hidden");
  messageContainer.textContent = messageText;
}


function setLoading(isLoading) {
  if (isLoading) {
    document.querySelector("#submit").disabled = true;
    document.querySelector("#spinner").classList.remove("hidden");
    document.querySelector("#button-text").classList.add("hidden");
  } else {
    document.querySelector("#submit").disabled = false;
    document.querySelector("#spinner").classList.add("hidden");
    document.querySelector("#button-text").classList.remove("hidden");
  }
}
echo temple
#

Can I disable this like $items?

lilac mantle
#

What do you mean?

#

If you don't use items, just remove all references of it in the code

#

Are you a developer?

echo temple
#

Junior developer

lilac mantle
#

Yeah if you're not using it, just remove it

echo temple
#

I was afraid it wouldn't work without it 😄

#

But I'm not sure what I'm going to send to create.php

#

Nothing?

#

Or then how to call it

lilac mantle
#

It's your integration

#

I don't know your requirements

#

Does the payment intent amount vary based on user selection in the fronte-end? Or do you know ahead of time?

#

This is all up to you...

echo temple
#

Okay I will try it something, otherwise I will contact you