#kelley_element-futurepayments

1 messages ยท Page 1 of 1 (latest)

lavish hearthBOT
#

๐Ÿ‘‹ 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.

river nest
#

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 with setup_future_usage which 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 id pi_1234 and I can look and show you what's missing?

sweet geode
#

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?

river nest
#

sorry that's a picture of your IDE.

#

Can you share real code as text, developer to developer?

sweet geode
#

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;
    }```
river nest
#

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")

sweet geode
#

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?

river nest
#

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

sweet geode
#

So we can use either or we have to use both?

river nest
#

both

sweet geode
#

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:

  1. Grab the customer and payment method IDs
  2. Create a new paymentIntent
  3. Grab the payment method to charge (if there is more than one)
  4. Then just hit the charge method?
river nest
#

yes except I have no idea what #4 means. But you would create a new PaymentIntent and pass both customer and payment_method

sweet geode
#
  1. How does the charge happen? Via the paymentIntent?
river nest
#

yes that's what a PaymentIntent is for (and how you are accepting the first payment)

sweet geode
#

OK understood.

#

This makes sense.

#

Thanks for helping me.

river nest
#

Of course ๐Ÿ™‚