Authorize the order/payment during the checkout process

This commit is contained in:
Mészáros Róbert 2020-04-14 15:39:55 +03:00
parent 584a1f4a3a
commit c6c931dc5c
2 changed files with 10 additions and 3 deletions

View file

@ -122,9 +122,9 @@ class OrderEndpoint
return $order; return $order;
} }
public function authorize(string $orderId) : bool { public function authorize(Order $order) : Order {
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$url = trailingslashit($this->host) . 'v2/checkout/orders/' . $orderId . '/authorize'; $url = trailingslashit($this->host) . 'v2/checkout/orders/' . $order->id() . '/authorize';
$args = [ $args = [
'headers' => [ 'headers' => [
'Authorization' => 'Bearer ' . $bearer, 'Authorization' => 'Bearer ' . $bearer,
@ -140,7 +140,9 @@ class OrderEndpoint
if (wp_remote_retrieve_response_code($response) !== 201) { if (wp_remote_retrieve_response_code($response) !== 201) {
throw new RuntimeException(__('Could not authorize order.', 'woocommerce-paypal-commerce-gateway')); throw new RuntimeException(__('Could not authorize order.', 'woocommerce-paypal-commerce-gateway'));
} }
return true; $json = json_decode($response['body']);
$order = $this->orderFactory->fromPayPalResponse($json);
return $order;
} }
public function order(string $id) : Order public function order(string $id) : Order

View file

@ -73,6 +73,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
$wcOrder = new \WC_Order($orderId); $wcOrder = new \WC_Order($orderId);
//ToDo: We need to fetch the order from paypal again to get it with the new status. //ToDo: We need to fetch the order from paypal again to get it with the new status.
$order = $this->sessionHandler->order(); $order = $this->sessionHandler->order();
update_post_meta($orderId, '_paypal_order_id', $order->id()); update_post_meta($orderId, '_paypal_order_id', $order->id());
@ -91,6 +92,10 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
$order = $this->endpoint->capture($order); $order = $this->endpoint->capture($order);
} }
if ($order->intent() === 'AUTHORIZE') {
$order = $this->endpoint->authorize($order);
}
$wcOrder->update_status('on-hold', __('Awaiting payment.', 'woocommerce-paypal-gateway')); $wcOrder->update_status('on-hold', __('Awaiting payment.', 'woocommerce-paypal-gateway'));
if ($order->status()->is(OrderStatus::COMPLETED)) { if ($order->status()->is(OrderStatus::COMPLETED)) {
$wcOrder->update_status('processing', __('Payment received.', 'woocommerce-paypal-gateway')); $wcOrder->update_status('processing', __('Payment received.', 'woocommerce-paypal-gateway'));