Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a description to the Charge event triggered by a Direct Stripe Subscription

How to add a description to a charge triggered by a subscription of Direct Stripe WordPress plugin plugin

First steps from your Stripe dashboard

  • Log in the Stripe Dashboard

  • Add a webhook endpoint in Developers -> Webhooks -> ( Add endpoint ) button

Steps with the custom code to be edited and installed as a WordPress plugin

  • Edit the code of ds-custom-code.php file
  • Insert your own Secret API key
  • Edit with your own pricing plan ID
  • Edit with the decription you want to add to charges triggered by the Subscription
  • Zip the file

Last steps from your WordPress dashboard

  • Go to Plugins -> Add new
  • Click the Upload plugin button
  • Select the Zipped file of ds-custom-code.php
  • Activate the plugin
<?php
/*
* Plugin Name: DS custom code
* Author: Nicolas Figueira
* Description: Add description to Charge triggered by Subscription
* Plugin URI: https://gist.github.com/New0/d72217de1e60871cf938715c2fd03b61
*
* This code is an add-on of Direct Stripe WordPress plugin
* Explanation at https://gist.github.com/New0/d72217de1e60871cf938715c2fd03b61#file-ds-custom-code-how-to-md
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'direct-stripe/v1', '/subscription-charge-succeeded', array(
'methods' => 'POST',
'callback' => 'ds_add_description',
) );
function ds_add_description(){
if ( ! class_exists('Stripe\Stripe')) {
require_once( DSCORE_PATH . 'vendor/autoload.php' );
}
\Stripe\Stripe::setApiKey('Stripe_secret_API_Key'); // EDIT WITH YOUR OWN API KEY
$input = @file_get_contents('php://input');
$event = json_decode($input);
if( $event->data->object->lines->data[0]->plan->id === "pricing_plan_id" ) { // EDIT with the pricing plan ID used in the Direct Stripe Button
$charge = \Stripe\Charge::retrieve( $event->data->object->charge );
$charge->description = "Webhook edited description"; //EDIT with the description you want to add to charges
$charge->save();
}
return;
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment