简体   繁体   中英

Stripe: How to set up recurring payments without plan?

First time working with Stripe API. Implementing it on WordPress using PHP and JS. Working on a donation form. Donor should be able to choose a suggested amount (radio buttons-25,50,75,100) or pay as he/she wishes (text field after selecting 'other'). I was able to get this working.

There is a check box to set the amount up as a recurring payment. I created recurring payment plans for the fixed options like 25, 50, 100 etc.

How do I set up a recurring payment if the donor chooses a custom amount? Can't find the relevant API. Please help.

Another approach that Stripe suggests is to setup a plan with a recurring amount of $1 (or $0.01 for more flexibility) and then vary the quantity as needed.

eg Using the $0.01 plan approach, if I wanted to charge 12.50/month I could adjust the quantity like so:

$customer->subscriptions->create(array("plan" => "basic", "quantity" => "1250"));

Stripe Support

First, you'll need to create a new customer .

On submit, you could use the custom amount to create a new plan :

$current_time = time();
$plan_name = strval( $current_time );

Stripe_Plan::create(array(
        "amount" => $_POST['custom-amount'],
        "interval" => "month",
        "name" => "Some Plan Name " . $_POST['customer-name'],
        "currency" => "usd",
        "id" => $plan_name
    )
);

Keep in mind that the 'id' needs to be unique. You could use the customer's name, a time stamp, or some other random method to ensure that this is always the case.

You'd then just create the subscription on the newly-added customer :

$customer = Stripe_Customer::retrieve($customer_just_created);
$customer->subscriptions->create(array("plan" => $plan_name));

You probably will be able to omit the first line above, as you should already have a customer variable assigned from when the customer was actually created.

This is low tech, but the easiest thing I found was to use as little of the Stripe API as possible. Instead of creating subscription plans and products and things like that, you just:

  • Create a stripe customer.
  • Charge them with a cron job.

If you already know how to charge somebody, you just have to figure out how to create the customer, and then the rest is good to go.

This means you shift some of Stripe's cleverness to our own infrastructure, but I actually found this easier to think about/maintain than thinking through all of Stripe's API docs and features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM