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!
04.
Give the User a Subscription (in our Database)
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!
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
}
}
24 Comments
hey,
i want get the current account balance, createdAt and email from the stripeapi(https://stripe.com/docs/api#customer_object-account_balance).
I've wrote a own Action in my ProfileControle to try this, but it won't work as expected.
here the code what i've tried so far from my showCustomerDetailAction():
and in my account.html.twig i have:
whitch looks like this: http://imgur.com/a/Wrce6
now i actually want it dynamically and not hardcoded. but i don't know where to start.
Is there an easier way to do it, not like i did?
Hey Blueblazer172 ,
What do you mean when say it won't work as expected? Do you have some errors? What problem exactly do you have? Your code looks valid and fine for me :)
Cheers!
Hi victor ,
if uncomment e.g {{ balance }}. it says the variable email is undefined. The same error is for email and created. But i pass the parameter to the account.html.twig in my showCustomerDetailAction().
I think there is no connection to stripe or the Action() Method has wrong Code.
Could one cause the issue for the above error? and what do i have to change if so ?
Thanks:)
Hm, it's weird! Because I see you pass those vars to the template, so the worst that could be is that you print nothing instead of those variables if they are null. Probably you do a mistake somewhere. Could you debug what you get back from Stripe in this action? Dump it below after you set those variables:
Also, looks closely to this error, Do you have it exactly in "profile/account.html.twig" template?
Cheers!
still this errors:
Variable "balance" does not exist in profile\account.html.twig at line 99
Variable "email" does not exist in profile\account.html.twig at line 106
Variable "created" does not exist in profile\account.html.twig at line 113
and my dumped output is here:
https://www2.pic-upload.de/...
so it seems the api works perfectly. I never user the name="show_customer_detail" attribute from the ProfileController.
I dont know where to load it. I think that is the problem.
Can you solve it ? :)
Hey Blueblazer172!
Ok, as will all tough-to-debug errors, this must be something quite small :). As you probably know now, the error 100% doesn't have anything to do with whether or not the Stripe API calls are working. It's actually much simpler than that: this error literally means that you are simply NOT passing "balance", "email" or "created" variables into your template. It's not that these may be null or something, it literally means that you are failing to pass them to your template - the variables are undefined.
And that's the mystery :). Because you are passing the variables into your template from your controller - we can see it very plainly:
This definitely means that you (should) have "balance", "created" and "email" variables in your template. So, for now, like Victor, I'm a bit stumped! So here's the help I need to help track this down:
1) Comment out the variables in account.html.twig once again so that the page works. Also, remove the dump() calls you have in your controller. Then, anywhere in account.html.twig, add this code:
That's it. What does this look like? This will dump all variables we have access to in our template, and might help hint us as to what's going wrong.
2) Post all of your code :). I mean, your entire controller file and your entire template. Also, can you take a full screenshot of your entire page - I want to see what the web debug toolbar looks like at the bottom.
Thanks!
sorry for my late reply:) i've been busy at the weekend xD
so here is the dummped data: http://imgur.com/FZANocY
and my code is here for the
ProfileController.php: http://pastebin.com/rhy5SD0X
and account.html.twig: http://pastebin.com/5pgLH7eL
Thanks again Ryan and Victor for any help:)
so it seems that the variables are not passed to the template anyways :/
Hey Blueblazer!
I don't know if you already know this but you can use {{ dump(variable) }} in your twig templates, so you can see what you are really passing to them :)
Yeah this works now ;) thanks anyways
Hey Blueblazer172 ,
Hm, your PasteBin links doesn't work anymore :/
It's weird, looks like you're talking about 2 different templates: you pass variables into one, but do "dump()" in another one. Are you sure you wrote "dump()" in template which is located exactly in "app/Resources/views/profile/account.html.twig" path?
Cheers!
remove the last ) from the url and you should see it :) xD
yeah i'm absolutely sure
Ok, I think I see a problem...
Actually, the problem number one: I bet you're on "/profile" page but you should be on the "/profile/customer" - see your route definitions, for both routes you're using the same "profile/account.html.twig" template. So go to the "/profile/customer" page to see email, balance and created at date.
The problem number too: you can share this templates with 2 actions, but then you should pass all those variables in both actions. Or you can add some extra checks in your template to ensure that variables are defined, i.e. use if statements with "is defined" Twig test in them. Or you just need to use different template for your new showCustomerDetailAction() since "profile/account.html.twig" one is already used in accountAction().
Cheers!
i've rendered it in the accountAction and passed the variables and now it is working. so stupid that i've not seen it before xD
Haha, as always, simple mistakes hard to reveal ;) I'm glad you figured this out finally!
Cheers!
okay now i have again errors :P this won't stop xD
when i register as a new customer i cannot access the profile page, because there is no stripe user with that email. now my question is how can i create the stripe user on registration? i've looked at the docs from the api but i didnt know how to handle it :/
Hey Blueblazer172 ,
Probably you don't need to create a new Stripe customer for new users - you have to create it only when they do purchases. Otherwise you will have a lot of empty Stripe customers who probably could never buy anything on your site. So just to do some extra checks and display hardcoded values for new users, I mean just print "0" for their balance, render User::getEmail()/User::getCreatedAt() to show their current email and created at date.
But if you still want to create a new Stripe customer for every new user - then you have to hook to the registration process. If you use FOSUserBundle - they dispatch an event when a new user is registered. If you write custom registration - then you just need to add a few line of code into it.
Cheers!
thanks :)
currently i send the users a flash message when they want to access the account page without having purchased anything.
i'll try your hint :)
hey,
how can i assign a role to a user e.g if he bought the new zelander he should get the role ROLE_NEW_ZEALANDER?
should i do this after successfully purchasing the subscription? i think yes. But how ?
Thanks :)
Hey Blueblazer!
Are you using FOSUserBundle for managing your Users ? In that case you could use
$user->addRole("ROLE_NEW_ZEALANDER");
just dont forget to persist it
But, why you need to add a new role to the user ? He will have access to secret sections or something ?
Have a nice day :)
Yes I am using FOS. ;)
Okay thanks a lot ;)
Yeah I want to let the users only with New Zealander to access some pages. That is easy ;)
I forgot to persist it to the db and so I had some errors 🙈 But now it is working ;)
I'm glad to hear you could fix it :)
Cheers!
is this the right way ? :P
this too? :P
Hey Blueblazer!
There is a better way for accesing to Entity Manager, like this:
@Security("is_granted('ROLE_REGISTERED_MEMBER') and (has_role('ROLE_PRO_USER') or has_role('ROLE_LEGEND_USER'))")
`Cheers!
"Houston: no signs of life"
Start the conversation!