A client of mine approached me regarding a problem with Authorize.net. Payments where the address contained Chinese characters failed.
The idea was to replace the address with a “-” on checkout if it contains Chinese characters, then process the payment and replace the “-” with the Chinese characters again after payment was finished.
The solution looked as follows:
Chinese character detection + replacement
Using the woocommerce_before_checkout_process action, a function is called where billing company, address1, address2 and city are scanned for Chinese characters. This is done as follows:
preg_match( '/[\x{4e00}-\x{9fa5}]/u', $str )
If true, the value is set to “-” in $_POST while the old values are added to the session.
Furthermore, if the shipping address was the same as the billing address, the shipping address got set using the original billing address.
This will allow the payment to be processed.
Emails
Something I didn’t think about immediately were the emails that get sent out. The solution there was to use the woocommerce_mail_content filter.
If the session indicates that the billing address was changed, the email will only show the shipping address (which is the original address containing Chinese characters).
That fixed this problem.
Fixing the mess
Unfortunately, replacing values with “-” will also cause quite a mess in WooCommerce. As a result, if values got added to the session.
If things were replaced with “-“, they have to be reset to the original values for:
- The billing address in the newly created order
- The billing address stored for the given user
This function was called by 3 actions:
- woocommerce_payment_complete
- woocommerce_order_status_failed
- woocommerce_order_status_on-hold
Everything was tested on a test server using manual payments first before uploading it to the live server.
After some iterations, the code worked as discussed and the client could start accept payments with addresses containing Chinese characters.