Skip to content

Instantly share code, notes, and snippets.

@New0
Last active October 5, 2018 07:38
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/81f216f1a42965f4f080babedc526d72 to your computer and use it in GitHub Desktop.
Save New0/81f216f1a42965f4f080babedc526d72 to your computer and use it in GitHub Desktop.
Set the amount of a Direct Stripe button based on a variable available on page load
<?php
/*
* The 'ds_filter_params' filter hook is available since Direct Stripe 2.1.8
*
* We want to set the amount based on a query parameter sent on the previous page, (or any variable available on page load)
* We called the query variable ds_amount
*/
add_filter( 'ds_filter_params', function( $params ){
if( $params['button_id'] === 'MyButtonID' ) { //EDIT 'MyButtonID' with your own CSS Button ID
$new_amount = isset($_GET['ds_amount']) ? $_GET['ds_amount']: '';
if( !empty( $new_amount )) {
//Set amount to charge
$params['amount'] = base64_encode( $new_amount );
//Set amount to display in the Strioe modal form (In case the option Display amount is enabled)
if( strpos( $new_amount , ',') !== false ) {
$new_amount = str_replace(',', '.', $new_amount );
}
if( is_numeric( $new_amount ) ){
$params['original_amount'] = $new_amount * 100;
} else {
$params['original_amount'] = 0;
}
}
}
return $params;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment