Chapters
35 Chapters
|
3:22:53
|
This course is archived!
This tutorial uses an older version of Symfony of the stripe-php SDK. The majority of the concepts are still valid, though there *are* differences. We've done our best to add notes & comments that describe these changes.
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.5.9, <7.4
Subscribe to download the code!Compatible PHP versions: >=5.5.9, <7.4
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
29.
Monthly to Yearly: The Billing Period Change
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial uses an older version of Symfony of the stripe-php SDK. The majority of the concepts are still valid, though there *are* differences. We've done our best to add notes & comments that describe these changes.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.5.9, <7.4",
"symfony/symfony": "3.1.*", // v3.1.10
"doctrine/orm": "^2.5", // v2.7.2
"doctrine/doctrine-bundle": "^1.6", // 1.6.8
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
"symfony/swiftmailer-bundle": "^2.3", // v2.6.2
"symfony/monolog-bundle": "^2.8", // v2.12.1
"symfony/polyfill-apcu": "^1.0", // v1.3.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.26
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"friendsofsymfony/user-bundle": "~2.0.1", // v2.0.1
"stof/doctrine-extensions-bundle": "^1.2", // v1.2.2
"stripe/stripe-php": "^3.15", // v3.23.0
"doctrine/doctrine-migrations-bundle": "^1.1", // v1.2.1
"phpunit/phpunit": "^5.5", // 5.7.20
"composer/package-versions-deprecated": "^1.11" // 1.11.99
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.4
"symfony/phpunit-bridge": "^3.0", // v3.3.0
"hautelook/alice-bundle": "^1.3", // v1.4.1
"doctrine/data-fixtures": "^1.2" // v1.2.2
}
}
12 Comments
Hello ! There is something that has not been tested. It is a question of passing from an annual subscription to monthly. And it causes a bug. If I click on "Bill monthly", he says to me:
You will have a balance of $0 that will be automatically applied to future invoices!Is there a solution ?
I have managed to solve this by using the
ending_balancefield of the Invoice. Also, I have noticed a separate issue usingamount_duewhen downgrading from a plan to another having the same duration.amount_dueworks fine when there is an upgrade, but not when there is a downgrade.Here is my code (apologies for the ugly nested else condition AND please note that I am using different logic in the condition that compare the plan durations)
Hey Martin,
Thanks for sharing your solution with others! Though, proration is a bit more complex than just price difference between 2 plans. It also takes into account how many "unused" time left from the old subscription, so the actual logic should be more complex, otherwise it will show the correct proration only in the beginning of your old subscription.
I hope this helps!
Cheers!
I've further improved the code removing my ugly nested else / if conditions. The previous solution I posted was also working - this one is just cleaner. I can confirm that this has been tested using 16 different scenarios including 6 days after initial subscription and the total always comes out calculated correctly and matching what Stripe actually charges.
Plan upgrade with same duration
Plan downgrade with same duration
Plan changed to lower duration
Plan changed to higher duration
Plan upgrade + higher duration
Plan upgrade + lower duration
Plan downgrade + higher duration
Plan downgrade + lower duration
(everything tested moments after the initial subscription + 6 days after initial subscription)
Hey Martin,
Thank you for sharing this with others! This sounds cool
Cheers!
Hey Kiuega!
Hmm. This could be due to another change in newer versions of the API. In the version of the API used in this tutorial, the
totalproperty that's returned from Stripe would be a negative number when going from yearly to monthly. After completing the change, that negative balance would be added to the "Customer" so that future invoices would automatically be lower. If you're seeing 0 fortotal, I bet that data is stored in another place or there is some other little tweak that needs to be made to get things working correctly.Let me know what you find out :). We use this exact feature here on SymfonyCasts.com but using an older API version.
Cheers!
Okay! I will also come back to this in a while to see if there is anything to do for the new version and I will tell you here
Hello @KNP team,
first of all, this tutorial is awesome and so well explained. It`s one of my favorite tutorials right now :)
But strange things happen now. When I switch from the yearly plan to the monthly, it says "You will have an approximate balance of $0 that will be automatically applied to future invoices!". Why does he get a $0 balance, switching from yearly to monthly? Shouldn`t the customer get notified about the potential forthcoming credit balance change when the subscription plan switches to monthly, i.e. "You will have an approximate balance of 890$ that will be automatically applied to future invoices!" ?
Because, when I proceed the following step and look into Stripe it actually says that the credit balance is $890.99, in the "preview", sweet alert does not reflect this (instead it gives me a 0 credit balance back).
The problem is, that when I switch from yearly to monthly, Stripe actually gives me in the upcoming invoice an amount_due of 0.
The two lines are (edited)
Because of amount_due = 0, previewPlanChangeAction in ProfileController:
returns a JSON Response of total = 0.
But don`t we want to inform the customer about his existing credit balance, after switching his plan?
Many thanks in advance,
Chris
Same problem, is there a solution ?
Hey Kiuega !
Ah, we missed replying to the original comment! A fail for us! As I just replied above - https://symfonycasts.com/sc... - I'm guessing that on a newer API version, the data must be available somewhere else. As Chris mentioned, after you complete the change from yearly to monthly, the account balance *is* correctly applied to the user. So the question is: where is Stripe exposing that data before the change? It's *possible* that they're not exposing it now, but that would surprise me.
Do you see any other data in the upcoming invoice that's useful?
Cheers!
In src/AppBundle/StripeClient.php on line 167 you compare
$stripeSubscription->current_period_start == $currentPeriodStart.
But $currentPeriodStart on line 158 equals $stripeSubscription->current_period_start.
Do you sure what comparison "if(true)" has the meaning?
Hey alexchromets
Nice question, and actually it look's like it is but we have a note about it on our code blocks. The thing here is that the currentPerdiodStart may change after saving the new plan, and it has an special meaning for Stripe:
Cheers!
"Houston: no signs of life"
Start the conversation!