Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the withCurrency and withUserDefaultCurrency SubscriptionBuilderClass Methods #1571

Closed
wants to merge 5 commits into from

Conversation

edulazaro
Copy link

Now it's possible o specify the currency for the subscriptions, so it's possible to use the customer currency with prices which have values on many currencies.

There are 2 tests included. On the second test, when a subscription is created with X currency, then X become the default currency for the customer. After that, the withUserDefaultCurrency method is used with another subscription, selecting the user default currency. Otherwise, the subscription will fail, as the price base currency will be selected, showing:

The currency of the selected prices (usd) does not match the customer currency (eur).

Related issue #1570

@edulazaro edulazaro changed the title Add the withCurrency and withUserDefaultCurrency SubscriptionBuilderClassMethods Add the withCurrency and withUserDefaultCurrency SubscriptionBuilderClass Methods Sep 19, 2023
@taylorotwell taylorotwell marked this pull request as draft September 19, 2023 15:28
@edulazaro edulazaro marked this pull request as ready for review September 20, 2023 08:50
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@edulazaro
Copy link
Author

@taylorotwell

This does not correct a bug as long as many currencies are not assigned to a price.

Providing further explanation, let's say that at Stripe we create a price and then assign an amount for each currency:

image

Let's assume there's a product called PlanA, it has 2 prices, one for monthly subscriptions and another for yearly, and each of them with GBP as base currency but that also with values for USD, SEK and EUR.

Now, let's say that a customer is on Sweden and already have a stripe_id, or in the same way, imagine the customer subscribes to the price using stripe elements directly, setting SEK as currency:

$stripe->subscriptions->create([
    'customer' => auth()->user()->stripe_id,
    'items' => [[
        'price' => PRICE_ID_HERE
    ]],
    'currency' => 'SEK',
    'payment_behavior' => 'default_incomplete',
    'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
    'expand' => ['latest_invoice.payment_intent'],
    'metadata' => [
       'name' => HERE_THE_PLAN_A_NAME,
     ]
]);

This customer will have SEK as its main currency, or might have SEK assigned from before (the currency cannot be changed once subscribed).

Now, imagine that next month this customer wants to subscribe to PlanB. which also has a price in GBP as base currency but that also has a price for USD, SEK and EUR.

I will let the customer pay with the card which is already saved as the default payment method, so I will do:

auth()->user()->newSubscription(PLAN_B_NAME, PRICEB_ID_HERE)->create(PM_ID);

This is what will happen:

image

This is because it's not possible to specify the currency, so the default one will be used, which might no be the same as the customer currency. To sort it, it's enough with sending the currency to Stripe, so I just added the 2 methods above, so you can do:

auth()->user()->newSubscription(PLAN_B_NAME, PRICE_ID_HERE)->withCurrency('SEK')->create(PM_ID);

The subscription will be successfully created.

Or also:

auth()->user()->newSubscription(PLAN_B_NAME, PRICE_ID_HERE)->withUserDefaultCurrency()->create(PM_ID);

If it cannot be added, we can use a fork or extend some classes meanwhile.

Thanks for reading!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants