#Cecile-payment_method

1 messages · Page 1 of 1 (latest)

gilded field
midnight scaffold
#

I used subscription mode before but informations displayed in the checkout form with this mode are not good for my client. The solution of making a simple payment and creating the subscription thourgh the webhook is better for her.
I manage to do correctly the creation subscription with a trial period ok to not charge twice the first period but I just realised that there is no payment method attached either to the subscription or the client so the second payment is not done

gilded field
midnight scaffold
#

It seems to be what I'm looking for but I'm having a curious issue, maybe are you used to it.
With my payment_methode id I got an invalid string error
Invalid string: {:"0"=>"pm_1K8omAH1x8t6cpUIKtsSf31L"}

gilded field
#

Can you share the code you are using to create the subscription?

midnight scaffold
#

Sure

#

$paiement = $stripe->paymentIntents->retrieve($session['payment_intent']);
$payment_methode = $paiement['payment_method'];
echo $payment_methode;
$stripe->customers->update(
$session['customer'],
['invoice_settings'=>['default_payment_method'=>["$payment_methode"]]]
);

#

$stripe_product = \Stripe\Product::create([
'name' => $item_name,
'description' => $product_info['item_slogan']
]);

#

$stripe_price = \Stripe\Price::create([
'nickname' => $stripe_product->name,
'product' => $stripe_product->id,
'unit_amount' => $new_price,
'currency' => 'EUR',
'recurring' => [
'interval' => $interval,
'usage_type' => 'licensed',
],
]);
$subscription = \Stripe\Subscription::create([
'customer' => $session['customer'],
'default_payment_method' => $payment_methode,
'items' => [
['price' => $stripe_price],
],
'proration_behavior' => 'none',
'trial_period_days' => $trial,
]);

#

Here is the response

#

</pre>pm_1K8omAH1x8t6cpUIKtsSf31L<br />
<b>Fatal error</b>: Uncaught (Status 400) (Request req_97pbn0Q4r2uHqw) Invalid string: {:"0"=>"pm_1K8omAH1x8t6cpUIKtsSf31L"}

#

I'm having the same issue even with the id wirtten manually here :

#

$stripe->customers->update(
$session['customer'],
['invoice_settings'=>['default_payment_method'=>['pm_1K8omAH1x8t6cpUIKtsSf31L']]]
);

#

The response is still :
<b>Fatal error</b>: Uncaught (Status 400) (Request req_FenkcQEZnxF50P) Invalid string: {:"0"=>"pm_1K8omAH1x8t6cpUIKtsSf31L"}

gilded field
#

Thanks for sharing your code, I'm having a look.

midnight scaffold
#

It's ok I found my error

gilded field
#

In your second example, I believe the code should be
['invoice_settings'=>['default_payment_method'=>'pm_1K8omAH1x8t6cpUIKtsSf31L']]

midnight scaffold
#

Yes exaclty

gilded field
#

The default_payment_method needs to be a string, not an array.

midnight scaffold
#

Now I'm having a new error, it doesn't seem to allowed neither to define the default payment method to the customer or to attach it to the subscription because :
"The customer does not have a payment method with the ID pm_1K8omAH1x8t6cpUIKtsSf31L. The payment method must be attached to the customer."

#

I used to attached the customer to the payment methode instead, this way/

$stripe->paymentMethods->attach(
$payment_methode['id'],
['customer' => $session['customer']]
);

and here's want I get:

"This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again."

gilded field
#

That's true, the Payment Method needs to be attached to the customer before making it the default payment method (sorry for not making that clearer earlier).
This can be done automatically while creating the checkout session (to avoid the second error you saw) by setting payment_intent_data.setup_future_usage to either on_session or off_session depending on your needs.
You can learn more about this in our documentation: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-setup_future_usage