Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.