2022-03-07 12:54:02 +01:00
< ? php
2022-04-14 17:17:56 +02:00
/**
* Create order for PUI .
*
* @ package WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice
*/
2022-03-07 12:54:02 +01:00
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 ;
2022-04-19 11:31:47 +02:00
use WP_Error ;
2022-03-07 12:54:02 +01:00
2022-04-14 17:17:56 +02:00
/**
* Class OrderEndpoint .
*/
2022-04-22 11:49:30 +02:00
class PayUponInvoiceOrderEndpoint {
2022-03-07 12:54:02 +01:00
use RequestTrait ;
/**
2022-04-14 17:17:56 +02:00
* The host .
*
2022-03-07 12:54:02 +01:00
* @ var string
*/
protected $host ;
/**
2022-04-14 17:17:56 +02:00
* The bearer .
*
2022-03-07 12:54:02 +01:00
* @ var Bearer
*/
protected $bearer ;
/**
2022-04-14 17:17:56 +02:00
* The order factory .
*
2022-03-07 12:54:02 +01:00
* @ var OrderFactory
*/
protected $order_factory ;
2022-03-09 16:23:03 +01:00
/**
2022-04-14 17:17:56 +02:00
* The FraudNet entity .
*
2022-03-09 16:23:03 +01:00
* @ var FraudNet
*/
2022-04-14 17:17:56 +02:00
protected $fraudnet ;
2022-03-07 12:54:02 +01:00
2022-03-09 17:22:17 +01:00
/**
2022-04-14 17:17:56 +02:00
* The logger .
*
2022-03-09 17:22:17 +01:00
* @ var LoggerInterface
*/
protected $logger ;
2022-04-14 17:17:56 +02:00
/**
* OrderEndpoint constructor .
*
* @ param string $host The host .
* @ param Bearer $bearer The bearer .
* @ param OrderFactory $order_factory The order factory .
* @ param FraudNet $fraudnet FrauNet entity .
* @ param LoggerInterface $logger The logger .
*/
2022-03-09 16:23:03 +01:00
public function __construct (
string $host ,
Bearer $bearer ,
OrderFactory $order_factory ,
2022-04-14 17:17:56 +02:00
FraudNet $fraudnet ,
2022-03-09 16:23:03 +01:00
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-04-14 17:17:56 +02: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 .
2022-04-14 17:17:56 +02:00
* @ param PaymentSource $payment_source The payment source .
* @ param string $fraudnet_session_id The FrauNet session ID .
2022-03-07 12:54:02 +01:00
* @ return Order
2022-04-14 17:17:56 +02:00
* @ throws RuntimeException When there is a problem with the payment source .
* @ throws PayPalApiException When there is a problem creating the order .
2022-03-07 12:54:02 +01:00
*/
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-04-14 17:17:56 +02:00
'PayPal-Client-Metadata-Id' => $fraudnet_session_id ? : $this -> fraudnet -> session_id (),
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 );
2022-04-19 12:41:13 +02:00
if ( $response instanceof WP_Error ) {
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-04-14 17:17:56 +02:00
if ( ! in_array ( $status_code , array ( 200 , 201 ), true ) ) {
2022-04-04 12:58:44 +02:00
$issue = $json -> details [ 0 ] -> issue ? ? null ;
2022-04-08 12:03:24 +02:00
2022-04-14 17:17:56 +02:00
$site_country_code = explode ( '-' , get_bloginfo ( 'language' ) )[ 0 ] ? ? '' ;
if ( 'PAYMENT_SOURCE_INFO_CANNOT_BE_VERIFIED' === $issue ) {
if ( 'de' === $site_country_code ) {
throw new RuntimeException ( 'Die Kombination aus Ihrem Namen und Ihrer Anschrift konnte nicht validiert werden. Bitte korrigieren Sie Ihre Daten und versuchen Sie es erneut. Weitere Informationen finden Sie in den Ratepay <a href="https://www.ratepay.com/legal-payment-dataprivacy/?lang=de" target="_blank">Datenschutzbestimmungen</a> oder nutzen Sie das Ratepay <a href="https://www.ratepay.com/kontakt/" target="_blank">Kontaktformular</a>.' );
2022-04-08 12:03:24 +02:00
} else {
2022-04-14 17:17:56 +02:00
throw new RuntimeException ( 'The combination of your name and address could not be validated. Please correct your data and try again. You can find further information in the <a href="https://www.ratepay.com/en/ratepay-data-privacy-statement/" target="_blank">Ratepay Data Privacy Statement</a> or you can contact Ratepay using this <a href="https://www.ratepay.com/en/contact/" target="_blank">contact form</a>.' );
2022-04-08 12:03:24 +02:00
}
2022-04-04 12:58:44 +02:00
}
2022-04-14 17:17:56 +02:00
if ( 'PAYMENT_SOURCE_DECLINED_BY_PROCESSOR' === $issue ) {
if ( 'de' === $site_country_code ) {
throw new RuntimeException ( 'Die gewählte Zahlungsart kann nicht genutzt werden. Diese Entscheidung basiert auf einem automatisierten <a href="https://www.ratepay.com/legal-payment-dataprivacy/?lang=de" target="_blank">Datenverarbeitungsverfahren</a>. Weitere Informationen finden Sie in den Ratepay Datenschutzbestimmungen oder nutzen Sie das Ratepay <a href="https://www.ratepay.com/kontakt/" target="_blank">Kontaktformular</a>.' );
2022-04-08 12:03:24 +02:00
} else {
2022-04-14 17:17:56 +02:00
throw new RuntimeException ( 'It is not possible to use the selected payment method. This decision is based on automated data processing. You can find further information in the <a href="https://www.ratepay.com/en/ratepay-data-privacy-statement/" target="_blank">Ratepay Data Privacy Statement</a> or you can contact Ratepay using this <a href="https://www.ratepay.com/en/contact/" target="_blank">contact form</a>.' );
2022-04-08 12:03:24 +02:00
}
2022-04-04 12:58:44 +02:00
}
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-04-14 17:17:56 +02:00
/**
* Get Ratepay payment instructions from PayPal order .
*
* @ param string $id The PayPal order ID .
* @ return array
* @ throws RuntimeException When there is a problem getting the order .
* @ throws PayPalApiException When there is a problem getting the order .
*/
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' ,
2022-04-14 17:17:56 +02:00
'PayPal-Request-Id' => uniqid ( 'ppcp-' , true ),
2022-03-20 14:15:56 +01:00
),
);
$response = $this -> request ( $url , $args );
2022-04-19 11:31:47 +02:00
if ( $response instanceof WP_Error ) {
2022-03-20 14:15:56 +01:00
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 );
}
2022-03-22 09:19:07 +01:00
return array (
$json -> payment_source -> pay_upon_invoice -> payment_reference ,
2022-04-14 17:17:56 +02:00
$json -> payment_source -> pay_upon_invoice -> deposit_bank_details ,
2022-03-22 09:19:07 +01:00
);
2022-03-20 14:15:56 +01:00
}
2022-03-07 12:54:02 +01:00
}