From 170252dbfb0412cb63eaa84f2a2eaff15c624507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reu=CC=88el=20van=20der=20Steege?= Date: Fri, 18 Oct 2024 10:25:03 +0200 Subject: [PATCH 1/5] Format phone number in E164 format. --- composer.json | 1 + src/AddressTransformer.php | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b986e5c..0724fd5 100644 --- a/composer.json +++ b/composer.json @@ -61,6 +61,7 @@ "php": ">=8.0", "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.6", diff --git a/src/AddressTransformer.php b/src/AddressTransformer.php index 625e29e..9ba29f8 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,12 @@ public function transform_wp_to_mollie( WordPressAddress $address ): MollieAddre $mollie_address = new MollieAddress( $given_name, $family_name, $email, $street_and_number, $city, $country ); + $phone_util = PhoneNumberUtil::getInstance(); + + $phone_number_object = $phone_util->parse( $address->get_phone(), $country ); + $mollie_address->organization_name = $address->get_company_name(); - $mollie_address->phone = $address->get_phone(); + $mollie_address->phone = $phone_util->format( $phone_number_object, PhoneNumberFormat::E164 ); $mollie_address->street_additional = $address->get_line_2(); $mollie_address->postal_code = $address->get_postal_code(); $mollie_address->region = $address->get_region(); From b783be02d5a1e231ced83dd4c33391b9a9406105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reu=CC=88el=20van=20der=20Steege?= Date: Fri, 18 Oct 2024 10:57:27 +0200 Subject: [PATCH 2/5] Fix "Parameter #1 $numberToParse of method libphonenumber\PhoneNumberUtil::parse() expects string, string|null given". --- src/AddressTransformer.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/AddressTransformer.php b/src/AddressTransformer.php index 9ba29f8..897e755 100644 --- a/src/AddressTransformer.php +++ b/src/AddressTransformer.php @@ -63,12 +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_util = PhoneNumberUtil::getInstance(); + $phone = $address->get_phone(); - $phone_number_object = $phone_util->parse( $address->get_phone(), $country ); + 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 = $phone_util->format( $phone_number_object, PhoneNumberFormat::E164 ); + $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(); From 2ed8e81af09f2f792179c98d6cffee9e93d37ea8 Mon Sep 17 00:00:00 2001 From: Remco Tolsma Date: Fri, 18 Oct 2024 13:32:22 +0200 Subject: [PATCH 3/5] Update composer.json --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 0724fd5..7e85c8a 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ }, "config": { "platform": { - "php": "8.0" + "php": "8.1" }, "platform-check": false, "sort-packages": true, @@ -58,7 +58,7 @@ } ], "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-json": "*", "automattic/jetpack-autoloader": "^3.0", "giggsey/libphonenumber-for-php": "^8.13", @@ -66,7 +66,7 @@ "pronamic/wp-http": "^1.2", "pronamic/wp-mollie": "^1.6", "woocommerce/action-scheduler": "^3.8", - "wp-pay/core": "^4.18" + "wp-pay/core": "^4.23" }, "require-dev": { "overtrue/phplint": "^9.0", From acf7350184fab030794736354614dd86f3a6d8db Mon Sep 17 00:00:00 2001 From: Remco Tolsma Date: Fri, 18 Oct 2024 13:44:23 +0200 Subject: [PATCH 4/5] Create AddressTransformerTest.php --- tests/src/AddressTransformerTest.php | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/src/AddressTransformerTest.php diff --git a/tests/src/AddressTransformerTest.php b/tests/src/AddressTransformerTest.php new file mode 100644 index 0000000..305239e --- /dev/null +++ b/tests/src/AddressTransformerTest.php @@ -0,0 +1,63 @@ + + * @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' ], + [ '+321234567890', '+321234567890' ], + ]; + } +} From 64bfce5e63544a8fa9d9cb6eea57fa89a3475476 Mon Sep 17 00:00:00 2001 From: Remco Tolsma Date: Fri, 18 Oct 2024 13:46:56 +0200 Subject: [PATCH 5/5] Update AddressTransformerTest.php --- tests/src/AddressTransformerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/AddressTransformerTest.php b/tests/src/AddressTransformerTest.php index 305239e..ee7b454 100644 --- a/tests/src/AddressTransformerTest.php +++ b/tests/src/AddressTransformerTest.php @@ -57,7 +57,9 @@ public function test_transform( $phone, $phone_e164 ) { public function transform_provider() { return [ [ '1234567890', '+311234567890' ], + [ '12 34 56 78 90', '+311234567890' ], [ '+321234567890', '+321234567890' ], + [ '+491234567890', '+491234567890' ], ]; } }