2022-03-07 12:54:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use RuntimeException;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
|
|
|
|
|
|
|
class OrderEndpoint {
|
|
|
|
|
|
|
|
use RequestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Bearer
|
|
|
|
*/
|
|
|
|
protected $bearer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var OrderFactory
|
|
|
|
*/
|
|
|
|
protected $order_factory;
|
|
|
|
|
2022-03-09 16:23:03 +01:00
|
|
|
/**
|
|
|
|
* @var FraudNet
|
|
|
|
*/
|
|
|
|
protected $fraudNet;
|
2022-03-07 12:54:02 +01:00
|
|
|
|
2022-03-09 17:22:17 +01:00
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2022-03-09 16:23:03 +01:00
|
|
|
public function __construct(
|
|
|
|
string $host,
|
|
|
|
Bearer $bearer,
|
|
|
|
OrderFactory $order_factory,
|
|
|
|
FraudNet $fraudNet,
|
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
2022-03-07 14:49:41 +01:00
|
|
|
$this->host = $host;
|
|
|
|
$this->bearer = $bearer;
|
2022-03-07 12:54:02 +01:00
|
|
|
$this->order_factory = $order_factory;
|
2022-03-07 14:49:41 +01:00
|
|
|
$this->logger = $logger;
|
2022-03-09 17:22:17 +01:00
|
|
|
$this->fraudNet = $fraudNet;
|
2022-03-07 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an order.
|
|
|
|
*
|
|
|
|
* @param PurchaseUnit[] $items The purchase unit items for the order.
|
|
|
|
* @return Order
|
|
|
|
*/
|
2022-03-25 14:28:27 +01:00
|
|
|
public function create( array $items, PaymentSource $payment_source, $fraudnet_session_id = '' ): Order {
|
2022-03-07 12:54:02 +01:00
|
|
|
$data = array(
|
2022-03-07 14:49:41 +01:00
|
|
|
'intent' => 'CAPTURE',
|
2022-03-07 12:54:02 +01:00
|
|
|
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
|
2022-03-07 14:49:41 +01:00
|
|
|
'purchase_units' => array_map(
|
2022-03-07 12:54:02 +01:00
|
|
|
static function ( PurchaseUnit $item ): array {
|
|
|
|
return $item->to_array();
|
|
|
|
},
|
|
|
|
$items
|
|
|
|
),
|
2022-03-07 14:49:41 +01:00
|
|
|
'payment_source' => array(
|
2022-03-09 17:22:17 +01:00
|
|
|
'pay_upon_invoice' => $payment_source->to_array(),
|
2022-03-07 12:54:02 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$bearer = $this->bearer->bearer();
|
2022-03-07 14:49:41 +01:00
|
|
|
$url = trailingslashit( $this->host ) . 'v2/checkout/orders';
|
|
|
|
$args = array(
|
2022-03-07 12:54:02 +01:00
|
|
|
'method' => 'POST',
|
|
|
|
'headers' => array(
|
2022-03-07 14:49:41 +01:00
|
|
|
'Authorization' => 'Bearer ' . $bearer->token(),
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Prefer' => 'return=representation',
|
2022-03-25 14:28:27 +01:00
|
|
|
'PayPal-Client-Metadata-Id' => $fraudnet_session_id ?: $this->fraudNet->sessionId(),
|
2022-03-07 14:49:41 +01:00
|
|
|
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
2022-03-07 12:54:02 +01:00
|
|
|
),
|
|
|
|
'body' => wp_json_encode( $data ),
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->request( $url, $args );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
2022-03-07 14:49:41 +01:00
|
|
|
throw new RuntimeException( $response->get_error_message() );
|
2022-03-07 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode( $response['body'] );
|
|
|
|
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
2022-03-11 12:33:49 +01:00
|
|
|
if ( ! in_array($status_code, [200,201] ) ) {
|
2022-03-07 14:49:41 +01:00
|
|
|
throw new PayPalApiException( $json, $status_code );
|
2022-03-07 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->order_factory->from_paypal_response( $json );
|
|
|
|
}
|
2022-03-20 14:15:56 +01:00
|
|
|
|
2022-03-22 09:19:07 +01:00
|
|
|
public function order_payment_instructions(string $id): array {
|
2022-03-20 14:15:56 +01:00
|
|
|
$bearer = $this->bearer->bearer();
|
|
|
|
$url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $id;
|
|
|
|
$args = array(
|
|
|
|
'headers' => array(
|
|
|
|
'Authorization' => 'Bearer ' . $bearer->token(),
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->request( $url, $args );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
throw new RuntimeException( $response->get_error_message() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_decode( $response['body'] );
|
|
|
|
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
|
|
|
if ( 200 !== $status_code ) {
|
|
|
|
throw new PayPalApiException( $json, $status_code );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Payment instructions');
|
|
|
|
$this->logger->info($json->payment_source->pay_upon_invoice->payment_reference);
|
|
|
|
$this->logger->info(wc_print_r($json->payment_source->pay_upon_invoice->deposit_bank_details, true));
|
2022-03-22 09:19:07 +01:00
|
|
|
|
|
|
|
return array(
|
|
|
|
$json->payment_source->pay_upon_invoice->payment_reference,
|
|
|
|
$json->payment_source->pay_upon_invoice->deposit_bank_details
|
|
|
|
);
|
2022-03-20 14:15:56 +01:00
|
|
|
}
|
2022-03-07 12:54:02 +01:00
|
|
|
}
|