capacitor-community / stripe

Stripe Mobile SDK wrapper for Capacitor
MIT License
185 stars 74 forks source link

Subscriptions #104

Closed jjacobs33589 closed 2 years ago

jjacobs33589 commented 2 years ago

Can this plugin be used to set up subscriptions with Stripe?

rdlabo commented 2 years ago

@jjacobs33589 Maybe yes. Difference of payment once or subscription looks like on backend only.

This plugin can paymentIntent and setupIntent. Please try to implement.

loicparent commented 2 years ago

Hello @jjacobs33589 & @rdlabo, I'm trying to setup the backend in this way but I encounter blocking.

The backend is a WordPress Website so a PHP function in an API route. I arrive to create the customer and the subscription but, I don't arrive to return the same value as indicated for this component.

For example: I have the customer ID but I don't have the ephemeralKey and I don't see anything about it in the documentation of Stripe (https://stripe.com/docs/api). I don't have the paymentIntent in the payload returned by the subscription too.

I'm a new user of Stripe and this component, could you maybe help me to better understand what I have to setup for the subscriptions?

Thanks and have a nice day, Loïc

stickypages commented 2 years ago

Done on your server where you have implemented your other stripe code.

Stripe.ephemeralKeys.create(
    { customer: customerId },
    { stripe_version: api_version }
});

You have to create your paymentIntent as well.

Stripe.paymentIntents.create({
    amount: cart.value.total_after_tip,
    currency,
    payment_method_types: ['card']
})

Not sure if that helps.

loicparent commented 2 years ago

Hi @stickypages,

Thank you for your reply :)

Ok but how can I link the paymentIntents with the subscription object? In fact, I have precise the amount, currency and payment_method_types in the subscription ans so, this are duplicate here.

Kind regards

loicparent commented 2 years ago

Also, is it possible that the "ephemeralKey" doesn't exist anymore? I don't see anything in the API documentation about this :-/

hideokamoto commented 2 years ago

Have you tried stripe.ephemeralKeys.create ? You can see the example code -> https://github.com/capacitor-community/stripe/blob/master/demo/server/src/app.controller.ts#L23-L26

hideokamoto commented 2 years ago

FYI The type information is here -> https://github.com/stripe/stripe-node/blob/master/types/2020-08-27/EphemeralKeys.d.ts#L68-L71

loicparent commented 2 years ago

Hi @hideokamoto,

Thank you for your reply :)

I use the PHP library of Stripe. I have seen some examples with the ephemeralKey but when I try to implement it, I have an error. Also, in the API documentation, I see anything about it (https://stripe.com/docs/api?lang=php).

On the other hand, by looking in the code of the PHP library, I see that there is indeed a class which manages this: https://github.com/stripe/stripe-php/blob/master/lib/EphemeralKey.php

I will try to test without the API documentation. Thanks!

Do you maybe have an idea for the "paymentIntents" combined with the subscription?

Kind regards

hideokamoto commented 2 years ago

Do you maybe have an idea for the "paymentIntents" combined with the subscription?

Probably we can get the payment intent of the subscription by the following code.

via: https://stripe.com/docs/billing/subscriptions/build-subscription?ui=elements#create-subscription

    const subscription = await stripe.subscriptions.create({
      customer: customerId,
      items: [{
        price: priceId,
      }],
      payment_behavior: 'default_incomplete',
      expand: ['latest_invoice.payment_intent'],
    });

    res.send({
      subscriptionId: subscription.id,
      clientSecret: subscription.latest_invoice.payment_intent.client_secret,
    });

It's just an example of Node.js, but I believe we can develop the same behavior in any other lang. I hope this information will help you.

loicparent commented 2 years ago

Hi @hideokamoto,

Yes, it helps me a lot, thanks!

So this is similar in PHP but not really well explained in the API documentation.

  1. Get the paymentIntent from the subscription:
    
    $subscription = $stripe->subscriptions->create([
    "customer" => $customer->id,
    "payment_behavior" => "default_incomplete",
    "items" => [
    [
      "price" => $subscription_price,
      "quantity" => "1"
    ]
    ],
    "expand" => ["latest_invoice.payment_intent"],
    ]);

$payment_intent_secret = $subscription->latest_invoice->payment_intent->client_secret;


2. Get the ephemeralKey:
```php
$ephemeral_key = $stripe->ephemeralKeys->create(
  ['customer' => $customer->id],
  ['stripe_version' => '2020-08-27']
);

$ephemeral_key_secret = $ephemeral_key->secret;

EDIT: In conclusion, I arrive to process the payment with a subscription using this tool.

Have a nice day