We <3 Creating Stripe Customers

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.

Start your All-Access Pass
Buy just this tutorial for $9.00

Head back to OrderController. Create a $user variable set to $this->getUser():

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 38
\Stripe\Stripe::setApiKey($this->getParameter('stripe_secret_key'));
/** @var User $user */
$user = $this->getUser();
... lines 43 - 70
}
... lines 72 - 78
}
}

This is the User object for who is currently logged in. I'll add some inline documentation to show that.

When the user submits the payment form, there are two different scenarios. First, if (!$user->getStripeCustomerId()), then this is a first-time buyer, and we need to create a new Stripe Customer for them:

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 40
/** @var User $user */
$user = $this->getUser();
if (!$user->getStripeCustomerId()) {
... lines 44 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

To do that, go back to their API documentation and find Create A Customer. Oh hey, it wrote the code for us again! Steal it! And paste it right inside the if statement:

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
$customer = \Stripe\Customer::create([
'email' => $user->getEmail(),
'source' => $token
]);
... lines 48 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

Customer has a lot of fields, but most are optional. Let's set email to $user->getEmail():

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
$customer = \Stripe\Customer::create([
'email' => $user->getEmail(),
... line 46
]);
... lines 48 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

So we can easily look up a user in Stripe's dashboard later.

The really important field is source. This refers to the payment source - so credit or debit card in our case - that you want to attach to the customer. Set this to the $token variable:

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
$customer = \Stripe\Customer::create([
... line 45
'source' => $token
]);
... lines 48 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

This is huge: it will attach that card to their account, and allow us - if we want to - to charge them using that same card in the future.

Set this call to a new $customer variable: the create() method returns a Stripe\Customer object:

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
$customer = \Stripe\Customer::create([
'email' => $user->getEmail(),
'source' => $token
]);
... lines 48 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

And we like that because this object has an id property.

To save that on our user record, say $user->setStripeCustomerId($customer->id):

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
$customer = \Stripe\Customer::create([
'email' => $user->getEmail(),
'source' => $token
]);
$user->setStripeCustomerId($customer->id);
... lines 49 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

Then, I'll use Doctrine to run the UPDATE query to the database:

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
... lines 44 - 47
$user->setStripeCustomerId($customer->id);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
... lines 53 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

If you're not using Doctrine, just make sure to update the user record in the database however you want.

Fetching the Existing Customer Object

Now, add the else: this means the user already has a Stripe customer object. Repeat customer! Instead of creating a new one, just fetch the customer with \Stripe\Customer::retrieve() and pass it $user->getStripeCustomerId():

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
... lines 44 - 52
} else {
$customer = \Stripe\Customer::retrieve($user->getStripeCustomerId());
... lines 55 - 57
}
... lines 59 - 70
}
... lines 72 - 78
}
}

Since this user is already in Stripe, we might eventually re-work our checkout page so that they don't need to re-enter their credit card. But, we haven't done that yet. And since they just submitted fresh card information, we should update their account with that. After all, this might be a different card than what they used the first time they ordered.

To do that, update the source field: set it to $token. To send that update to Stripe, call $customer->save():

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 42
if (!$user->getStripeCustomerId()) {
... lines 44 - 52
} else {
$customer = \Stripe\Customer::retrieve($user->getStripeCustomerId());
$customer->source = $token;
$customer->save();
}
... lines 59 - 70
}
... lines 72 - 78
}
}

So in both situations, the token will now be attached to the customer that's associated with our user. Phew!

Charging the User

The last thing we need to update is the Charge: instead of passing source, charge the customer instead. Set 'customer' => $user->getStripeCustomerId():

... lines 1 - 11
class OrderController extends BaseController
{
... lines 14 - 31
public function checkoutAction(Request $request)
{
... lines 34 - 35
if ($request->isMethod('POST')) {
... lines 37 - 59
\Stripe\Charge::create(array(
... lines 61 - 62
"customer" => $user->getStripeCustomerId(),
... line 64
));
... lines 66 - 70
}
... lines 72 - 78
}
}

We're no long saying "Charge this credit card", we're saying "Charge this customer, using whatever credit card they have on file".

Ok, time to try it out! Go back and reload this page. Run through the checkout with our fake data and hit Pay. Hey, hey - no errors!

So go check your Stripe dashboard. Under Payments, you should see this new charge. And if you click into it, it is now associated with a customer. Success! The customer page shows even more information: the attached card, any past payments and eventually subscriptions. This is one big step forward.

Copy the customer's id and query for that on our fos_user table. Yes, it did update!

Since adding a customer went so well, let's talk about invoices.

Leave a comment!

  • 2020-03-23 weaverryan

    Hey Martin!

    Hmmm. Sorry about that! Thanks for posting - it's on my list to add some notes through the tutorial so that people are aware of changes. It looks like (but this is just a guess) this change might be due to a newer version of the SDK.

    Cheers!

  • 2020-03-21 Martin

    I am unable to update the source with:

    $customer = \Stripe\Customer::retrieve($user->getStripeCustomerId());
    $customer->source = $token;
    $customer->save();

    I have also tried with $customer->sources and $customer->default_source which are the new fields available since https://stripe.com/docs/upg...

    This is what I am doing to update the Customer's source:

    Customer::update(
    $user->getStripeCustomerId(),
    [
    'source' => $token,
    // any other data
    ]
    );

  • 2020-03-09 weaverryan

    Thanks for the update Carlos Alberto Martínez Gadea - I appreciate it - these notes will help me with any updates or notes we need to add :).

    Cheers!

  • 2020-03-06 Carlos Alberto Martínez Gadea

    I have to come back, and inform you that the 'source' field is working fine. I was using an old Stripe PHP package (4.x). Now with the 7.x works like a charm.

    Sorry for the misunderstanding.

  • 2020-03-05 weaverryan

    Hi Carlos Alberto Martínez Gadea!

    Thank you for that feedback - it's super useful to know that the bulk of the tutorial is still working. That makes me lean towards getting some well-placed notes in those few area that need updates. And thanks for the kind word - it's *most* important to me that the tutorials are saving people time ❤️

    Cheers!

  • 2020-03-04 Carlos Alberto Martínez Gadea

    I ended up using the 'sources' field, assigning to the first position the new token.

    I don't think the tutorial should be removed or deprecated. Simply updated. Aside from this, and the other comment (which I'll answer you now), the rest has worked like a charm. It's the best tutorial to implement Stripe payments over Symfony (although it isn't a requirement and most of it could be used in other PHP framework).

    Thanks for the tutorial, by the way :)

  • 2020-03-03 weaverryan

    Hey Carlos Alberto Martínez Gadea!

    You're totally right. Stripe has since updated their API and doesn't have a source field anymore. I believe the change is from this API upgrade - https://stripe.com/docs/upg... - I believe that "source" is (as you said) now effectively default_source.

    I hope that helps! The tutorial, unfortunately, is starting to show it's age - it's on my list to update or deprecate it officially.

    Cheers!

  • 2020-03-03 Carlos Alberto Martínez Gadea

    The Customer entity in Stripe, at least the one I have, doesn't have the 'source' field, but the 'default_source' (and the 'sources' field as well).