XCoder
XCoder

Reputation: 51

Stripe : Specify Specific Card for Subscription

I have set up subscription based website that allows people to have multiple subscriptions. I decided to go with Stripe for payment and card processing. It took very little time to get it integrated into my Symfony2 project. I was able to create subscriptions, customers, and add cards within a couple of hours. Then I ran into an issue. If a customer has multiple cards, I wanted to be able to allow them to choose which card they wanted to use when they create a new subscription. It sounded easy. After 2 days and about 30 hours of combing through their documentation I have to say that I cannot figure out how to get this to work.

The way I have this set up is that when the customer creates a card I store the "card id" in my database along with the brand. This just makes it easy to load details on the server side when the page is being requested. The customer creating the new subscription sees their cards and choose which one they want to use for the new subscription. This is passed to my php script via AJAX to create the new subscription. However, when I try to use a specific card, I am getting a 400 error indicating that the "card id" is not a token. I know that it is not a token since the token was used to add the card to the customer account but how in the world do I specify the exact card that the customer wants to use?

NOTE: Using an a new token creates another instance of the card.Not an option.

PHP:

         require_once('../stripe-php/init.php');

    //Set Secret API Key
            \Stripe\Stripe::setApiKey("sk_test_XXXXXXXXXXXXXXXXXXXXX");

//Retrieve Customer
            $cu = \Stripe\Customer::retrieve($_POST['customer_id']);

//Create Subscription using saved customer "card id"
            $createSubscription = $cu->subscriptions->create(array("plan" => $_POST['sub_option'], "source" => $card));

POSTED TO STRIPE:

plan: "500-2016"
source: "card_xxxxxxxxxxxxxxxxxxxxx"

STRIPE ERROR: TYPE 400

error:
type: "invalid_request_error"
message: "No such token: card_xxxxxxxxxxxxxxxxxxxxxxx"
param: "source"

Upvotes: 3

Views: 2111

Answers (3)

user85461
user85461

Reputation: 6640

I got a reply from Stripe support on this: it is not possible for one Customer to have subscriptions with different payment sources. All invoices for subscriptions are always billed to the current default_source for the Customer. So if you change the default as Giles Bennett suggested, you'll be changing it for all subscriptions, regardless of what the default was at time of creation.

If you need one user to have subscriptions with more than one source, you need to create multiple stripe Customer objects for that user, with a different default_source for each.

Upvotes: 3

Giles Bennett
Giles Bennett

Reputation: 1636

I realise this thread is quite old, but having come across it whilst trying to answer the same question myself, this solution may be of use to those who come along afterwards.

The Stripe API says that the "source" parameter for the API call to create a new subscription is optional - if ommitted, then it will default to the customer's default card. If included, then it can only be a token (ie. a new card) or a dictionary entry (again, for a new card, just not tokenised).

The solution could be to update the customer's default source first. Using Cartalyst through Laravel, as we are :

$customer = Stripe::customers()->update( "customer_id", [
    'default_source' => "card_id"
]);

You can then proceed to add your subscription as normal - the newly-defaulted card will be assigned to it. If needs be (depending on your application) then you may also wish to save the previous default card ID to a variable first, to then allow you to set it back to being the default card after your new subscription.

I hope that helps.

Upvotes: 1

XCoder
XCoder

Reputation: 51

Since I have not received any input from SO or Stripe, I have somewhat came to the conclusion that this cannot be done. I found a similar question on a different forum that ended with the results being - No Response From Stripe - and that this cannot be done. Though the Stripe documentation does not hit on this subject it does appear that a Subscription can only be charged to the default card. There is no "Card" object for subscriptions as there is for a "Charge".

Upvotes: 1

Related Questions