Created
February 16, 2023 19:56
-
-
Save idokd/1c0616c84ce922989db7dd6222dc4fc1 to your computer and use it in GitHub Desktop.
Woocommerce Multiple Stripe Accounts By Country
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "key": "group_63ee7c57739d9", | |
| "title": "Stripe Accounts", | |
| "fields": [ | |
| { | |
| "key": "field_63ee7c579826f", | |
| "label": "Stripe Accounts", | |
| "name": "stripe_accounts", | |
| "aria-label": "", | |
| "type": "repeater", | |
| "instructions": "Override the Stripe Account to use for different countries, use the Woocommerce Stripe settings for the default ( for most countries ), and define here a row for each country with its specific Stripe account settings to use. That will be matched to the order billing country.", | |
| "required": 0, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "acfe_repeater_stylised_button": 1, | |
| "layout": "block", | |
| "pagination": 0, | |
| "min": 0, | |
| "max": 0, | |
| "collapsed": "field_63ee7c57a98ab", | |
| "button_label": "Add Account", | |
| "rows_per_page": 20, | |
| "sub_fields": [ | |
| { | |
| "key": "field_63ee7c57a983a", | |
| "label": "Account ID", | |
| "name": "account_id", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee7c57a98ab", | |
| "label": "Account Name", | |
| "name": "account_name", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee7c57a98f2", | |
| "label": "Publishable Key", | |
| "name": "publishable_key_live", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee7c57a9957", | |
| "label": "Secret Key", | |
| "name": "secret_key_live", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee8020b0886", | |
| "label": "Publishable Key Test", | |
| "name": "publishable_key_test", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee800db0885", | |
| "label": "Secret Key Test", | |
| "name": "secret_key_test", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee7c57a99b9", | |
| "label": "Country", | |
| "name": "country", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "Country code, such as: US, GB, IL", | |
| "required": 1, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| }, | |
| { | |
| "key": "field_63ee7c57a9a03", | |
| "label": "Countries", | |
| "name": "countries", | |
| "aria-label": "", | |
| "type": "text", | |
| "instructions": "Destination Countries (seperated with comma: , ) For example: FR,IT,SP", | |
| "required": 0, | |
| "conditional_logic": 0, | |
| "wrapper": { | |
| "width": "", | |
| "class": "", | |
| "id": "" | |
| }, | |
| "wpml_cf_preferences": 1, | |
| "default_value": "", | |
| "maxlength": "", | |
| "placeholder": "", | |
| "prepend": "", | |
| "append": "", | |
| "parent_repeater": "field_63ee7c579826f" | |
| } | |
| ] | |
| } | |
| ], | |
| "location": [ | |
| [ | |
| { | |
| "param": "options_page", | |
| "operator": "==", | |
| "value": "acf-options-taxes-override" | |
| } | |
| ] | |
| ], | |
| "menu_order": 0, | |
| "position": "normal", | |
| "style": "default", | |
| "label_placement": "top", | |
| "instruction_placement": "label", | |
| "hide_on_screen": "", | |
| "active": true, | |
| "description": "", | |
| "show_in_rest": 0, | |
| "acfe_display_title": "", | |
| "acfe_autosync": [ | |
| "php", | |
| "json" | |
| ], | |
| "acfe_form": 0, | |
| "acfe_meta": "", | |
| "acfe_note": "" | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // That was tested only on simple transactions, yet to be tested on subscriptions or others | |
| $stripe_accounts = [ | |
| 'live' => [ | |
| 'JP' => 'pk_live_....', | |
| 'US' => 'pk_live_....', | |
| 'XX' => 'pk_live_....' | |
| ], | |
| 'test' => [ | |
| 'JP' => 'pk_test_....', | |
| 'US' => 'pk_test_....', | |
| 'XX' => 'pk_test_....' | |
| ] | |
| ]; | |
| $stripe_accounts_secerts = [ | |
| 'live' => [ | |
| 'JP' => 'sk_live_....', | |
| 'US' => 'sk_live_....', | |
| 'XX' => 'sk_live_....' | |
| ], | |
| 'test' => [ | |
| 'JP' => 'sk_test_....', | |
| 'US' => 'sk_test_....', | |
| 'XX' => 'sk_test_....' | |
| ] | |
| ]; | |
| add_filter( 'wc_stripe_accounts', function( $stripe_accounts = [], $mode = 'live' ) { | |
| $accounts = get_field( 'stripe_accounts', 'options' ); | |
| foreach ( $accounts as $account ) { | |
| $country = $account[ 'country' ] ? : 'XX'; | |
| if ( $mode ) { | |
| $stripe_accounts[ $country ] = $mode == 'live' ? $account[ 'publishable_key_live' ] : $account[ 'publishable_key_test' ]; | |
| } else { | |
| $stripe_accounts[ 'live' ][ $country ] = $account[ 'publishable_key_live' ]; | |
| $stripe_accounts[ 'test' ][ $country ] = $account[ 'publishable_key_test' ]; | |
| } | |
| } | |
| return( $stripe_accounts ); | |
| }, 50 , 2 ); | |
| add_filter( 'wc_stripe_accounts_secrets', function( $stripe_accounts, $mode = 'live' ) { | |
| $accounts = get_field( 'stripe_accounts', 'options' ); | |
| $stripe_accounts = []; | |
| foreach ( $accounts as $account ) { | |
| $country = $account[ 'country' ] ? : 'XX'; | |
| if ( $mode ) { | |
| $stripe_accounts[ $country ] = $mode == 'live' ? $account[ 'secret_key_live' ] : $account[ 'secret_key_test' ]; | |
| } else { | |
| $stripe_accounts[ 'live' ][ $country ] = $account[ 'secret_key_live' ]; | |
| $stripe_accounts[ 'test' ][ $country ] = $account[ 'secret_key_test' ]; | |
| } | |
| } | |
| return( $stripe_accounts ); | |
| }, 50 , 2 ); | |
| add_filter( 'wc_stripe_params', 'stripe_conditional_publishable_key', 9999 ); | |
| function stripe_conditional_publishable_key( $params ) { | |
| global $stripe_accounts; | |
| $mode = strpos( $params[ 'key' ], 'live' ) > 0 ? 'live' : 'test'; | |
| $accounts = apply_filters( 'wc_stripe_accounts', array_merge([ 'XX' => $params[ 'key' ] ], $stripe_accounts[ $mode ] ), $mode ); | |
| $country = stripe_order_country_in_cart(); | |
| if ( $country && isset( $accounts[ $country ] ) ) $params[ 'key' ] = $accounts[ $country ]; | |
| $params[ 'accounts' ] = $accounts; | |
| return( $params ); | |
| } | |
| add_filter( 'wc_stripe_payment_request_params', 'stripe_conditional_publishable_key_request', 9999 ); | |
| function stripe_conditional_publishable_key_request( $params ) { | |
| global $stripe_accounts; | |
| $mode = strpos( $params[ 'stripe' ][ 'key' ], 'live' ) > 0 ? 'live' : 'test'; | |
| $accounts = apply_filters( 'wc_stripe_accounts', array_merge( [ 'XX' => $params[ 'stripe' ][ 'key' ] ], $stripe_accounts[ $mode ] ), $mode ); | |
| $country = stripe_order_country_in_cart(); | |
| if ( $country && isset( $accounts[ $country ] ) ) $params[ 'stripe' ][ 'key' ] = $accounts[ $country ]; | |
| $params[ 'stripe'][ 'accounts' ] = $accounts; | |
| return( $params ); | |
| } | |
| add_filter( 'woocommerce_stripe_request_headers', 'stripe_conditional_private_key_headers', 9999 ); | |
| function stripe_conditional_private_key_headers( $headers ) { | |
| global $stripe_accounts_secerts; | |
| $secret = base64_decode( str_replace( 'Basic ', '', $headers[ 'Authorization' ] ) ); | |
| $mode = strpos( $secret, 'live' ) > 0 ? 'live' : 'test'; | |
| $secrets = apply_filters( 'wc_stripe_accounts_secrets', array_merge( [ 'XX' => $secret ], $stripe_accounts_secerts[ $mode ] ), $mode ); | |
| $country = stripe_order_country_in_cart(); | |
| if ( $country && isset( $secrets[ $country ] ) ) $headers[ 'Authorization' ] = 'Basic ' . base64_encode( $secrets[ $country ] ); | |
| return( $headers ); | |
| } | |
| add_action( 'woocommerce_before_checkout_form', 'stripe_conditional_woocommerce_checkout_payment', 9999 ); | |
| function stripe_conditional_woocommerce_checkout_payment() { | |
| ?> | |
| <script> | |
| jQuery( document ).ready( function() { | |
| jQuery( 'select#billing_country' ).on( 'change', function() { | |
| var country = jQuery( '#billing_country :selected' ).val(); | |
| wc_stripe_payment_request_params[ 'stripe' ][ 'key' ] = wc_stripe_payment_request_params[ 'stripe' ][ 'accounts' ][ country in wc_stripe_payment_request_params[ 'stripe' ][ 'accounts' ] ? country : 'XX' ]; | |
| wc_stripe_params[ 'key' ] = wc_stripe_params[ 'accounts' ][ country in wc_stripe_params[ 'accounts' ] ? country : 'XX' ]; | |
| } ); | |
| } ); | |
| </script> | |
| <?php | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will enable your WooCommerce Stripe Gateway plugin to use multiple stripe accounts so based on country you can define the Stripe Account to use.
you can hard code the $stripe_accounts & $stripe_accounts_secerts arrays, or load the ACF options Field Group to have your simple simple interface.
The default plugin settings will be used to all countries that are not 'overriden' by this code/settings.