-
Notifications
You must be signed in to change notification settings - Fork 59
Vault via Orders API for redirect gateway in Blocks Checkout (3796) #4131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+280
−38
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14797c4
Add PayPal saved payment to blocks checkout
Dinamiko 3fa0ea4
Fix proceed with PayPal button flow in blocks checkout
Dinamiko 532e1c6
Merge branch 'dev/PCP-5413-remove-legacy-ui' into poc/vault-component
Dinamiko 370da31
Add save payment during purchase in blocks checkout
Dinamiko 4ea805c
Remove unnecessary comments
Dinamiko f81539f
Merge branch 'dev/develop' into poc/vault-component
Dinamiko c81ff36
Add explicit save after authorize meta updates in vault token flow
Dinamiko a62987e
Add HTTP status code check in CapturePayPalPayment::create_order()
Dinamiko 7559542
Fix typo check
Dinamiko 434dd67
Fix typo check
Dinamiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/ppcp-blocks/resources/js/Components/paypal-place-order-content.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { useEffect } from '@wordpress/element'; | ||
|
|
||
| export const PayPalPlaceOrderContent = ( { | ||
| description, | ||
| placeOrderButtonDescription, | ||
| eventRegistration, | ||
| emitResponse, | ||
| } ) => { | ||
| const { onPaymentSetup } = eventRegistration; | ||
| const { responseTypes } = emitResponse; | ||
|
|
||
| useEffect( | ||
| () => | ||
| onPaymentSetup( () => { | ||
| return { type: responseTypes.SUCCESS }; | ||
| } ), | ||
| [ onPaymentSetup, responseTypes ] | ||
| ); | ||
|
|
||
| if ( placeOrderButtonDescription ) { | ||
| return ( | ||
| <div> | ||
| <p dangerouslySetInnerHTML={ { __html: description } } /> | ||
| <p | ||
| style={ { textAlign: 'center' } } | ||
| className="ppcp-place-order-description" | ||
| dangerouslySetInnerHTML={ { | ||
| __html: placeOrderButtonDescription, | ||
| } } | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| return <div dangerouslySetInnerHTML={ { __html: description } } />; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
modules/ppcp-wc-gateway/src/Endpoint/CapturePayPalPayment.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WooCommerce\PayPalCommerce\WcGateway\Endpoint; | ||
|
|
||
| use Psr\Log\LoggerInterface; | ||
| use RuntimeException; | ||
| use WC_Order; | ||
| 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; | ||
| use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; | ||
| use WooCommerce\PayPalCommerce\Settings\Data\SettingsProvider; | ||
| use WP_Error; | ||
|
|
||
| class CapturePayPalPayment { | ||
|
|
||
| use RequestTrait; | ||
|
|
||
| private string $host; | ||
|
|
||
| private Bearer $bearer; | ||
|
|
||
| private OrderFactory $order_factory; | ||
|
|
||
| private PurchaseUnitFactory $purchase_unit_factory; | ||
|
|
||
| private SettingsProvider $settings_provider; | ||
|
|
||
| private LoggerInterface $logger; | ||
|
|
||
| public function __construct( | ||
| string $host, | ||
| Bearer $bearer, | ||
| OrderFactory $order_factory, | ||
| PurchaseUnitFactory $purchase_unit_factory, | ||
| SettingsProvider $settings_provider, | ||
| LoggerInterface $logger | ||
| ) { | ||
| $this->host = $host; | ||
| $this->bearer = $bearer; | ||
| $this->order_factory = $order_factory; | ||
| $this->purchase_unit_factory = $purchase_unit_factory; | ||
| $this->settings_provider = $settings_provider; | ||
| $this->logger = $logger; | ||
| } | ||
|
|
||
| /** | ||
| * Creates PayPal order from the given PayPal/Venmo vault ID. | ||
| * | ||
| * @throws RuntimeException When request fails. | ||
| */ | ||
| public function create_order( | ||
| string $vault_id, | ||
| string $custom_id, | ||
| string $invoice_id, | ||
| WC_Order $wc_order, | ||
| string $payment_source_name = 'paypal' | ||
| ): Order { | ||
| $intent = strtoupper( $this->settings_provider->payment_intent() ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE'; | ||
| $items = array( $this->purchase_unit_factory->from_wc_cart( null, false, $wc_order->get_payment_method() ) ); | ||
|
|
||
| // phpcs:disable WordPress.Security.NonceVerification | ||
| $pay_for_order = wc_clean( wp_unslash( $_GET['pay_for_order'] ?? '' ) ); | ||
| $order_key = wc_clean( wp_unslash( $_GET['key'] ?? '' ) ); | ||
| // phpcs:enable | ||
| if ( $pay_for_order && $order_key === $wc_order->get_order_key() ) { | ||
| $items = array( $this->purchase_unit_factory->from_wc_order( $wc_order ) ); | ||
| } | ||
|
|
||
| $data = array( | ||
| 'intent' => $intent, | ||
| 'purchase_units' => array_map( | ||
| static function ( PurchaseUnit $item ): array { | ||
| return $item->to_array(); | ||
| }, | ||
| $items | ||
| ), | ||
| 'payment_source' => array( | ||
| $payment_source_name => array( | ||
| 'vault_id' => $vault_id, | ||
| 'experience_context' => array( | ||
| 'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED', | ||
| ), | ||
| ), | ||
| ), | ||
| 'custom_id' => $custom_id, | ||
| 'invoice_id' => $invoice_id, | ||
| ); | ||
|
|
||
| $bearer = $this->bearer->bearer(); | ||
| $url = trailingslashit( $this->host ) . 'v2/checkout/orders'; | ||
| $args = array( | ||
| 'method' => 'POST', | ||
| 'headers' => array( | ||
| 'Authorization' => 'Bearer ' . $bearer->token(), | ||
| 'Content-Type' => 'application/json', | ||
| 'PayPal-Request-Id' => uniqid( 'ppcp-', true ), | ||
| ), | ||
| '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 ( ! in_array( $status_code, array( 200, 201 ), true ) ) { | ||
| $error = new PayPalApiException( $json, $status_code ); | ||
| $this->logger->warning( $error->getMessage() ); | ||
| throw $error; | ||
| } | ||
|
|
||
| return $this->order_factory->from_paypal_response( $json ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, do we also want to check the status code here? like is done in orderEndpoint for example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!