dongqiao9015 2016-03-18 03:58
浏览 79

如何在Stripe中创建变量订阅

I am using stripe to capture credit cards. I have my forms so that they are variable inputs, and it works great. I am passing the information from my <input> to a charge.php file and it captures successfully.

When I try to use this information to create subscriptions, I am unable to use variable amounts. I can only create a subscription that has a set amount.

I was hoping to use the $finalamount to set the amount of the subscription. I am okay with the name and id to be the same as the amount.

How can I create a variable subscription including custom amount, name, and id based on what the user inputs?

<?php

require_once('init.php');

\Stripe\Stripe::setApiKey("sk_test_***********");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
$finalamount = $amount * 100;

\Stripe\Plan::create(array(
  "amount" => $finalamount, //this does not work. It only works if there is a present amount. 
  "interval" => "month",
  "name" => "Green Plan",
  "currency" => "usd",
  "id" => "green")
);

// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "plan" => "green",
  "description" => "Description",
  "email" => $email)
);


// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => $finalamount, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);

?>
  • 写回答

1条回答 默认 最新

  • duanchui1955 2016-03-18 04:31
    关注

    You need to remove this code

    \Stripe\Charge::create(array(
      "amount" => $finalamount, // amount in cents, again
      "currency" => "usd",
      "customer" => $customer->id)
    );
    

    because when you create a customer specifying plan your customer is subscribed and charged automatically.

    hope it helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应