Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra card to Stripe customer, in Curl?

I'm trying to get my head around this. Basically, it looks like there are a ton of PHP/Ruby/Node etc modules, but again a very lacking Perl one (Business::Stripe, that hasn't been updated since 2012!)

So, I've resorted to doing my own updates using curl. It's a bit messy, as I've used the Perl module for the basic customer creation, and then a curl query to subscribe them to a package (as the Perl module doesn't offer "plans" as a feature)

  use Business::Stripe;
  my $stripe     = Business::Stripe->new(
    -api_key         => $CFG->{stripe_mode}
  );

  my $cid = $stripe->customers_create(
    card => $_[2],
    email => $in->{email},
    description => 'For Site Membership'
  );

Then subscribe with:

   my $sub = `curl https://api.stripe.com/v1/subscriptions -u $key: -d plan=name_$in->{package} -d customer=$cid`;
   my $json = decode_json($sub);

Now this works... but the problem I'm having, is how do I add a new card to this customer?

I can get the customer object back fine:

   my $sub = `curl https://api.stripe.com/v1/customers/cus_xxxx -u sk_test_KEY:`;
   my $json = decode_json($sub);

...and this gives me all the information on the user. But how do I add a new card for them? I have already got the code that asks for all the card information, and then passes back the token (where the card is associated with), but I'm stumped on the next step. I've tried googling around and I'm not coming up with anything helpful (there are loads of posts about it with example of ruby/node/php, but none is pure curl :/)

like image 488
Andrew Newby Avatar asked Oct 14 '25 20:10

Andrew Newby


2 Answers

Eugh, I knew this would happen!!!! Literally minutes after posting this, I came across:

https://stripe.com/docs/api#card_object

So the solution is to just pass the token along via:

my $res = `curl https://api.stripe.com/v1/customers/$USER->{stripe_customer_id}/sources -u $key: -d source=$in->{token}`;

...and then decode:

my $json = decode_json($res);

...and this returns an object with the new card:

$VAR1 = {
      'object' => 'card',
      'address_zip' => undef,
      'address_state' => undef,
      'fingerprint' => 'lRvQRC14boreOKjk',
      'brand' => 'Visa',
      'tokenization_method' => undef,
      'dynamic_last4' => undef,
      'address_zip_check' => undef,
      'address_line2' => undef,
      'funding' => 'credit',
      'exp_month' => 12,
      'address_city' => undef,
      'metadata' => {},
      'id' => 'card_xxxx',
      'country' => 'US',
      'name' => '[email protected]',
      'exp_year' => 2019,
      'address_line1' => undef,
      'address_country' => undef,
      'cvc_check' => 'pass',
      'customer' => 'cus_xxxx',
      'last4' => '4242',
      'address_line1_check' => undef
    };

...and sure enough, it now shows the extra card associated with the user:

enter image description here

Hopefully this helps save someone else the hassle :)

Also, I came across a much better Perl module for doing it programmatically:

http://search.cpan.org/~rconover/Net-Stripe-0.30/lib/Net/Stripe.pm

like image 91
Andrew Newby Avatar answered Oct 18 '25 00:10

Andrew Newby


I recommend you use Net::API::Stripe, which is the most comprehensive perl library for Stripe with over 200 modules covering all aspects of Stripe API.

To achieve what you want, you would do:

my $stripe = Net::API::Stripe->new({
    conf_file => './stripe-settings.json',
    # Switch it to 1 once you are ready to go live
    livemode => 0,
    ignore_unknown_parameters => 1,
    expand => 'all',
}) || die( Net::API::Stripe->error );

my $cust = $stripe->customers( create => {
    email => $email,
    description => $description,
    payment_method => $payment_method,
}) || die( $stripe->error );

my $sub = $stripe->subscriptions( create => {
    customer => $cust,
    items    => [
        price => "price_1Le1oa2eZvKYlo2CuD7mwpZu",
    ]
}) || die( $stripe->error );

Full disclosure, I am the author of that perl module.
like image 32
Jacques Avatar answered Oct 18 '25 02:10

Jacques



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!