Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active November 1, 2017 23:01
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 Shelob9/0a5d6c5b2ce0cdf63b755ed433b449cf to your computer and use it in GitHub Desktop.
Save Shelob9/0a5d6c5b2ce0cdf63b755ed433b449cf to your computer and use it in GitHub Desktop.
Examples of the cf_stripe_pre_payment filter to use stripe token generated by Caldera Forms for alternative API calls. See: https://calderaforms.com/doc/cf_stripe_pre_payment/
<?php
/**
* Use Caldera Forms Stripe Token with Stripe API
*
* In this example a customer is created and then an authorization, not a charge is created.
*/
add_filter( 'cf_stripe_pre_payment',function( $early_return, $token, $config, $form ){
//change your field IDs here!
$email = Caldera_Forms::get_field_data( 'fld1234567', $form );
$name = Caldera_Forms::get_field_data( 'fld1234568', $form );
$customer = \Stripe\Customer::create(array(
"description" => $name,
"email" => $email,
"source" => $token
));
\Stripe\Charge::create(array(
"amount" => 2000, //amount is in cents, this $20
"currency" => "usd",
"source" => $customer->id,
"capture" => false,//authorize, but not charge
"description" => "Authorization for $name $email"
));
//returning true prevents Caldera Forms default behaviour
//IMPORTANT: you can not use tokens twice, so if you returned null here, Caldera Forms would attempt to use same token, and fail
return true;
//You may also return an error array here
return array(
'type' => 'error',
'note' => 'Charge could not be completed'
);
}, 10, 4 );
/**
* Use Caldera Forms Stripe Token with Stripe API
*
* In this example a customer is created (so customer can be reused) and then customer is charged 1 time, according to processor settings
*/
add_filter( 'cf_stripe_pre_payment',function( $early_return, $token, $config, $form ){
//Create customer
try {
$customer = \Stripe\Customer::create(array(
"email" => Caldera_Forms::get_field_data($config['email'], $form),
"source" => $token
));
} catch (Exception $e ) {
//Stop processing and use Exception message as error.
return array(
'type' => 'error',
'note' => $e->getMessage()
);
}
try {
$charge = \Stripe\Charge::create(array(
"customer" => $customer,
"amount" => (Caldera_Forms::get_field_data($config['amount'], $form) * 100), // amount in cents, again
"currency" => strtolower($config['currency']),
"description" => $config['description'],
));
} catch (Exception $e) {
//Stop processing and use Exception message as error.
return array(
'type' => 'error',
'note' => $e->getMessage()
);
}
return true;
}, 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment