#darrenwathen_63201
1 messages · Page 1 of 1 (latest)
hello! can you share your code snippet and the exact error message you're getting?
Its the standard sample code from stripe. The initialise in checkout-js is:
// This is your test publishable API key.
const stripe = Stripe("pk_test_...");
// The items the customer wants to buy
const items = [{ id: "xl-tshirt" }];
let elements;
initialize();
checkStatus();
Error in my dev console is:
Unhandled Promise Rejection: SyntaxError: The string did not match the expected pattern.
Its from when it is handling the json response:
let emailAddress = '';
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("/hovr2/create.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
}).then((r) => r.json());
you mean at r.json()?
Yep.
check what's your server responding with
yep
Shall do.
create is sending an error:
Notice: Undefined property: Stripe\Service\CoreServiceFactory::$paymentIntent in /Users/darren/vendor/stripe/stripe-php/lib/Service/AbstractServiceFactory.php on line 65
{"error":"Call to a member function create() on null"
This looks like your server is not creating payment intent. Can you check your create.php where and why the error is thrown?
Just using the sample php script from the dev section. Nothing fancy. php is:
require_once '/usr/local/var/www/vendor/autoload.php';
require_once './inc-files/secrets.php';
$stripe = new \Stripe\StripeClient($stripeSecretKey);
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
// Create a PaymentIntent with amount and currency
$paymentIntent = $stripe->paymentIntent->create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'aud',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
Which line is throwing the error and what the error message is?
I understand, but which line in your code trigger the library file to throw the error? Is it $paymentIntent = $stripe->paymentIntent->create()?
Sorry. This call:$paymentIntent = $stripe->paymentIntent->create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'aud',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
Can you check if $stripe = new \Stripe\StripeClient($stripeSecretKey); is null? Looks like $stripe is null
it's not null.
Stripe\StripeClient Object
(
[config:Stripe\BaseStripeClient:private] => Array
(
[api_key] => sk_test_...
[client_id] =>
[stripe_account] =>
[stripe_version] =>
[api_base] => https://api.stripe.com
[connect_base] => https://connect.stripe.com
[files_base] => https://files.stripe.com
)
[defaultOpts:Stripe\BaseStripeClient:private] => Stripe\Util\RequestOptions Object
(
[apiKey] =>
[headers] => Array
(
[Stripe-Account] =>
[Stripe-Version] =>
)
[apiBase] =>
)
[coreServiceFactory:Stripe\StripeClient:private] =>
)
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
This is strange. It seems it got to do with $stripe->paymentIntent that is null according to https://github.com/stripe/stripe-php/blob/master/lib/Service/AbstractServiceFactory.php#L65
If you run from the example code here https://stripe.com/docs/payments/quickstart?lang=php without any modification, does the same issue occur?
same error.
Thanks for sharing!
Can you change to following:
<?php
require_once '../vendor/autoload.php';
require_once '../secrets.php';
\Stripe\Stripe::setApiKey($stripeSecretKey);
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
// Create a PaymentIntent with amount and currency
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'aud',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
that works.
Great to hear that it works now! I'll share the feedback to the relevant team to fix the example code
Thanks for your help.
No problem! Happy to help 😄
Sorry, one other qquestion. How dod I pass in the amount? When I try any function calls it fails with the same string type error.
what do you mean by how do you pass in the amount? you would define the amount for the PaymentIntent to be created :
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => here
Thats where I am passing the value but any variable I put in there fails.
have you logged the variable to find out what value is being passed in?
Its not so much the variable, it at when I reference a session variable or call an exteernal function the script has issues.