Ensure purchase units contains tax rate and shipping when creating the order

This commit is contained in:
dinamiko 2022-04-28 11:02:00 +02:00
parent 158f900282
commit 4978107554
4 changed files with 56 additions and 3 deletions

View file

@ -96,6 +96,9 @@ class PayUponInvoiceOrderEndpoint {
* @throws PayPalApiException When there is a problem creating the order.
*/
public function create( array $items, PaymentSource $payment_source ): Order {
$a = 1;
$data = array(
'intent' => 'CAPTURE',
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
@ -110,6 +113,11 @@ class PayUponInvoiceOrderEndpoint {
),
);
$a = 1;
$data = $this->ensure_tax_rate( $data );
$data = $this->ensure_shipping( $data, $payment_source->to_array() );
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/checkout/orders';
$args = array(
@ -191,4 +199,44 @@ class PayUponInvoiceOrderEndpoint {
$json->payment_source->pay_upon_invoice->deposit_bank_details,
);
}
/**
* Ensures items contains tax rate.
*
* @param array $data The data.
* @return array
*/
private function ensure_tax_rate( array $data ): array {
$items_count = count( $data['purchase_units'][0]['items'] );
for ( $i = 0; $i < $items_count; $i++ ) {
if ( ! isset( $data['purchase_units'][0]['items'][ $i ]['tax_rate'] ) ) {
$data['purchase_units'][0]['items'][ $i ]['tax_rate'] = '0.00';
}
}
return $data;
}
/**
* Ensures purchase units contains shipping by using payment source data.
*
* @param array $data The data.
* @param array $payment_source The payment source.
* @return array
*/
private function ensure_shipping( array $data, array $payment_source ): array {
if ( isset( $data['purchase_units'][0]['shipping'] ) ) {
return $data;
}
$given_name = $payment_source['name']['given_name'] ?? '';
$surname = $payment_source['name']['surname'] ?? '';
$address = $payment_source['billing_address'] ?? array();
$data['purchase_units'][0]['shipping']['name']['full_name'] = $given_name . ' ' . $surname;
$data['purchase_units'][0]['shipping']['address'] = $address;
return $data;
}
}

View file

@ -214,7 +214,7 @@ class PayUponInvoice {
'billing_birth_date',
array(
'type' => 'date',
'label' => $site_country_code === 'de' ? 'Geburtsdatum' : 'Birth date',
'label' => 'de' === $site_country_code ? 'Geburtsdatum' : 'Birth date',
'class' => array( 'form-row-wide' ),
'required' => true,
'clear' => true,

View file

@ -116,7 +116,9 @@ class PayUponInvoiceOrderEndpointTest extends TestCase
$this->fraudnet->shouldReceive('session_id')->andReturn('');
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
$purchaseUnit->shouldReceive('to_array')->andReturn([]);
$purchaseUnit->shouldReceive('to_array')->andReturn([
'items' => [],
]);
$items = [$purchaseUnit];
$paymentSource = Mockery::mock(PaymentSource::class);
@ -134,7 +136,9 @@ class PayUponInvoiceOrderEndpointTest extends TestCase
{
$this->fraudnet->shouldReceive('session_id')->andReturn('');
$purchaseUnit = Mockery::mock(PurchaseUnit::class);
$purchaseUnit->shouldReceive('to_array')->andReturn([]);
$purchaseUnit->shouldReceive('to_array')->andReturn([
'items' => [],
]);
$items = [$purchaseUnit];
$paymentSource = Mockery::mock(PaymentSource::class);
$paymentSource->shouldReceive('to_array')->andReturn([]);

View file

@ -103,6 +103,7 @@ class PayUponInvoiceGatewayTest extends TestCase
$order = Mockery::mock(Order::class);
$order->shouldReceive('id')->andReturn('1');
$order->shouldReceive('intent')->andReturn('CAPTURE');
$order->shouldReceive('payment_source')->andReturn('');
$purchase_unit = Mockery::mock(PurchaseUnit::class);
$payment_source = Mockery::mock(PaymentSource::class);