1
votes

My code creates and loads the Stripe Checkout where the customer enters name and card details and is returned to a php script where the token is received and used to create a Stripe customer and then send a purchase request to charge their card.

How would I best add a subscription using the same card and the omnipay library where possible? Do I use the token or grab the source id from the purchase object and use that, and create a subscription after the purchase?

Inside a class, the call to createSubscription in the code below gives the error "This customer has no attached payment source".

public function createSubscription($token,$customer_id,$plan,$source_id)
{
        try {
            $response = $this->gateway->createSubscription(
                                array(
                                    'customerReference' => $customer_id,
                                    'plan'              => $plan,
                                    'source'            => $token
                                )
            )->send();
            if ($response->isSuccessful()) {
                $data = $response->getData();
return $data;
            }
        } catch (Exception $e) {
            $message = "Error creating Stripe subscription: " . $e->getMessage();
            return;
        }
    }
1
Stripe subscriptions will always charge the default card (default_source) on a customer, so if you've already added a card there is no need to pass 'source' What are you passing to 'source' here? A token obtained from Stripe.js/Checkout (for testing: tok_visa)?duck
I tried passing the token received from Stripe after the customer completes their 'Checkout'. Not sure whether 'source' here can be a token or whether it has to be either a source id or card object. I will try adding a subscription after charging the card, but not sure if the source will only be suitable for a one-time charge.Nick W
Couldn't get it to work after the purchase with either the source id or the token, I got 'invalid_request_error - This customer has no attached payment source'. I'll try again using the create customer request.Nick W

1 Answers

0
votes

I've succeeded by creating the customer with card details attached using the token, then creating a subscription using the 'plan' and 'customerReference' parameters, and to finally charging the card using the customerReference (isSuccessful check and error checking omitted for clarity):

 $response = $gateway->createCustomer(array(
     'description'       => 'Test Customer',
     'email'             => $_POST['stripeEmail'],
     'source'  => $token
 ))->send();

 $response = $gateway->createSubscription(array(
     "customerReference" => $customer_id,
     'plan' => 'plan_name',
 ))->send();

$transaction = $gateway->purchase(array(
                  'amount'   => '5.00',
                  'currency' => 'gbp',
                  'receipt_email' => '[email protected]',
                  "description" => 'Test Order',
                  'customerReference' => $customer_id,
));