From cbe10b5f6efda082f7d620f5028501299ac6aba2 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 11 Jul 2022 12:30:29 +0200 Subject: [PATCH] Fix merge issues --- .../src/Endpoint/OrderEndpoint.php | 47 +++++++++++++++++++ modules/ppcp-wc-gateway/services.php | 1 + .../src/Gateway/OXXO/OXXOGateway.php | 30 +++++++++--- .../Gateway/PayUponInvoice/PayUponInvoice.php | 13 +++-- 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index 41448a542..abefeb38a 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; +use stdClass; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus; @@ -28,6 +29,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; +use WP_Error; /** * Class OrderEndpoint @@ -564,4 +566,49 @@ class OrderEndpoint { $new_order = $this->order( $order_to_update->id() ); return $new_order; } + + /** + * Confirms payment source. + * + * @param string $id The PayPal order ID. + * @param array $payment_source The payment source. + * @return stdClass + * @throws PayPalApiException If the request fails. + * @throws RuntimeException If something unexpected happens. + */ + public function confirm_payment_source( string $id, array $payment_source ): stdClass { + $bearer = $this->bearer->bearer(); + $url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $id . '/confirm-payment-source'; + + $data = array( + 'payment_source' => $payment_source, + 'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL', + 'application_context' => array( + 'locale' => 'es-MX', + ), + ); + + $args = array( + 'method' => 'POST', + 'headers' => array( + 'Authorization' => 'Bearer ' . $bearer->token(), + 'Content-Type' => 'application/json', + 'Prefer' => 'return=representation', + ), + 'body' => wp_json_encode( $data ), + ); + + $response = $this->request( $url, $args ); + if ( $response instanceof WP_Error ) { + 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 ); + } + + return $json; + } } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index aae86676b..6eb0cc5c2 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -2238,6 +2238,7 @@ return array( return new OXXOGateway( $container->get( 'api.endpoint.order' ), $container->get( 'api.factory.purchase-unit' ), + $container->get( 'api.factory.shipping-preference' ), $container->get( 'woocommerce.logger.woocommerce' ) ); }, diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php index a222992a3..681cea86c 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php @@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; /** * Class PayUponInvoiceGateway. @@ -36,6 +37,13 @@ class OXXOGateway extends WC_Payment_Gateway { */ protected $purchase_unit_factory; + /** + * The shipping preference factory. + * + * @var ShippingPreferenceFactory + */ + protected $shipping_preference_factory; + /** * The logger. * @@ -46,13 +54,15 @@ class OXXOGateway extends WC_Payment_Gateway { /** * OXXOGateway constructor. * - * @param OrderEndpoint $order_endpoint The order endpoint. - * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. - * @param LoggerInterface $logger The logger. + * @param OrderEndpoint $order_endpoint The order endpoint. + * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. + * @param ShippingPreferenceFactory $shipping_preference_factory The shipping preference factory. + * @param LoggerInterface $logger The logger. */ public function __construct( OrderEndpoint $order_endpoint, PurchaseUnitFactory $purchase_unit_factory, + ShippingPreferenceFactory $shipping_preference_factory, LoggerInterface $logger ) { $this->id = self::ID; @@ -75,9 +85,10 @@ class OXXOGateway extends WC_Payment_Gateway { ) ); - $this->order_endpoint = $order_endpoint; - $this->purchase_unit_factory = $purchase_unit_factory; - $this->logger = $logger; + $this->order_endpoint = $order_endpoint; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->shipping_preference_factory = $shipping_preference_factory; + $this->logger = $logger; } /** @@ -122,7 +133,12 @@ class OXXOGateway extends WC_Payment_Gateway { $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); try { - $order = $this->order_endpoint->create( array( $purchase_unit ) ); + $shipping_preference = $this->shipping_preference_factory->from_state( + $purchase_unit, + 'checkout' + ); + + $order = $this->order_endpoint->create( array( $purchase_unit ), $shipping_preference ); $payment_source = array( 'oxxo' => array( 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 87f89997a..d3941282a 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\CaptureFactory; use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\WcGateway\Helper\CheckoutHelper; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceHelper; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; @@ -116,11 +117,9 @@ class PayUponInvoice { protected $pui_product_status; /** - * The capture factory. - * - * @var CaptureFactory + * @var CheckoutHelper */ - protected $capture_factory; + protected $checkout_helper; /** * PayUponInvoice constructor. @@ -137,7 +136,7 @@ class PayUponInvoice { * @param string $current_ppcp_settings_page_id Current PayPal settings page id. * @param PayUponInvoiceProductStatus $pui_product_status The PUI product status. * @param PayUponInvoiceHelper $pui_helper The PUI helper. - * @param CaptureFactory $capture_factory The capture factory. + * @param CheckoutHelper $checkout_helper The checkout helper. */ public function __construct( string $module_url, @@ -152,7 +151,7 @@ class PayUponInvoice { string $current_ppcp_settings_page_id, PayUponInvoiceProductStatus $pui_product_status, PayUponInvoiceHelper $pui_helper, - CaptureFactory $capture_factory + CheckoutHelper $checkout_helper ) { $this->module_url = $module_url; $this->fraud_net = $fraud_net; @@ -166,7 +165,7 @@ class PayUponInvoice { $this->current_ppcp_settings_page_id = $current_ppcp_settings_page_id; $this->pui_product_status = $pui_product_status; $this->pui_helper = $pui_helper; - $this->capture_factory = $capture_factory; + $this->checkout_helper = $checkout_helper; } /**