Skip to content

Instantly share code, notes, and snippets.

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