Skip to content

Commit 995955d

Browse files
authored
Merge pull request #223 from saucal/fix/japanese-address-format-issue
Improve detection japanese region
2 parents b084456 + 7d83a08 commit 995955d

9 files changed

+194
-106
lines changed

includes/class-wc-amazon-payments-advanced-api-abstract.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ public static function format_name( $name ) {
390390
);
391391
}
392392
// Use fallback value for the last name to avoid field required errors.
393-
$last_name_fallback = '.';
394-
$names = explode( ' ', $name );
393+
$last_name_fallback = '';
394+
$names = preg_split( '/ | /', $name );
395395
return array(
396396
'first_name' => array_shift( $names ),
397397
'last_name' => empty( $names ) ? $last_name_fallback : implode( ' ', $names ),
@@ -477,11 +477,13 @@ public static function format_address( $address ) {
477477

478478
}
479479

480-
$formatted['phone'] = isset( $address->Phone ) ? (string) $address->Phone : null;
480+
// Prevent invalid characters in phone number.
481+
$formatted['phone'] = isset( $address->Phone ) ? (string) filter_var($address->Phone, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH) : null;
482+
481483
$formatted['city'] = isset( $address->City ) ? (string) $address->City : null;
482484
if ( ! empty( $address->CountryCode ) && in_array( $address->CountryCode, array( 'JP' ) ) ) {
483485
if ( empty( $formatted['city'] ) ) {
484-
$formatted['city'] = ''; // Force empty city
486+
$formatted['city'] = ''; // Force empty city
485487
}
486488
}
487489
$formatted['postcode'] = isset( $address->PostalCode ) ? (string) $address->PostalCode : null;
@@ -496,6 +498,8 @@ public static function format_address( $address ) {
496498
$valid_state_values = array_map( 'wc_strtoupper', array_flip( array_map( 'wc_strtoupper', $valid_states ) ) );
497499
$uc_state = wc_strtoupper( $formatted['state'] );
498500

501+
$uc_state = WC_Gateway_Amazon_Payments_Advanced::maybe_get_jp_region_code( $uc_state );
502+
499503
if ( isset( $valid_state_values[ $uc_state ] ) ) {
500504
// With this part we consider state value to be valid as well, convert it to the state key for the valid_states check below.
501505
$uc_state = $valid_state_values[ $uc_state ];

includes/class-wc-amazon-payments-advanced-api.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,27 @@ public static function create_checkout_session_classic_params( $redirect_url = n
472472
* @return array
473473
*/
474474
public static function get_create_checkout_session_config( $redirect_url = null ) {
475+
static $config = array();
476+
477+
$cached_config = empty( $redirect_url ) ? $redirect_url : 'default';
478+
479+
if ( ! empty( $config[ $cached_config ] ) && ! is_wp_error( $config[ $cached_config ] ) ) {
480+
return $config[ $cached_config ];
481+
}
482+
475483
$settings = self::get_settings();
476484
$client = self::get_client();
477485
$payload = self::create_checkout_session_params( $redirect_url );
478486

479487
$signature = $client->generateButtonSignature( $payload );
480-
return array(
488+
489+
$config[ $cached_config ] = array(
481490
'publicKeyId' => $settings['public_key_id'],
482491
'payloadJSON' => $payload,
483492
'signature' => $signature,
484493
);
494+
495+
return $config[ $cached_config ];
485496
}
486497

487498
/**

includes/class-wc-amazon-payments-advanced-utils.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,51 @@ public static function get_edit_order_screen_id() {
2626
? wc_get_page_screen_id( 'shop-order' )
2727
: 'shop_order';
2828
}
29+
30+
31+
/**
32+
* Get non required fields.
33+
*
34+
* @return array
35+
*/
36+
public static function get_non_required_fields() {
37+
38+
$non_required_fields = array(
39+
'billing_last_name',
40+
'billing_state',
41+
'billing_phone',
42+
'shipping_last_name',
43+
'shipping_state',
44+
);
45+
46+
return apply_filters( 'woocommerce_amazon_pa_non_required_fields', $non_required_fields );
47+
}
48+
49+
50+
/**
51+
* Get non required fields per country.
52+
*
53+
* @return array
54+
*/
55+
public static function get_non_required_fields_per_country() {
56+
57+
$mapped_fields_per_country = array(
58+
'JP' => array(
59+
'city',
60+
),
61+
);
62+
63+
$non_required_fields = array();
64+
65+
foreach ( $mapped_fields_per_country as $country => $fields ) {
66+
foreach ( $fields as $field ) {
67+
$non_required_fields[ $country ][] = 'billing_' . $field;
68+
$non_required_fields[ $country ][] = 'billing-' . $field;
69+
$non_required_fields[ $country ][] = 'shipping_' . $field;
70+
$non_required_fields[ $country ][] = 'shipping-' . $field;
71+
}
72+
}
73+
74+
return apply_filters( 'woocommerce_amazon_pa_non_required_fields_per_country', $non_required_fields, $mapped_fields_per_country );
75+
}
2976
}

includes/class-wc-gateway-amazon-payments-advanced-abstract.php

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,53 @@ abstract class WC_Gateway_Amazon_Payments_Advanced_Abstract extends WC_Payment_G
1818
* Specifically requested by Amazon's JP Team.
1919
*/
2020
const JP_REGION_CODE_MAP = array(
21-
'JP01' => '北海道',
22-
'JP02' => '青森県',
23-
'JP03' => '岩手県',
24-
'JP04' => '宮城県',
25-
'JP05' => '秋田県',
26-
'JP06' => '山形県',
27-
'JP07' => '福島県',
28-
'JP08' => '茨城県',
29-
'JP09' => '栃木県',
30-
'JP10' => '群馬県',
31-
'JP11' => '埼玉県',
32-
'JP12' => '千葉県',
33-
'JP13' => '東京都',
34-
'JP14' => '神奈川県',
35-
'JP15' => '新潟県',
36-
'JP16' => '富山県',
37-
'JP17' => '石川県',
38-
'JP18' => '福井県',
39-
'JP19' => '山梨県',
40-
'JP20' => '長野県',
41-
'JP21' => '岐阜県',
42-
'JP22' => '静岡県',
43-
'JP23' => '愛知県',
44-
'JP24' => '三重県',
45-
'JP25' => '滋賀県',
46-
'JP26' => '京都府',
47-
'JP27' => '大阪府',
48-
'JP28' => '兵庫県',
49-
'JP29' => '奈良県',
50-
'JP30' => '和歌山県',
51-
'JP31' => '鳥取県',
52-
'JP32' => '島根県',
53-
'JP33' => '岡山県',
54-
'JP34' => '広島県',
55-
'JP35' => '山口県',
56-
'JP36' => '徳島県',
57-
'JP37' => '香川県',
58-
'JP38' => '愛媛県',
59-
'JP39' => '高知県',
60-
'JP40' => '福岡県',
61-
'JP41' => '佐賀県',
62-
'JP42' => '長崎県',
63-
'JP43' => '熊本県',
64-
'JP44' => '大分県',
65-
'JP45' => '宮崎県',
66-
'JP46' => '鹿児島県',
67-
'JP47' => '沖縄県',
21+
'JP01' => array( 'Hokkaido', 'Hokkaidō', '北海道' ),
22+
'JP02' => array( 'Aomori', 'Aomori-ken', '青森県' ),
23+
'JP03' => array( 'Iwate', 'Iwate-ken', '岩手県' ),
24+
'JP04' => array( 'Miyagi', 'Miyagi-ken', '宮城県' ),
25+
'JP05' => array( 'Akita', 'Akita-ken', '秋田県' ),
26+
'JP06' => array( 'Yamagata', 'Yamagata-ken', '山形県' ),
27+
'JP07' => array( 'Fukushima', 'Fukushima-ken', '福島県' ),
28+
'JP08' => array( 'Ibaraki', 'Ibaraki-ken', '茨城県' ),
29+
'JP09' => array( 'Tochigi', 'Tochigi-ken', '栃木県' ),
30+
'JP10' => array( 'Gunma', 'Gunma-ken', '群馬県' ),
31+
'JP11' => array( 'Saitama', 'Saitama-ken', '埼玉県' ),
32+
'JP12' => array( 'Chiba', 'Chiba-ken', '千葉県' ),
33+
'JP13' => array( 'Tokyo', 'Tōkyō-to', '東京都' ),
34+
'JP14' => array( 'Kanagawa', 'Kanagawa-ken', '神奈川県' ),
35+
'JP15' => array( 'Niigata', 'Niigata-ken', '新潟県' ),
36+
'JP16' => array( 'Toyama', 'Toyama-ken', '富山県' ),
37+
'JP17' => array( 'Ishikawa', 'Ishikawa-ken', '石川県' ),
38+
'JP18' => array( 'Fukui', 'Fukui-ken', '福井県' ),
39+
'JP19' => array( 'Yamanashi', 'Yamanashi-ken', '山梨県' ),
40+
'JP20' => array( 'Nagano', 'Nagano-ken', '長野県' ),
41+
'JP21' => array( 'Gifu', 'Gifu-ken', '岐阜県' ),
42+
'JP22' => array( 'Shizuoka', 'Shizuoka-ken', '静岡県' ),
43+
'JP23' => array( 'Aichi', 'Aichi-ken', '愛知県' ),
44+
'JP24' => array( 'Mie', 'Mie-ken', '三重県' ),
45+
'JP25' => array( 'Shiga', 'Shiga-ken', '滋賀県' ),
46+
'JP26' => array( 'Kyoto', 'Kyōto-fu', '京都府' ),
47+
'JP27' => array( 'Osaka', 'Ōsaka-fu', '大阪府' ),
48+
'JP28' => array( 'Hyogo', 'Hyōgo-ken', '兵庫県' ),
49+
'JP29' => array( 'Nara', 'Nara-ken', '奈良県' ),
50+
'JP30' => array( 'Wakayama', 'Wakayama-ken', '和歌山県' ),
51+
'JP31' => array( 'Tottori', 'Tottori-ken', '鳥取県' ),
52+
'JP32' => array( 'Shimane', 'Shimane-ken', '島根県' ),
53+
'JP33' => array( 'Okayama', 'Okayama-ken', '岡山県' ),
54+
'JP34' => array( 'Hiroshima', 'Hiroshima-ken', '広島県' ),
55+
'JP35' => array( 'Yamaguchi', 'Yamaguchi-ken', '山口県' ),
56+
'JP36' => array( 'Tokushima', 'Tokushima-ken', '徳島県' ),
57+
'JP37' => array( 'Kagawa', 'Kagawa-ken', '香川県' ),
58+
'JP38' => array( 'Ehime', 'Ehime-ken', '愛媛県' ),
59+
'JP39' => array( 'Kochi', 'Kōchi-ken', '高知県' ),
60+
'JP40' => array( 'Fukuoka', 'Fukuoka-ken', '福岡県' ),
61+
'JP41' => array( 'Saga', 'Saga-ken', '佐賀県' ),
62+
'JP42' => array( 'Nagasaki', 'Nagasaki-ken', '長崎県' ),
63+
'JP43' => array( 'Kumamoto', 'Kumamoto-ken', '熊本県' ),
64+
'JP44' => array( 'Oita', 'Ōita-ken', '大分県' ),
65+
'JP45' => array( 'Miyazaki', 'Miyazaki-ken', '宮崎県' ),
66+
'JP46' => array( 'Kagoshima', 'Kagoshima-ken', '鹿児島県' ),
67+
'JP47' => array( 'Okinawa', 'Okinawa-ken', '沖縄県' ),
6868
);
6969

7070
/**
@@ -943,7 +943,7 @@ public function checkout_button( $echo = true, $elem = 'div', $id = 'pay_with_am
943943
$subscriptions_enabled = empty( $this->settings['subscriptions_enabled'] ) || 'yes' === $this->settings['subscriptions_enabled'];
944944
$cart_contains_sub = class_exists( 'WC_Subscriptions_Cart' ) ? WC_Subscriptions_Cart::cart_contains_subscription() : false;
945945

946-
if ( $subscriptions_installed && ! $subscriptions_enabled && $cart_contains_sub ) {
946+
if ( $subscriptions_installed && ! $subscriptions_enabled && $cart_contains_sub && ! is_product() ) {
947947
return;
948948
}
949949

@@ -1020,30 +1020,21 @@ public function remove_gateways( $gateways ) {
10201020
return $gateways;
10211021
}
10221022

1023-
/**
1024-
* Override billing fields when checking out with Amazon.
1025-
*
1026-
* @param array $fields billing fields.
1027-
*/
1028-
public function override_billing_fields( $fields ) {
1029-
// Last name and State are not required on Amazon billing addrress forms.
1030-
$fields['billing_last_name']['required'] = false;
1031-
$fields['billing_state']['required'] = false;
1032-
// Phone field is missing on some sandbox accounts.
1033-
$fields['billing_phone']['required'] = false;
1034-
1035-
return $fields;
1036-
}
10371023

10381024
/**
1039-
* Override address fields when checking out with Amazon.
1025+
* Override checkout fields when checking out with Amazon.
10401026
*
10411027
* @param array $fields default address fields.
10421028
*/
1043-
public function override_shipping_fields( $fields ) {
1044-
// Last name and State are not required on Amazon shipping address forms.
1045-
$fields['shipping_last_name']['required'] = false;
1046-
$fields['shipping_state']['required'] = false;
1029+
public function override_checkout_fields( $fields ) {
1030+
1031+
$non_required_fields = WC_Amazon_Payments_Advanced_Utils::get_non_required_fields();
1032+
1033+
foreach ( $fields as $key => $field ) {
1034+
if ( ! in_array( $key, $non_required_fields, true ) ) {
1035+
$fields[ $key ]['required'] = false;
1036+
}
1037+
}
10471038

10481039
return $fields;
10491040
}
@@ -1055,8 +1046,8 @@ public function checkout_init_common() {
10551046
// Remove other gateways after being logged in.
10561047
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'remove_gateways' ) );
10571048
// Some fields are not enforced on Amazon's side. Marking them as optional avoids issues with checkout.
1058-
add_filter( 'woocommerce_billing_fields', array( $this, 'override_billing_fields' ) );
1059-
add_filter( 'woocommerce_shipping_fields', array( $this, 'override_shipping_fields' ) );
1049+
add_filter( 'woocommerce_billing_fields', array( $this, 'override_checkout_fields' ) );
1050+
add_filter( 'woocommerce_shipping_fields', array( $this, 'override_checkout_fields' ) );
10601051
// Always ship to different address.
10611052
add_action( 'woocommerce_ship_to_different_address_checked', '__return_true' );
10621053
}

includes/class-wc-gateway-amazon-payments-advanced-subscriptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public function __construct() {
1919

2020
add_filter( 'woocommerce_amazon_pa_form_fields_before_legacy', array( $this, 'add_enable_subscriptions_field' ) );
2121

22+
add_filter( 'woocommerce_amazon_pa_supports', array( $this, 'add_subscription_support' ) );
23+
2224
if ( 'yes' !== WC_Amazon_Payments_Advanced_API::get_settings( 'subscriptions_enabled' ) ) {
2325
return;
2426
}
2527

2628
add_action( 'wp_loaded', array( $this, 'init_handlers' ), 12 );
2729

28-
add_filter( 'woocommerce_amazon_pa_supports', array( $this, 'add_subscription_support' ) );
29-
3030
// WC Subscription Hook.
3131
add_filter( 'woocommerce_subscriptions_process_payment_for_change_method_via_pay_shortcode', array( $this, 'filter_payment_method_changed_result' ), 10, 2 );
3232
}

includes/class-wc-gateway-amazon-payments-advanced.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ protected function get_availability() {
111111
return false;
112112
}
113113

114+
if ( ! $this->possible_subscription_cart_supported() ) {
115+
return false;
116+
}
117+
114118
return true;
115119
}
116120

@@ -293,6 +297,7 @@ protected function get_js_params() {
293297
'button_language' => $this->settings['button_language'],
294298
'ledger_currency' => $this->get_ledger_currency(),
295299
'estimated_order_amount' => self::get_estimated_order_amount(),
300+
'overriden_fields_per_country' => WC_Amazon_Payments_Advanced_Utils::get_non_required_fields_per_country(),
296301
),
297302
$params
298303
);
@@ -1051,10 +1056,12 @@ public function display_payment_method_selected( $checkout_session = null ) {
10511056
* session object.
10521057
*
10531058
* @param object $checkout_session The active checkout session.
1059+
* @param bool $allow_null Whether to bypass creating a new session if it's not available.
1060+
*
10541061
* @return boolean
10551062
*/
1056-
public function has_payment_preferences( $checkout_session = null ) {
1057-
if ( null === $checkout_session ) {
1063+
public function has_payment_preferences( $checkout_session = null, $allow_null = false ) {
1064+
if ( ! $allow_null && null === $checkout_session ) {
10581065
$checkout_session = $this->get_checkout_session();
10591066
}
10601067

@@ -1069,10 +1076,11 @@ public function has_payment_preferences( $checkout_session = null ) {
10691076
* Returns the selected payment method from Amazon.
10701077
*
10711078
* @param object $checkout_session The active checkout session.
1079+
* @param bool $allow_null Whether to bypass creating a new session if it's not available.
10721080
* @return string
10731081
*/
1074-
public function get_selected_payment_label( $checkout_session = null ) {
1075-
if ( null === $checkout_session ) {
1082+
public function get_selected_payment_label( $checkout_session = null, $allow_null = false ) {
1083+
if ( ! $allow_null && null === $checkout_session ) {
10761084
$checkout_session = $this->get_checkout_session();
10771085
}
10781086

@@ -3031,8 +3039,8 @@ public function update_address_details_for_classic( $payload, $checkout_session_
30313039
$shipping_state = $order->get_shipping_state();
30323040
$shipping_country = $order->get_shipping_country( 'edit' );
30333041

3034-
if ( 'JP' === strtoupper( $shipping_country ) && 'JP' === strtoupper( WC_Amazon_Payments_Advanced_API::get_region() ) && isset( self::JP_REGION_CODE_MAP[ $shipping_state ] ) ) {
3035-
$shipping_state = self::JP_REGION_CODE_MAP[ $shipping_state ];
3042+
if ( 'JP' === strtoupper( $shipping_country ) && 'JP' === strtoupper( WC_Amazon_Payments_Advanced_API::get_region() ) ) {
3043+
$shipping_state = $this->maybe_get_jp_region_code( $shipping_state );
30363044
}
30373045

30383046
$payload['addressDetails'] = array(
@@ -3060,6 +3068,32 @@ public function update_address_details_for_classic( $payload, $checkout_session_
30603068
return $payload;
30613069
}
30623070

3071+
/**
3072+
* Maybe get JP region code.
3073+
*
3074+
* @param string $shipping_state The shipping state.
3075+
*
3076+
* @return string
3077+
*/
3078+
public static function maybe_get_jp_region_code( $shipping_state ) {
3079+
3080+
static $formatted_states;
3081+
3082+
if ( isset( $formatted_states[ $shipping_state ] ) ) {
3083+
return $formatted_states[ $shipping_state ];
3084+
}
3085+
3086+
$formatted_states[ $shipping_state ] = $shipping_state;
3087+
3088+
foreach ( self::JP_REGION_CODE_MAP as $region_code => $region ) {
3089+
if ( is_array( $region ) && in_array( $shipping_state, $region, true ) ) {
3090+
$formatted_states[ $shipping_state ] = $region_code;
3091+
}
3092+
}
3093+
3094+
return $formatted_states[ $shipping_state ];
3095+
}
3096+
30633097
/**
30643098
* Get the estimated order amount from the cart totals.
30653099
*

0 commit comments

Comments
 (0)