mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
Do not send Paypal-Request-Id
The current usage is incorrect and only causes issues without solving any problems (we don't implement any way to retry requests).
This commit is contained in:
parent
d23a33e4c2
commit
2f6240bfb4
9 changed files with 18 additions and 312 deletions
|
@ -54,7 +54,6 @@ use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
return array(
|
||||
|
@ -118,8 +117,7 @@ return array(
|
|||
$container->get( 'api.factory.payment-token' ),
|
||||
$container->get( 'api.factory.payment-token-action-links' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'api.repository.customer' ),
|
||||
$container->get( 'api.repository.paypal-request-id' )
|
||||
$container->get( 'api.repository.customer' )
|
||||
);
|
||||
},
|
||||
'api.endpoint.webhook' => static function ( ContainerInterface $container ) : WebhookEndpoint {
|
||||
|
@ -187,7 +185,6 @@ return array(
|
|||
$settings = $container->get( 'wcgateway.settings' );
|
||||
$intent = $settings->has( 'intent' ) && strtoupper( (string) $settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
|
||||
$application_context_repository = $container->get( 'api.repository.application-context' );
|
||||
$paypal_request_id = $container->get( 'api.repository.paypal-request-id' );
|
||||
$subscription_helper = $container->get( 'subscription.helper' );
|
||||
return new OrderEndpoint(
|
||||
$container->get( 'api.host' ),
|
||||
|
@ -197,7 +194,6 @@ return array(
|
|||
$intent,
|
||||
$logger,
|
||||
$application_context_repository,
|
||||
$paypal_request_id,
|
||||
$subscription_helper
|
||||
);
|
||||
},
|
||||
|
@ -208,9 +204,6 @@ return array(
|
|||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
'api.repository.paypal-request-id' => static function( ContainerInterface $container ) : PayPalRequestIdRepository {
|
||||
return new PayPalRequestIdRepository();
|
||||
},
|
||||
'api.repository.application-context' => static function( ContainerInterface $container ) : ApplicationContextRepository {
|
||||
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
|
|
|
@ -101,13 +101,6 @@ class OrderEndpoint {
|
|||
*/
|
||||
private $bn_code;
|
||||
|
||||
/**
|
||||
* The paypal request id repository.
|
||||
*
|
||||
* @var PayPalRequestIdRepository
|
||||
*/
|
||||
private $paypal_request_id_repository;
|
||||
|
||||
/**
|
||||
* OrderEndpoint constructor.
|
||||
*
|
||||
|
@ -118,7 +111,6 @@ class OrderEndpoint {
|
|||
* @param string $intent The intent.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param ApplicationContextRepository $application_context_repository The application context repository.
|
||||
* @param PayPalRequestIdRepository $paypal_request_id_repository The paypal request id repository.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param string $bn_code The BN Code.
|
||||
*/
|
||||
|
@ -130,7 +122,6 @@ class OrderEndpoint {
|
|||
string $intent,
|
||||
LoggerInterface $logger,
|
||||
ApplicationContextRepository $application_context_repository,
|
||||
PayPalRequestIdRepository $paypal_request_id_repository,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
string $bn_code = ''
|
||||
) {
|
||||
|
@ -143,7 +134,6 @@ class OrderEndpoint {
|
|||
$this->logger = $logger;
|
||||
$this->application_context_repository = $application_context_repository;
|
||||
$this->bn_code = $bn_code;
|
||||
$this->paypal_request_id_repository = $paypal_request_id_repository;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
}
|
||||
|
||||
|
@ -169,7 +159,6 @@ class OrderEndpoint {
|
|||
* @param Payer|null $payer The payer off the order.
|
||||
* @param PaymentToken|null $payment_token The payment token.
|
||||
* @param PaymentMethod|null $payment_method The payment method.
|
||||
* @param string $paypal_request_id The paypal request id.
|
||||
*
|
||||
* @return Order
|
||||
* @throws RuntimeException If the request fails.
|
||||
|
@ -179,8 +168,7 @@ class OrderEndpoint {
|
|||
string $shipping_preference,
|
||||
Payer $payer = null,
|
||||
PaymentToken $payment_token = null,
|
||||
PaymentMethod $payment_method = null,
|
||||
string $paypal_request_id = ''
|
||||
PaymentMethod $payment_method = null
|
||||
): Order {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$data = array(
|
||||
|
@ -219,8 +207,6 @@ class OrderEndpoint {
|
|||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
||||
$paypal_request_id = $paypal_request_id ? $paypal_request_id : uniqid( 'ppcp-', true );
|
||||
$args['headers']['PayPal-Request-Id'] = $paypal_request_id;
|
||||
if ( $this->bn_code ) {
|
||||
$args['headers']['PayPal-Partner-Attribution-Id'] = $this->bn_code;
|
||||
}
|
||||
|
@ -260,7 +246,6 @@ class OrderEndpoint {
|
|||
throw $error;
|
||||
}
|
||||
$order = $this->order_factory->from_paypal_response( $json );
|
||||
$this->paypal_request_id_repository->set_for_order( $order, $paypal_request_id );
|
||||
return $order;
|
||||
}
|
||||
|
||||
|
@ -281,10 +266,9 @@ class OrderEndpoint {
|
|||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
'PayPal-Request-Id' => $this->paypal_request_id_repository->get_for_order( $order ),
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
),
|
||||
);
|
||||
if ( $this->bn_code ) {
|
||||
|
@ -356,10 +340,9 @@ class OrderEndpoint {
|
|||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
'PayPal-Request-Id' => $this->paypal_request_id_repository->get_for_order( $order ),
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
),
|
||||
);
|
||||
if ( $this->bn_code ) {
|
||||
|
@ -430,9 +413,8 @@ class OrderEndpoint {
|
|||
$url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $id;
|
||||
$args = array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'PayPal-Request-Id' => $this->paypal_request_id_repository->get_for_order_id( $id ),
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
if ( $this->bn_code ) {
|
||||
|
@ -517,12 +499,9 @@ class OrderEndpoint {
|
|||
$args = array(
|
||||
'method' => 'PATCH',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
'PayPal-Request-Id' => $this->paypal_request_id_repository->get_for_order(
|
||||
$order_to_update
|
||||
),
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
),
|
||||
'body' => wp_json_encode( $patches_array ),
|
||||
);
|
||||
|
|
|
@ -133,7 +133,7 @@ class PayUponInvoiceOrderEndpoint {
|
|||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
'PayPal-Client-Metadata-Id' => $this->fraudnet->session_id(),
|
||||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
||||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ), // Request-Id header is required.
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
|
|
@ -19,7 +19,6 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenActionLinksFactory;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
||||
|
||||
/**
|
||||
* Class PaymentTokenEndpoint
|
||||
|
@ -70,13 +69,6 @@ class PaymentTokenEndpoint {
|
|||
*/
|
||||
protected $customer_repository;
|
||||
|
||||
/**
|
||||
* The request id repository.
|
||||
*
|
||||
* @var PayPalRequestIdRepository
|
||||
*/
|
||||
private $request_id_repository;
|
||||
|
||||
/**
|
||||
* PaymentTokenEndpoint constructor.
|
||||
*
|
||||
|
@ -86,7 +78,6 @@ class PaymentTokenEndpoint {
|
|||
* @param PaymentTokenActionLinksFactory $payment_token_action_links_factory The PaymentTokenActionLinks factory.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param CustomerRepository $customer_repository The customer repository.
|
||||
* @param PayPalRequestIdRepository $request_id_repository The request id repository.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
|
@ -94,8 +85,7 @@ class PaymentTokenEndpoint {
|
|||
PaymentTokenFactory $factory,
|
||||
PaymentTokenActionLinksFactory $payment_token_action_links_factory,
|
||||
LoggerInterface $logger,
|
||||
CustomerRepository $customer_repository,
|
||||
PayPalRequestIdRepository $request_id_repository
|
||||
CustomerRepository $customer_repository
|
||||
) {
|
||||
|
||||
$this->host = $host;
|
||||
|
@ -104,7 +94,6 @@ class PaymentTokenEndpoint {
|
|||
$this->payment_token_action_links_factory = $payment_token_action_links_factory;
|
||||
$this->logger = $logger;
|
||||
$this->customer_repository = $customer_repository;
|
||||
$this->request_id_repository = $request_id_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -243,14 +232,11 @@ class PaymentTokenEndpoint {
|
|||
),
|
||||
);
|
||||
|
||||
$request_id = uniqid( 'ppcp-vault', true );
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Request-Id' => $request_id,
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
@ -277,8 +263,6 @@ class PaymentTokenEndpoint {
|
|||
|
||||
$links = $this->payment_token_action_links_factory->from_paypal_response( $json );
|
||||
|
||||
$this->request_id_repository->set( "ppcp-vault-{$user_id}", $request_id );
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
|
@ -302,7 +286,6 @@ class PaymentTokenEndpoint {
|
|||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Request-Id' => $this->request_id_repository->get( "ppcp-vault-{$user_id}" ),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
|
|
|
@ -1,112 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* The repository for the request IDs.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Repository
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||
|
||||
/**
|
||||
* Class PayPalRequestIdRepository
|
||||
*/
|
||||
class PayPalRequestIdRepository {
|
||||
|
||||
const KEY = 'ppcp-request-ids';
|
||||
|
||||
/**
|
||||
* Returns a request ID based on the order ID.
|
||||
*
|
||||
* @param string $order_id The order ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_for_order_id( string $order_id ): string {
|
||||
return $this->get( $order_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request ID for an order.
|
||||
*
|
||||
* @param Order $order The order.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_for_order( Order $order ): string {
|
||||
return $this->get_for_order_id( $order->id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a request ID for a specific order.
|
||||
*
|
||||
* @param Order $order The order.
|
||||
* @param string $request_id The ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function set_for_order( Order $order, string $request_id ): bool {
|
||||
$this->set( $order->id(), $request_id );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a request ID for the given key.
|
||||
*
|
||||
* @param string $key The key in the request ID storage.
|
||||
* @param string $request_id The ID.
|
||||
*/
|
||||
public function set( string $key, string $request_id ): void {
|
||||
$all = $this->all();
|
||||
$day_in_seconds = 86400;
|
||||
$all[ $key ] = array(
|
||||
'id' => $request_id,
|
||||
'expiration' => time() + 10 * $day_in_seconds,
|
||||
);
|
||||
$all = $this->cleanup( $all );
|
||||
update_option( self::KEY, $all );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a request ID.
|
||||
*
|
||||
* @param string $key The key in the request ID storage.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get( string $key ): string {
|
||||
$all = $this->all();
|
||||
return isset( $all[ $key ] ) ? (string) $all[ $key ]['id'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all IDs.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function all(): array {
|
||||
|
||||
return (array) get_option( 'ppcp-request-ids', array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up outdated request IDs.
|
||||
*
|
||||
* @param array $all All request IDs.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function cleanup( array $all ): array {
|
||||
|
||||
foreach ( $all as $order_id => $value ) {
|
||||
if ( time() < $value['expiration'] ) {
|
||||
continue;
|
||||
}
|
||||
unset( $all[ $order_id ] );
|
||||
}
|
||||
return $all;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue