#darrenwathen_63201

1 messages · Page 1 of 1 (latest)

wooden knotBOT
analog thorn
#

hello! can you share your code snippet and the exact error message you're getting?

clear seal
#

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());

analog thorn
#

you mean at r.json()?

clear seal
#

Yep.

analog thorn
#

check what's your server responding with

clear seal
#

You mean from the create.php?

analog thorn
#

yep

clear seal
#

Shall do.

wooden knotBOT
clear seal
#

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"

hollow hedge
#

This looks like your server is not creating payment intent. Can you check your create.php where and why the error is thrown?

clear seal
#

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()]);
}

hollow hedge
#

Which line is throwing the error and what the error message is?

clear seal
#

line 65

#

but not from create.php, its from a library file

hollow hedge
#

I understand, but which line in your code trigger the library file to throw the error? Is it $paymentIntent = $stripe->paymentIntent->create()?

clear seal
#

Sorry. This call:$paymentIntent = $stripe->paymentIntent->create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'aud',
'automatic_payment_methods' => [
'enabled' => true,
],
]);

hollow hedge
#

Can you check if $stripe = new \Stripe\StripeClient($stripeSecretKey); is null? Looks like $stripe is null

clear seal
#

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] => 

)

hollow hedge
clear seal
#

same error.

hollow hedge
#

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()]);
}
clear seal
#

that works.

hollow hedge
#

Great to hear that it works now! I'll share the feedback to the relevant team to fix the example code

clear seal
#

Thanks for your help.

hollow hedge
#

No problem! Happy to help 😄

clear seal
#

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.

analog thorn
#

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
clear seal
#

Thats where I am passing the value but any variable I put in there fails.

analog thorn
#

have you logged the variable to find out what value is being passed in?

clear seal
#

Its not so much the variable, it at when I reference a session variable or call an exteernal function the script has issues.