How to create variable subscriptions in Stripe

拜拜、爱过 提交于 2019-12-12 03:34:54

问题


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:


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 :)



来源:https://stackoverflow.com/questions/36075869/how-to-create-variable-subscriptions-in-stripe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!