6.11.Gateway - Stripe
Filters/Codesnippets WPPizza -> Gateways -> Stripe
Codesnippet(s) to modify “Stripe” gateway options – namely test/live api keys in this example – depending on the user selection of a custom added dropdown formfield, in this case allowing for different stripe accounts to be targeted depending on “Shop Branches” selection by user (requires WPPizza Stripe v4.1+)
A) Register a custom “Shop Branches” dropdown formfield as per “Register Formfields” filter
@param: array
@return: array
/*
register a dropdown field containing shop branches
*/
add_filter('wppizza_register_formfields', 'myprefix_branches_dropdown');
function myprefix_branches_dropdown($formfields){
/*
*unique* identifier for the formfield you want to add
*/
$unique_ident = 'shop_branch';
/***********************************************
adding a select dropdown
***********************************************/
$formfields[$unique_ident] = array(
/* sort order - at which position is this field displayed */
'sort' => 10,
/* the unique ident */
'key' => $unique_ident ,
/* label displayed for the formfield */
'lbl' => 'some label',
/* an array of available dropdown values*/
'value' => array('Branch 1','Branch 2','Branch 3','Branch N'),
/* set to select to display a select dropdown*/
'type' => 'select',
/* true to enable (pointless to set this to false)*/
'enabled' => true,
/* true to make the field required on delivery, false otherwise */
'required' => true,
/* true to make the field required on pickup, false otherwise */
'required_on_pickup' => true,
/* true to prefill with a value if it is known (will only ever do anything with 'onregister' being true as well) */
'prefill' => false,
/* true to make field part of the user registration values */
'onregister' => false,
/* true to add the value entered to the email subject */
'add_to_subject_line' => false,
/* placeholder - used as initial, non-selected value or bool false / empty */
'placeholder' => '--Please Select--',
/* validation as available wppizza->order form. as there can be multiple validation rules, this should be an array,
however as this is a select field , the rules as set below are really the only sensible setting here
*/
'validation' => array(
'default' => true,
),
);
return $formfields;
}
B) Set session data – via ajax – on change of dropdown value
/*
adding javascript to footer of checkout page using 'wp_footer' action
- updates (session) user data - via ajax - on change of dropdown field
- results in reload of page which subsequently applies filtered options
as per 'wppizza_gateway_stripe_filter_options' below
*/
add_action('wp_footer', 'myprefix_on_branches_change');
function myprefix_on_branches_change(){
/*
same *unique* identifier set for the formfield
*/
$unique_ident = 'shop_branch';
if(wppizza_is_checkout()){
echo"<script>
jQuery(document).ready(function($){
$(document).on('change', '#".$unique_ident."', function(e){
//add loading div
$('html').css({'position':'relative'});/*stretch html to make loading cover whole page */
$('body').prepend('<div class=\"wppizza-loading\"></div>');
$('.wppizza-ordernow').attr('disabled', 'true');/* disable send order button */
jQuery.post(wppizza.ajaxurl , {action :'wppizza_json', vars:{'type':'update_userdata', 'data': $('#wppizza-send-order').serialize()}}, function(obj) {
window.location.reload(true);
return;
},'json');
});
});
</script>";
}
}
C) Apply live/test Api keys depending on “Branch” selected
@param: array
@param: array
@param: str
@return: array
/*
apply test/live keys
depending on branch selected
Note: the 'filter_ident' parameter (identifies which function calls the filter) is only referenced here for completeness sake and will not be required in 99.9% of cases
*/
add_filter('wppizza_gateway_stripe_filter_options', 'myprefix_filter_stripe_options', 10, 3);
function myprefix_filter_stripe_options($gateway_options, $order_details, $filter_ident){
/*
same *unique* identifier set for the formfield
*/
$unique_ident = 'shop_branch';
/*
values branch 1
*/
if($order_details['customer'][$unique_ident]['value'] == 'Branch 1' ){
/*
set Stripe API keys as required for Branch 1
edit as required
*/
$gateway_options['LiveSecretKey'] = 'sk_live_someapikeybranch_1';
$gateway_options['LivePublishableKey'] = 'pk_live_someapikeybranch_1';
$gateway_options['TestSecretKey'] = 'sk_test_someapikeybranch_1';
$gateway_options['TestPublishableKey'] = 'pk_test_someapikeybranch_1';
$gateway_options['WebhookLiveKey'] = 'whsec_someapikeybranch_1';
$gateway_options['WebhookTestKey'] = 'whsec_someapikeybranch_1';
}
/*
values branch 2
*/
elseif($order_details['customer'][$unique_ident]['value'] == 'Branch 2' ){
/*
set Stripe API keys as required for Branch 2
edit as required
*/
$gateway_options['LiveSecretKey'] = 'sk_live_someapikeybranch_2';
$gateway_options['LivePublishableKey'] = 'pk_live_someapikeybranch_2';
$gateway_options['TestSecretKey'] = 'sk_test_someapikeybranch_2';
$gateway_options['TestPublishableKey'] = 'pk_test_someapikeybranch_2';
$gateway_options['WebhookLiveKey'] = 'whsec_someapikeybranch_2';
$gateway_options['WebhookTestKey'] = 'whsec_someapikeybranch_2';
}
/*
... etc etc ....
*/
return $gateway_options;
}
documentor id 5