Change address handling for card button

Make the name field to always appear and send at least one address
This commit is contained in:
Alex P 2022-07-19 09:24:19 +03:00
parent d4e8bd453c
commit 619e9d2552
4 changed files with 59 additions and 30 deletions

View file

@ -192,7 +192,7 @@ class OrderEndpoint {
'application_context' => $this->application_context_repository
->current_context( $shipping_preference )->to_array(),
);
if ( $payer && ! empty( $payer->email_address() ) && ! empty( $payer->name() ) ) {
if ( $payer && ! empty( $payer->email_address() ) ) {
$data['payer'] = $payer->to_array();
}
if ( $payment_token ) {

View file

@ -18,7 +18,7 @@ class Payer {
/**
* The name.
*
* @var PayerName
* @var PayerName|null
*/
private $name;
@ -46,7 +46,7 @@ class Payer {
/**
* The address.
*
* @var Address
* @var Address|null
*/
private $address;
@ -67,7 +67,7 @@ class Payer {
/**
* Payer constructor.
*
* @param PayerName $name The name.
* @param PayerName|null $name The name.
* @param string $email_address The email.
* @param string $payer_id The payer id.
* @param Address|null $address The address.
@ -76,7 +76,7 @@ class Payer {
* @param PayerTaxInfo|null $tax_info The tax info.
*/
public function __construct(
PayerName $name,
?PayerName $name,
string $email_address,
string $payer_id,
Address $address = null,
@ -97,12 +97,21 @@ class Payer {
/**
* Returns the name.
*
* @return PayerName
* @return PayerName|null
*/
public function name(): PayerName {
public function name(): ?PayerName {
return $this->name;
}
/**
* Sets the name.
*
* @param PayerName|null $name The value.
*/
public function set_name( ?PayerName $name ): void {
$this->name = $name;
}
/**
* Returns the email address.
*
@ -139,6 +148,15 @@ class Payer {
return $this->address;
}
/**
* Sets the address.
*
* @param Address|null $address The value.
*/
public function set_address( ?Address $address ): void {
$this->address = $address;
}
/**
* Returns the phone.
*
@ -164,27 +182,26 @@ class Payer {
*/
public function to_array() {
$payer = array(
'name' => $this->name()->to_array(),
'email_address' => $this->email_address(),
);
if ( $this->address() ) {
$payer['address'] = $this->address->to_array();
if ( 2 !== strlen( $this->address()->country_code() ) ) {
unset( $payer['address'] );
}
if ( $this->name ) {
$payer['name'] = $this->name->to_array();
}
if ( $this->payer_id() ) {
$payer['payer_id'] = $this->payer_id();
if ( $this->address && 2 === strlen( $this->address->country_code() ) ) {
$payer['address'] = $this->address->to_array();
}
if ( $this->payer_id ) {
$payer['payer_id'] = $this->payer_id;
}
if ( $this->phone() ) {
$payer['phone'] = $this->phone()->to_array();
if ( $this->phone ) {
$payer['phone'] = $this->phone->to_array();
}
if ( $this->tax_info() ) {
$payer['tax_info'] = $this->tax_info()->to_array();
if ( $this->tax_info ) {
$payer['tax_info'] = $this->tax_info->to_array();
}
if ( $this->birthdate() ) {
$payer['birth_date'] = $this->birthdate()->format( 'Y-m-d' );
if ( $this->birthdate ) {
$payer['birth_date'] = $this->birthdate->format( 'Y-m-d' );
}
return $payer;
}

View file

@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface;
use stdClass;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
@ -332,18 +333,35 @@ class CreateOrderEndpoint implements EndpointInterface {
private function create_paypal_order( \WC_Order $wc_order = null ): Order {
assert( $this->purchase_unit instanceof PurchaseUnit );
$funding_source = $this->parsed_request_data['funding_source'] ?? '';
$payer = $this->payer( $this->parsed_request_data, $wc_order );
$shipping_preference = $this->shipping_preference_factory->from_state(
$this->purchase_unit,
$this->parsed_request_data['context'],
WC()->cart,
$this->parsed_request_data['funding_source'] ?? ''
$funding_source
);
if ( 'card' === $funding_source ) {
if ( ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS === $shipping_preference ) {
if ( $payer ) {
$payer->set_address( null );
}
}
if ( ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING === $shipping_preference ) {
if ( $payer ) {
$payer->set_name( null );
}
}
}
try {
return $this->api_endpoint->create(
array( $this->purchase_unit ),
$shipping_preference,
$this->payer( $this->parsed_request_data, $wc_order ),
$payer,
null,
$this->payment_method()
);
@ -365,7 +383,7 @@ class CreateOrderEndpoint implements EndpointInterface {
return $this->api_endpoint->create(
array( $this->purchase_unit ),
$shipping_preference,
$this->payer( $this->parsed_request_data, $wc_order ),
$payer,
null,
$this->payment_method()
);

View file

@ -1046,8 +1046,6 @@ class OrderEndpointTest extends TestCase
$payer = Mockery::mock(Payer::class);
$payer->expects('email_address')->andReturn('email@email.com');
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$result = $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer);
$this->assertEquals($expectedOrder, $result);
@ -1138,8 +1136,6 @@ class OrderEndpointTest extends TestCase
$payer = Mockery::mock(Payer::class);
$payer->expects('email_address')->andReturn('email@email.com');
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer);
}
@ -1229,8 +1225,6 @@ class OrderEndpointTest extends TestCase
$this->expectException(RuntimeException::class);
$payer = Mockery::mock(Payer::class);
$payer->expects('email_address')->andReturn('email@email.com');
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer);
}