Skip to content

Instantly share code, notes, and snippets.

@New0
Last active January 9, 2019 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save New0/659b7398285e820cb72330f265c5122b to your computer and use it in GitHub Desktop.
Save New0/659b7398285e820cb72330f265c5122b to your computer and use it in GitHub Desktop.
Subscribe a user to a plan based on a donation amount with Direct Stripe.
<?php
/*
* Plugin Name: DS custom code
* Description: Create subscriptions based on donation amount
* Author: Nicolas Figueira
* Version: 0.0.1
*
* To use this as a plugin :
* – Edit the code with the css ID you set for your button
* – Zip the file
* – Go to Add new plugin page of your WordPress admin
* – Click the Upload Plugin button and upload the zipped file
* – Activate the plugin and test
*
*** Also unset the capture option on the donation button in order not to capture the first payment twice
*** or add a trial period to the subscription ( "trial_period_days" => 30, )
*/
add_action('direct_stripe_before_success_redirection', function( $answer_id, $post_id, $button_id ) {
if( $button_id !== 'MyButtonID' ){ // Replace MyButtonID with your the CSS ID set on the button that will convert donation amount to subscriptions
return $answer_id;
} else {
$charge = \Stripe\Charge::retrieve($answer_id);
$new_plan = Stripe\Plan::create([
"amount" => $charge->amount,
"interval" => "month",
"product" => [
"name" => "Plan set by donation of " . $charge->customer
],
"currency" => "eur",
"id" => "special-" . $answer_id
]);
$subscription = Stripe\Subscription::create([
"customer" => $charge->customer,
"items" => [
[
"plan" => $new_plan,
],
]
]);
return $answer_id;
}
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment