perl - 在 Curl 中向 Stripe 客户添加额外的卡?

标签 perl stripe-payments

我正在努力解决这个问题。基本上,看起来有大量的 PHP/Ruby/Node 等模块,但又是一个非常缺乏 Perl 的模块(Business::Stripe,自 2012 年以来就没有更新过!)

所以,我求助于使用 curl 进行自己的更新。有点困惑,因为我使用 Perl 模块来创建基本的客户,然后使用 curl 查询为他们订阅包(因为 Perl 模块不提供“计划”作为功能)

  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'
  );

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

现在这可行......但我遇到的问题是如何向该客户添加新卡?

我可以很好地恢复客户对象:
   my $sub = `curl https://api.stripe.com/v1/customers/cus_xxxx -u sk_test_KEY:`;
   my $json = decode_json($sub);

...这为我提供了有关用户的所有信息。但是我如何为他们添加一张新卡呢?我已经获得了询问所有卡信息的代码,然后将 token (与卡关联的地方)传回,但我对下一步感到困惑。我试过用谷歌搜索,但没有找到任何有用的东西(有很多关于它的帖子,例如 ruby​​/node/php,但没有一个是纯 curl :/)

提前致谢!

最佳答案

呃,我就知道会这样!!!!从字面上看,在发布此消息几分钟后,我遇到了:

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

所以解决方案是通过以下方式传递 token :

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

...然后解码:
my $json = decode_json($res);

...这将返回一个带有新卡的对象:
$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' => 'andy@domain.co.uk',
      'exp_year' => 2019,
      'address_line1' => undef,
      'address_country' => undef,
      'cvc_check' => 'pass',
      'customer' => 'cus_xxxx',
      'last4' => '4242',
      'address_line1_check' => undef
    };

...果然,它现在显示与用户关联的额外卡:

enter image description here

希望这有助于为其他人省去麻烦:)

此外,我遇到了一个更好的 Perl 模块,可以通过编程方式进行:

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

关于perl - 在 Curl 中向 Stripe 客户添加额外的卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001753/

相关文章:

linux - 如何使用 Perl 打印到文本文件?

perl - 如何在具有多个 Perl 版本的 Perl 模块中安装脚本?

javascript - Stripe 支付 : Getting Error as Customer cus_***** does not have a linked card with ID tok_*****

javascript - 使用 Stripe API,如何检查用户是否为特定产品付费?

ruby-on-rails - 在 Rails 中以美分存储价格

regex - 如何使用 Perl 将以下字符串中的逗号替换为 & ?

Template Toolkit 中的 Perl 逻辑

python - 计算两个模式之间的重复记录

ios - 使用 Stripe iOS SDK 标记银行信息

android - 将 Stripe 的 creatToken 转换为 Kotlin