diff --git a/composer.json b/composer.json index 80892e9..0500a73 100644 --- a/composer.json +++ b/composer.json @@ -61,11 +61,12 @@ "php": ">=8.1", "ext-json": "*", "automattic/jetpack-autoloader": "^3.0", + "giggsey/libphonenumber-for-php": "^8.13", "justinrainbow/json-schema": "^5.2", "pronamic/wp-http": "^1.2", "pronamic/wp-mollie": "^1.7", "woocommerce/action-scheduler": "^3.8", - "wp-pay/core": "^4.18" + "wp-pay/core": "^4.23" }, "require-dev": { "overtrue/phplint": "^9.0", diff --git a/src/AddressTransformer.php b/src/AddressTransformer.php index 625e29e..897e755 100644 --- a/src/AddressTransformer.php +++ b/src/AddressTransformer.php @@ -10,6 +10,8 @@ namespace Pronamic\WordPress\Pay\Gateways\Mollie; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; use InvalidArgumentException; use Pronamic\WordPress\Mollie\Address as MollieAddress; use Pronamic\WordPress\Pay\Address as WordPressAddress; @@ -61,8 +63,18 @@ public function transform_wp_to_mollie( WordPressAddress $address ): MollieAddre $mollie_address = new MollieAddress( $given_name, $family_name, $email, $street_and_number, $city, $country ); + $phone = $address->get_phone(); + + if ( null !== $phone ) { + $phone_util = PhoneNumberUtil::getInstance(); + + $phone_number_object = $phone_util->parse( $phone, $country ); + + $phone = $phone_util->format( $phone_number_object, PhoneNumberFormat::E164 ); + } + $mollie_address->organization_name = $address->get_company_name(); - $mollie_address->phone = $address->get_phone(); + $mollie_address->phone = $phone; $mollie_address->street_additional = $address->get_line_2(); $mollie_address->postal_code = $address->get_postal_code(); $mollie_address->region = $address->get_region(); diff --git a/tests/src/AddressTransformerTest.php b/tests/src/AddressTransformerTest.php new file mode 100644 index 0000000..ee7b454 --- /dev/null +++ b/tests/src/AddressTransformerTest.php @@ -0,0 +1,65 @@ + + * @copyright 2005-2024 Pronamic + * @license GPL-3.0-or-later + * @package Pronamic\WordPress\Pay + */ + +namespace Pronamic\WordPress\Pay\Gateways\Mollie; + +use Pronamic\WordPress\Mollie\Address as MollieAddress; +use Pronamic\WordPress\Pay\Address as PronamicAddress; +use Pronamic\WordPress\Pay\ContactName; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; + +/** + * Address transformer test class + */ +class AddressTransformerTest extends TestCase { + /** + * Test transform. + * + * @param string $phone Phone number. + * @param string $phone_e164 Phone number E164. + * + * @dataProvider transform_provider + */ + public function test_transform( $phone, $phone_e164 ) { + $name = new ContactName(); + + $name->set_first_name( 'John' ); + $name->set_last_name( 'Doe' ); + + $pronamic_address = new PronamicAddress(); + + $pronamic_address->set_name( $name ); + $pronamic_address->set_email( 'john.doe@example.com' ); + $pronamic_address->set_phone( $phone ); + $pronamic_address->set_line_1( 'Kleine Kerkstraat 1' ); + $pronamic_address->set_city( 'Leeuwarden' ); + $pronamic_address->set_country_code( 'NL' ); + + $transformer = new AddressTransformer(); + + $mollie_address = $transformer->transform_wp_to_mollie( $pronamic_address ); + + $this->assertEquals( $phone_e164, $mollie_address->phone ); + } + + /** + * Transform provider. + * + * @return array + */ + public function transform_provider() { + return [ + [ '1234567890', '+311234567890' ], + [ '12 34 56 78 90', '+311234567890' ], + [ '+321234567890', '+321234567890' ], + [ '+491234567890', '+491234567890' ], + ]; + } +}