Skip to content

Commit

Permalink
Merge pull request #60 from pronamic/59-phone-field-should-contain-va…
Browse files Browse the repository at this point in the history
…lid-phone-number

59 phone field should contain valid phone number
  • Loading branch information
rvdsteege authored Oct 18, 2024
2 parents 34bf21e + 64bfce5 commit 37d794f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 13 additions & 1 deletion src/AddressTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
65 changes: 65 additions & 0 deletions tests/src/AddressTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Address transformer test
*
* @author Pronamic <[email protected]>
* @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( '[email protected]' );
$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' ],
];
}
}

0 comments on commit 37d794f

Please sign in to comment.