Merge branch 'trunk' into PCP-991-v2-detach-vaulting-from-wc-subscriptions-support

This commit is contained in:
Emili Castells Guasch 2023-07-19 14:26:15 +02:00
commit d6c12ce29d
34 changed files with 669 additions and 379 deletions

View file

@ -298,6 +298,7 @@ return array(
$shipping_factory = $container->get( 'api.factory.shipping' );
$payments_factory = $container->get( 'api.factory.payments' );
$prefix = $container->get( 'api.prefix' );
$soft_descriptor = $container->get( 'wcgateway.soft-descriptor' );
return new PurchaseUnitFactory(
$amount_factory,
@ -306,7 +307,8 @@ return array(
$item_factory,
$shipping_factory,
$payments_factory,
$prefix
$prefix,
$soft_descriptor
);
},
'api.factory.patch-collection-factory' => static function ( ContainerInterface $container ): PatchCollectionFactory {

View file

@ -18,7 +18,6 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -27,7 +26,6 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
@ -176,13 +174,12 @@ class OrderEndpoint {
/**
* Creates an order.
*
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
* @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.
* @param string $user_action The user action.
* @param PurchaseUnit[] $items The purchase unit items for the order.
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
* @param Payer|null $payer The payer off the order.
* @param PaymentToken|null $payment_token The payment token.
* @param string $paypal_request_id The paypal request id.
* @param string $user_action The user action.
*
* @return Order
* @throws RuntimeException If the request fails.
@ -192,7 +189,6 @@ class OrderEndpoint {
string $shipping_preference,
Payer $payer = null,
PaymentToken $payment_token = null,
PaymentMethod $payment_method = null,
string $paypal_request_id = '',
string $user_action = ApplicationContext::USER_ACTION_CONTINUE
): Order {
@ -221,9 +217,6 @@ class OrderEndpoint {
if ( $payment_token ) {
$data['payment_source']['token'] = $payment_token->to_array();
}
if ( $payment_method ) {
$data['payment_method'] = $payment_method->to_array();
}
/**
* The filter can be used to modify the order creation request body data.

View file

@ -41,6 +41,9 @@ class ApplicationContext {
self::USER_ACTION_PAY_NOW,
);
const PAYMENT_METHOD_UNRESTRICTED = 'UNRESTRICTED';
const PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED = 'IMMEDIATE_PAYMENT_REQUIRED';
/**
* The brand name.
*
@ -91,11 +94,11 @@ class ApplicationContext {
private $cancel_url;
/**
* The payment method.
* The payment method preference.
*
* @var null
* @var string
*/
private $payment_method;
private $payment_method_preference;
/**
* ApplicationContext constructor.
@ -107,6 +110,7 @@ class ApplicationContext {
* @param string $landing_page The landing page.
* @param string $shipping_preference The shipping preference.
* @param string $user_action The user action.
* @param string $payment_method_preference The payment method preference.
*
* @throws RuntimeException When values are not valid.
*/
@ -117,7 +121,8 @@ class ApplicationContext {
string $locale = '',
string $landing_page = self::LANDING_PAGE_NO_PREFERENCE,
string $shipping_preference = self::SHIPPING_PREFERENCE_NO_SHIPPING,
string $user_action = self::USER_ACTION_CONTINUE
string $user_action = self::USER_ACTION_CONTINUE,
string $payment_method_preference = self::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED
) {
if ( ! in_array( $landing_page, self::VALID_LANDING_PAGE_VALUES, true ) ) {
@ -129,16 +134,14 @@ class ApplicationContext {
if ( ! in_array( $user_action, self::VALID_USER_ACTION_VALUES, true ) ) {
throw new RuntimeException( 'User action preference not correct' );
}
$this->return_url = $return_url;
$this->cancel_url = $cancel_url;
$this->brand_name = $brand_name;
$this->locale = $locale;
$this->landing_page = $landing_page;
$this->shipping_preference = $shipping_preference;
$this->user_action = $user_action;
// Currently we have not implemented the payment method.
$this->payment_method = null;
$this->return_url = $return_url;
$this->cancel_url = $cancel_url;
$this->brand_name = $brand_name;
$this->locale = $locale;
$this->landing_page = $landing_page;
$this->shipping_preference = $shipping_preference;
$this->user_action = $user_action;
$this->payment_method_preference = $payment_method_preference;
}
/**
@ -205,12 +208,10 @@ class ApplicationContext {
}
/**
* Returns the payment method.
*
* @return PaymentMethod|null
* Returns the payment method preference.
*/
public function payment_method() {
return $this->payment_method;
public function payment_method_preference(): string {
return $this->payment_method_preference;
}
/**
@ -223,9 +224,6 @@ class ApplicationContext {
if ( $this->user_action() ) {
$data['user_action'] = $this->user_action();
}
if ( $this->payment_method() ) {
$data['payment_method'] = $this->payment_method();
}
if ( $this->shipping_preference() ) {
$data['shipping_preference'] = $this->shipping_preference();
}
@ -244,6 +242,11 @@ class ApplicationContext {
if ( $this->cancel_url() ) {
$data['cancel_url'] = $this->cancel_url();
}
if ( $this->payment_method_preference ) {
$data['payment_method'] = array(
'payee_preferred' => $this->payment_method_preference,
);
}
return $data;
}
}

View file

@ -73,6 +73,13 @@ class Item {
*/
protected $tax_rate;
/**
* The cart item key.
*
* @var string|null
*/
protected $cart_item_key;
/**
* Item constructor.
*
@ -84,6 +91,7 @@ class Item {
* @param string $sku The SKU.
* @param string $category The category.
* @param float $tax_rate The tax rate.
* @param ?string $cart_item_key The cart key for this item.
*/
public function __construct(
string $name,
@ -93,18 +101,20 @@ class Item {
Money $tax = null,
string $sku = '',
string $category = 'PHYSICAL_GOODS',
float $tax_rate = 0
float $tax_rate = 0,
string $cart_item_key = null
) {
$this->name = $name;
$this->unit_amount = $unit_amount;
$this->quantity = $quantity;
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->tax_rate = $tax_rate;
$this->name = $name;
$this->unit_amount = $unit_amount;
$this->quantity = $quantity;
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->tax_rate = $tax_rate;
$this->cart_item_key = $cart_item_key;
}
/**
@ -179,6 +189,15 @@ class Item {
return round( (float) $this->tax_rate, 2 );
}
/**
* Returns the cart key for this item.
*
* @return string|null
*/
public function cart_item_key():?string {
return $this->cart_item_key;
}
/**
* Returns the object as array.
*
@ -202,6 +221,10 @@ class Item {
$item['tax_rate'] = (string) $this->tax_rate();
}
if ( $this->cart_item_key() ) {
$item['cart_item_key'] = (string) $this->cart_item_key();
}
return $item;
}
}

View file

@ -1,81 +0,0 @@
<?php
/**
* The PaymentMethod object
*
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
/**
* Class PaymentMethod
*/
class PaymentMethod {
const PAYER_SELECTED_DEFAULT = 'PAYPAL';
const PAYEE_PREFERRED_UNRESTRICTED = 'UNRESTRICTED';
const PAYEE_PREFERRED_IMMEDIATE_PAYMENT_REQUIRED = 'IMMEDIATE_PAYMENT_REQUIRED';
/**
* The preferred value.
*
* @var string
*/
private $preferred;
/**
* The selected value.
*
* @var string
*/
private $selected;
/**
* PaymentMethod constructor.
*
* @param string $preferred The preferred value.
* @param string $selected The selected value.
*/
public function __construct(
string $preferred = self::PAYEE_PREFERRED_UNRESTRICTED,
string $selected = self::PAYER_SELECTED_DEFAULT
) {
$this->preferred = $preferred;
$this->selected = $selected;
}
/**
* Returns the payer preferred value.
*
* @return string
*/
public function payee_preferred(): string {
return $this->preferred;
}
/**
* Returns the payer selected value.
*
* @return string
*/
public function payer_selected(): string {
return $this->selected;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
return array(
'payee_preferred' => $this->payee_preferred(),
'payer_selected' => $this->payer_selected(),
);
}
}

View file

@ -211,6 +211,15 @@ class PurchaseUnit {
return $this->custom_id;
}
/**
* Sets the custom ID.
*
* @param string $custom_id The value to set.
*/
public function set_custom_id( string $custom_id ): void {
$this->custom_id = $custom_id;
}
/**
* Returns the invoice id.
*

View file

@ -44,7 +44,8 @@ class ItemFactory {
public function from_wc_cart( \WC_Cart $cart ): array {
$items = array_map(
function ( array $item ): Item {
$product = $item['data'];
$product = $item['data'];
$cart_item_key = $item['key'] ?? null;
/**
* The WooCommerce product.
@ -61,7 +62,9 @@ class ItemFactory {
$this->prepare_description( $product->get_description() ),
null,
$product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
0,
$cart_item_key
);
},
$cart->get_cart_contents()

View file

@ -70,6 +70,13 @@ class PurchaseUnitFactory {
*/
private $prefix;
/**
* The Soft Descriptor.
*
* @var string
*/
private $soft_descriptor;
/**
* PurchaseUnitFactory constructor.
*
@ -80,6 +87,7 @@ class PurchaseUnitFactory {
* @param ShippingFactory $shipping_factory The shipping factory.
* @param PaymentsFactory $payments_factory The payments factory.
* @param string $prefix The prefix.
* @param string $soft_descriptor The soft descriptor.
*/
public function __construct(
AmountFactory $amount_factory,
@ -88,7 +96,8 @@ class PurchaseUnitFactory {
ItemFactory $item_factory,
ShippingFactory $shipping_factory,
PaymentsFactory $payments_factory,
string $prefix = 'WC-'
string $prefix = 'WC-',
string $soft_descriptor = ''
) {
$this->amount_factory = $amount_factory;
@ -98,6 +107,7 @@ class PurchaseUnitFactory {
$this->shipping_factory = $shipping_factory;
$this->payments_factory = $payments_factory;
$this->prefix = $prefix;
$this->soft_descriptor = $soft_descriptor;
}
/**
@ -128,7 +138,7 @@ class PurchaseUnitFactory {
$payee = $this->payee_repository->payee();
$custom_id = (string) $order->get_id();
$invoice_id = $this->prefix . $order->get_order_number();
$soft_descriptor = '';
$soft_descriptor = $this->soft_descriptor;
$purchase_unit = new PurchaseUnit(
$amount,
@ -198,7 +208,7 @@ class PurchaseUnitFactory {
}
}
$invoice_id = '';
$soft_descriptor = '';
$soft_descriptor = $this->soft_descriptor;
$purchase_unit = new PurchaseUnit(
$amount,
$items,
@ -233,7 +243,7 @@ class PurchaseUnitFactory {
$description = ( isset( $data->description ) ) ? $data->description : '';
$custom_id = ( isset( $data->custom_id ) ) ? $data->custom_id : '';
$invoice_id = ( isset( $data->invoice_id ) ) ? $data->invoice_id : '';
$soft_descriptor = ( isset( $data->soft_descriptor ) ) ? $data->soft_descriptor : '';
$soft_descriptor = ( isset( $data->soft_descriptor ) ) ? $data->soft_descriptor : $this->soft_descriptor;
$items = array();
if ( isset( $data->items ) && is_array( $data->items ) ) {
$items = array_map(

View file

@ -47,18 +47,21 @@ class ApplicationContextRepository {
string $user_action = ApplicationContext::USER_ACTION_CONTINUE
): ApplicationContext {
$brand_name = $this->settings->has( 'brand_name' ) ? $this->settings->get( 'brand_name' ) : '';
$locale = $this->valid_bcp47_code();
$landingpage = $this->settings->has( 'landing_page' ) ?
$brand_name = $this->settings->has( 'brand_name' ) ? $this->settings->get( 'brand_name' ) : '';
$locale = $this->valid_bcp47_code();
$landingpage = $this->settings->has( 'landing_page' ) ?
$this->settings->get( 'landing_page' ) : ApplicationContext::LANDING_PAGE_NO_PREFERENCE;
$context = new ApplicationContext(
$payment_preference = $this->settings->has( 'payee_preferred' ) && $this->settings->get( 'payee_preferred' ) ?
ApplicationContext::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED : ApplicationContext::PAYMENT_METHOD_UNRESTRICTED;
$context = new ApplicationContext(
network_home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ),
(string) wc_get_checkout_url(),
(string) $brand_name,
$locale,
(string) $landingpage,
$shipping_preferences,
$user_action
$user_action,
$payment_preference
);
return $context;
}