Merge branch 'trunk' into pcp-157-blocks

This commit is contained in:
Alex P 2023-02-15 11:38:31 +02:00
commit 70c48762fa
No known key found for this signature in database
GPG key ID: 54487A734A204D71
39 changed files with 893 additions and 849 deletions

View file

@ -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,
$container->get( 'wcgateway.is-fraudnet-enabled' ),
$container->get( 'wcgateway.fraudnet' )
@ -210,9 +206,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' );

View file

@ -116,13 +116,6 @@ class OrderEndpoint {
*/
private $bn_code;
/**
* The paypal request id repository.
*
* @var PayPalRequestIdRepository
*/
private $paypal_request_id_repository;
/**
* OrderEndpoint constructor.
*
@ -133,7 +126,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 bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false.
* @param FraudNet $fraudnet The FraudNet entity.
@ -147,7 +139,6 @@ class OrderEndpoint {
string $intent,
LoggerInterface $logger,
ApplicationContextRepository $application_context_repository,
PayPalRequestIdRepository $paypal_request_id_repository,
SubscriptionHelper $subscription_helper,
bool $is_fraudnet_enabled,
FraudNet $fraudnet,
@ -162,7 +153,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->is_fraudnet_enabled = $is_fraudnet_enabled;
$this->subscription_helper = $subscription_helper;
$this->fraudnet = $fraudnet;
@ -242,8 +232,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;
}
@ -252,6 +240,10 @@ class OrderEndpoint {
$args['headers']['PayPal-Client-Metadata-Id'] = $this->fraudnet->session_id();
}
if ( isset( $data['payment_source'] ) ) {
$args['headers']['PayPal-Request-Id'] = uniqid( 'ppcp-', true );
}
$response = $this->request( $url, $args );
if ( is_wp_error( $response ) ) {
$error = new RuntimeException(
@ -288,7 +280,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;
}
@ -309,10 +300,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 ) {
@ -384,10 +374,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 ) {
@ -458,9 +447,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 ) {
@ -534,17 +522,19 @@ class OrderEndpoint {
}
}
/**
* The filter can be used to modify the order patching request body data (the final prices, items).
*/
$patches_array = apply_filters( 'ppcp_patch_order_request_body_data', $patches_array );
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/checkout/orders/' . $order_to_update->id();
$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 ),
);

View file

@ -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 ),
);

View file

@ -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',
),
);

View file

@ -284,7 +284,14 @@ class PurchaseUnit {
$this->items()
),
);
if ( $ditch_items_when_mismatch && $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() ) ) {
$ditch = $ditch_items_when_mismatch && $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() );
/**
* The filter can be used to control when the items and totals breakdown are removed from PayPal order info.
*/
$ditch = apply_filters( 'ppcp_ditch_items_breakdown', $ditch, $this );
if ( $ditch ) {
unset( $purchase_unit['items'] );
unset( $purchase_unit['amount']['breakdown'] );
}

View file

@ -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;
}
}