#kelley_element-futurepayments
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1278774993106501642
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
kelley_element-futurepayments
but I am missing the step or part of the step to save the payment method.
the way you do this is withsetup_future_usagewhich is covered in that doc. Do you know if you are using that parameter?
If you're unsure, can you give me an example PaymentIntent idpi_1234and I can look and show you what's missing?
Soo. in the payment intent I need to add that?
So when creating the payment intent I just need to pass the parameter of setup_future_usage?
and then will the response return some id for the payment method?
sorry that's a picture of your IDE.
Can you share real code as text, developer to developer?
ok one sec.
if ( ! $this->canAutoCreatePaymentIntent( $order_transaction ) ) {
throw new \Exception( "Can't create a payment intent: " . print_r( $order_transaction, true ) );
}
$cpm = $this->ea->ref( $order_transaction['customer_payment_method_id'] );
$token = CustomerPaymentMethodStripeToken::fromCustomerPaymentMethod( $cpm );
$pi = $this->stripeClient()->paymentIntents->create( [
'setup_future_usage' => 'off_session',
'automatic_payment_methods' => [ 'enabled' => true ],
'amount' => $order_transaction['amount'] * 100,
'currency' => strtolower( Settings::get_currency_code() ),
] + ( $token->stripeCustomer() ? [ 'customer' => $token->stripeCustomer()->id ] : [] ) );
if ( $pi && nc_g( $order_transaction, 'id' ) ) {
$this->ea->update( 'order_transactions', $order_transaction, [
'identity_token' => $pi->id
] );
}
return $pi;
}```
Then if there is any changes to the order we can update existing pi.
$maybe_update = (
$table == 'order_transactions'
&& is_numeric(nc_g($changes, 'amount'))
&& $changes['amount'] > 0
&& $updated['identity_token']
&& $updated['customer_payment_method_id']
);
if ( $maybe_update ) {
$cpm = $this->ea->get( 'customer_payment_methods', [
'id' => $updated['customer_payment_method_id'],
'payment_provider.slug' => 'stripe'
] );
if ( $cpm ) {
$this->stripeClient()->paymentIntents->update( $updated['identity_token'], [
'amount' => $updated['amount'] * 100
] );
}
}
return $updated;
}```
Okay so you do have setup_future_usage: 'off_session' already. That means you're already telling us to save that PaymentMethod for future payments. Does that make sense to you? (I'll then explain the "bug")
Yes makes perfect sense. and I can see it stored in stripe.
So where do I get the id to create a charge in the future for a subscription? Is that in the response body when the payment is completed?
But how do I create a new charge with that same payment method on the server?
it's this step. After the PaymentIntent is confirmed successfully you have both the Customer id cus_1234 and the associated PaymentMethod id pm_12345
So we can use either or we have to use both?
both
OK so I should store the customer id and payment method id in the database?
Then when I'm ready to charge them again I do this:
- Grab the customer and payment method IDs
- Create a new paymentIntent
- Grab the payment method to charge (if there is more than one)
- Then just hit the charge method?
yes except I have no idea what #4 means. But you would create a new PaymentIntent and pass both customer and payment_method
- How does the charge happen? Via the paymentIntent?
yes that's what a PaymentIntent is for (and how you are accepting the first payment)
Of course ๐