Set transaction id for authorization

This commit is contained in:
Alex P 2021-10-14 16:34:03 +03:00
parent 79e2dd2ef5
commit d40384aa1a
2 changed files with 21 additions and 18 deletions

View file

@ -176,7 +176,7 @@ class OrderProcessor {
$transaction_id = $this->get_paypal_order_transaction_id( $order ); $transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( '' !== $transaction_id ) { if ( $transaction_id ) {
$this->set_order_transaction_id( $transaction_id, $wc_order ); $this->set_order_transaction_id( $transaction_id, $wc_order );
} }
@ -216,32 +216,34 @@ class OrderProcessor {
} }
/** /**
* Retrieve transaction id from PayPal order. * Retrieves transaction id from PayPal order.
* *
* @param Order $order Order to get transaction id from. * @param Order $order The order to get transaction id from.
* *
* @return string * @return string|null
*/ */
private function get_paypal_order_transaction_id( Order $order ): string { private function get_paypal_order_transaction_id( Order $order ): ?string {
$purchase_units = $order->purchase_units(); $purchase_unit = $order->purchase_units()[0] ?? null;
if ( ! $purchase_unit ) {
if ( ! isset( $purchase_units[0] ) ) { return null;
return '';
} }
$payments = $purchase_units[0]->payments(); $payments = $purchase_unit->payments();
if ( null === $payments ) { if ( null === $payments ) {
return ''; return null;
} }
$captures = $payments->captures(); $capture = $payments->captures()[0] ?? null;
if ( $capture ) {
if ( isset( $captures[0] ) ) { return $capture->id();
return $captures[0]->id();
} }
return ''; $authorization = $payments->authorizations()[0] ?? null;
if ( $authorization ) {
return $authorization->id();
}
return null;
} }
/** /**

View file

@ -159,7 +159,8 @@ class OrderProcessorTest extends TestCase
$wcOrder $wcOrder
->expects('update_status') ->expects('update_status')
->with('on-hold', 'Awaiting payment.'); ->with('on-hold', 'Awaiting payment.');
$wcOrder->expects('set_transaction_id')
->with($transactionId);
$this->assertTrue($testee->process($wcOrder)); $this->assertTrue($testee->process($wcOrder));
} }