mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge branch 'master' into feature/create-order-intent-authorize
# Conflicts: # modules.local/ppcp-api-client/services.php # modules.local/ppcp-button/src/Assets/SmartButton.php
This commit is contained in:
commit
479572cfc7
25 changed files with 804 additions and 253 deletions
|
@ -17,5 +17,13 @@
|
|||
"psr-4": {
|
||||
"Inpsyde\\PayPalCommerce\\ApiClient\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Inpsyde\\PayPalCommerce\\ApiClient\\": "tests/PHPUnit"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"unit": "./vendor/bin/phpunit --coverage-html build/coverage-report"
|
||||
}
|
||||
}
|
|
@ -5,13 +5,13 @@ namespace Inpsyde\PayPalCommerce\ApiClient;
|
|||
|
||||
use Dhii\Data\Container\ContainerInterface;
|
||||
use Inpsyde\CacheModule\Provider\CacheProviderInterface;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Config\Config;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\AddressFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\AmountFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\ItemFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\LineItemFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayeeFactory;
|
||||
|
@ -20,6 +20,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
|||
use Inpsyde\PayPalCommerce\ApiClient\Factory\ShippingFactory;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
|
||||
return [
|
||||
|
||||
|
@ -65,7 +66,7 @@ return [
|
|||
$errorResponseFactory
|
||||
);
|
||||
},
|
||||
'api.cart-repository' => function (ContainerInterface $container) : CartRepository {
|
||||
'api.repository.cart' => function (ContainerInterface $container) : CartRepository {
|
||||
/*
|
||||
* ToDo: We need to watch out and see, if we can load the Cart Repository
|
||||
* only on specific situations.
|
||||
|
@ -82,14 +83,23 @@ return [
|
|||
$factory = $container->get('api.factory.purchase-unit');
|
||||
return new CartRepository($cart, $factory);
|
||||
},
|
||||
'api.config.config' => function (ContainerInterface $container) : Config {
|
||||
return new Config();
|
||||
},
|
||||
'api.repository.payee' => function (ContainerInterface $container) : PayeeRepository {
|
||||
$config = $container->get('api.config.config');
|
||||
return new PayeeRepository($config);
|
||||
},
|
||||
'api.factory.purchase-unit' => function (ContainerInterface $container) : PurchaseUnitFactory {
|
||||
|
||||
$amountFactory = $container->get('api.factory.amount');
|
||||
$payeeRepository = $container->get('api.repository.payee');
|
||||
$payeeFactory = $container->get('api.factory.payee');
|
||||
$itemFactory = $container->get('api.factory.item');
|
||||
$shippingFactory = $container->get('api.factory.shipping');
|
||||
return new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
|
|
|
@ -52,6 +52,11 @@ class Bearer
|
|||
throw new RuntimeException(__('Could not find token.', 'woocommerce-paypal-commerce-gateway'));
|
||||
}
|
||||
$token = (string) $json->access_token;
|
||||
|
||||
/**
|
||||
* ToDo: Does expires_in really work as expected. Woke up after a weekend and bearer did
|
||||
* not work. Validate.
|
||||
**/
|
||||
$this->cache->set(self::CACHE_KEY, $token, $json->expires_in);
|
||||
return $token;
|
||||
}
|
||||
|
|
39
modules.local/ppcp-api-client/src/Config/Config.php
Normal file
39
modules.local/ppcp-api-client/src/Config/Config.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Config;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\NotFoundException;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
|
||||
// phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
* This container contains settings, which are not handled by the gateway's userinterface.
|
||||
*
|
||||
* ToDo: We need to read the configuration from somewhere.
|
||||
* @package Inpsyde\PayPalCommerce\ApiClient\Config
|
||||
*/
|
||||
class Config implements ContainerInterface
|
||||
{
|
||||
|
||||
private $config = [
|
||||
'merchant_email' => 'payment-facilitator@websupporter.net',
|
||||
'merchant_id' => '939Y32KZSLC8G',
|
||||
];
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
if (! $this->has($id)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return $this->config[$id];
|
||||
}
|
||||
|
||||
public function has($id)
|
||||
{
|
||||
return array_key_exists($id, $this->config);
|
||||
}
|
||||
}
|
|
@ -101,7 +101,6 @@ class OrderEndpoint
|
|||
$this->handleResponseWpError($url, $args);
|
||||
throw new RuntimeException(__('Could not capture order.', 'woocommerce-paypal-commerce-gateway'));
|
||||
}
|
||||
|
||||
$json = json_decode($response['body']);
|
||||
if (wp_remote_retrieve_response_code($response) !== 201) {
|
||||
$errors = $this->errorResponseFactory->fromPayPalResponse(
|
||||
|
|
|
@ -36,9 +36,12 @@ class Payee
|
|||
|
||||
public function toArray() : array
|
||||
{
|
||||
return [
|
||||
$data = [
|
||||
'email_address' => $this->email(),
|
||||
'merchant_id' => $this->merchantId(),
|
||||
];
|
||||
if ($this->merchantId) {
|
||||
$data['merchant_id'] = $this->merchantId();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Exception;
|
||||
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Exception;
|
||||
|
||||
class NotFoundException extends Exception implements NotFoundExceptionInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -5,14 +5,105 @@ namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
|
|||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Amount;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\AmountBreakdown;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Item;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
class AmountFactory
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
private $itemFactory;
|
||||
public function __construct(ItemFactory $itemFactory)
|
||||
{
|
||||
$this->itemFactory = $itemFactory;
|
||||
}
|
||||
|
||||
public function fromWcCart(\WC_Cart $cart) : Amount
|
||||
{
|
||||
$currency = get_woocommerce_currency();
|
||||
$total = new Money((float) $cart->get_total('numeric'), $currency);
|
||||
$itemsTotal = $cart->get_cart_contents_total() + $cart->get_discount_total();
|
||||
$itemsTotal = new Money((float) $itemsTotal, $currency);
|
||||
$shipping = new Money(
|
||||
(float) $cart->get_shipping_total() + $cart->get_shipping_tax(),
|
||||
$currency
|
||||
);
|
||||
|
||||
$taxes = new Money(
|
||||
(float) $cart->get_cart_contents_tax() + (float) $cart->get_discount_tax(),
|
||||
$currency
|
||||
);
|
||||
|
||||
$discount = null;
|
||||
if ($cart->get_discount_total()) {
|
||||
$discount = new Money(
|
||||
(float) $cart->get_discount_total() + $cart->get_discount_tax(),
|
||||
$currency
|
||||
);
|
||||
}
|
||||
//ToDo: Evaluate if more is needed? Fees?
|
||||
$breakdown = new AmountBreakdown(
|
||||
$itemsTotal,
|
||||
$shipping,
|
||||
$taxes,
|
||||
null, // insurance?
|
||||
null, // handling?
|
||||
null, //shipping discounts?
|
||||
$discount
|
||||
);
|
||||
$amount = new Amount(
|
||||
$total,
|
||||
$breakdown
|
||||
);
|
||||
return $amount;
|
||||
}
|
||||
|
||||
public function fromWcOrder(\WC_Order $order) : Amount
|
||||
{
|
||||
$currency = $order->get_currency();
|
||||
$items = $this->itemFactory->fromWcOrder($order);
|
||||
$total = new Money((float) $order->get_total(), $currency);
|
||||
$itemsTotal = new Money((float)array_reduce(
|
||||
$items,
|
||||
function (float $total, Item $item): float {
|
||||
return $total + $item->quantity() * $item->unitAmount()->value();
|
||||
},
|
||||
0
|
||||
), $currency);
|
||||
$shipping = new Money(
|
||||
(float) $order->get_shipping_total() + (float) $order->get_shipping_tax(),
|
||||
$currency
|
||||
);
|
||||
$taxes = new Money((float)array_reduce(
|
||||
$items,
|
||||
function (float $total, Item $item): float {
|
||||
return $total + $item->quantity() * $item->tax()->value();
|
||||
},
|
||||
0
|
||||
), $currency);
|
||||
|
||||
$discount = null;
|
||||
if ((float) $order->get_total_discount(false)) {
|
||||
$discount = new Money(
|
||||
(float) $order->get_total_discount(false),
|
||||
$currency
|
||||
);
|
||||
}
|
||||
|
||||
//ToDo: Evaluate if more is needed? Fees?
|
||||
$breakdown = new AmountBreakdown(
|
||||
$itemsTotal,
|
||||
$shipping,
|
||||
$taxes,
|
||||
null, // insurance?
|
||||
null, // handling?
|
||||
null, //shipping discounts?
|
||||
$discount
|
||||
);
|
||||
$amount = new Amount(
|
||||
$total,
|
||||
$breakdown
|
||||
);
|
||||
}
|
||||
|
||||
public function fromPayPalResponse(\stdClass $data) : Amount
|
||||
|
|
|
@ -14,6 +14,75 @@ class ItemFactory
|
|||
{
|
||||
}
|
||||
|
||||
public function fromWcCart(\WC_Cart $cart) : array {
|
||||
$currency = get_woocommerce_currency();
|
||||
$items = array_map(
|
||||
function (array $item) use ($currency): Item {
|
||||
$product = $item['data'];
|
||||
/**
|
||||
* @var \WC_Product $product
|
||||
*/
|
||||
$quantity = (int) $item['quantity'];
|
||||
|
||||
$price = (float) wc_get_price_including_tax($product);
|
||||
$priceWithoutTax = (float) wc_get_price_excluding_tax($product);
|
||||
$priceWithoutTaxRounded = round($priceWithoutTax, 2);
|
||||
$tax = round($price - $priceWithoutTaxRounded, 2);
|
||||
$tax = new Money($tax, $currency);
|
||||
return new Item(
|
||||
mb_substr($product->get_name(), 0, 127),
|
||||
new Money($priceWithoutTaxRounded, $currency),
|
||||
$quantity,
|
||||
mb_substr($product->get_description(), 0, 127),
|
||||
$tax,
|
||||
$product->get_sku(),
|
||||
($product->is_downloadable()) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
|
||||
);
|
||||
},
|
||||
$cart->get_cart_contents()
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WC_Order $order
|
||||
* @return Item[]
|
||||
*/
|
||||
public function fromWcOrder(\WC_Order $order) : array {
|
||||
|
||||
return array_map(
|
||||
function (\WC_Order_Item_Product $item) use ($order): Item {
|
||||
return $this->fromWcOrderLineItem($item, $order);
|
||||
},
|
||||
$order->get_items('line_item')
|
||||
);
|
||||
}
|
||||
|
||||
private function fromWcOrderLineItem(\WC_Order_Item_Product $item, \WC_Order $order) : Item {
|
||||
|
||||
$currency = $order->get_currency();
|
||||
$product = $item->get_product();
|
||||
/**
|
||||
* @var \WC_Product $product
|
||||
*/
|
||||
$quantity = $item->get_quantity();
|
||||
|
||||
$price = (float) $order->get_item_subtotal($item, true);
|
||||
$priceWithoutTax = (float) $order->get_item_subtotal($item, false);
|
||||
$priceWithoutTaxRounded = round($priceWithoutTax, 2);
|
||||
$tax = round($price - $priceWithoutTaxRounded, 2);
|
||||
$tax = new Money($tax, $currency);
|
||||
return new Item(
|
||||
mb_substr($product->get_name(), 0, 127),
|
||||
new Money($priceWithoutTaxRounded, $currency),
|
||||
$quantity,
|
||||
mb_substr($product->get_description(), 0, 127),
|
||||
$tax,
|
||||
$product->get_sku(),
|
||||
($product->is_downloadable()) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
|
||||
);
|
||||
}
|
||||
|
||||
public function fromPayPalRequest(\stdClass $data) : Item
|
||||
{
|
||||
if (! isset($data->name)) {
|
||||
|
|
|
@ -3,28 +3,29 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Amount;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\AmountBreakdown;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Item;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
|
||||
class PurchaseUnitFactory
|
||||
{
|
||||
|
||||
private $amountFactory;
|
||||
private $payeeRepository;
|
||||
private $payeeFactory;
|
||||
private $itemFactory;
|
||||
private $shippingFactory;
|
||||
public function __construct(
|
||||
AmountFactory $amountFactory,
|
||||
PayeeRepository $payeeRepository,
|
||||
PayeeFactory $payeeFactory,
|
||||
ItemFactory $itemFactory,
|
||||
ShippingFactory $shippingFactory
|
||||
) {
|
||||
|
||||
$this->amountFactory = $amountFactory;
|
||||
$this->payeeRepository = $payeeRepository;
|
||||
$this->payeeFactory = $payeeFactory;
|
||||
$this->itemFactory = $itemFactory;
|
||||
$this->shippingFactory = $shippingFactory;
|
||||
|
@ -32,90 +33,17 @@ class PurchaseUnitFactory
|
|||
|
||||
public function fromWcOrder(\WC_Order $order) : PurchaseUnit
|
||||
{
|
||||
$currency = get_woocommerce_currency();
|
||||
|
||||
$items = array_map(
|
||||
function (\WC_Order_Item_Product $item) use ($currency, $order): Item {
|
||||
|
||||
$product = $item->get_product();
|
||||
/**
|
||||
* @var \WC_Product $product
|
||||
*/
|
||||
$quantity = $item->get_quantity();
|
||||
|
||||
$price = (float) $order->get_item_subtotal($item, true);
|
||||
$priceWithoutTax = (float) $order->get_item_subtotal($item, false);
|
||||
$priceWithoutTaxRounded = round($priceWithoutTax, 2);
|
||||
$tax = round($price - $priceWithoutTaxRounded, 2);
|
||||
$tax = new Money($tax, $currency);
|
||||
return new Item(
|
||||
mb_substr($product->get_name(), 0, 127),
|
||||
new Money($priceWithoutTaxRounded, $currency),
|
||||
$quantity,
|
||||
mb_substr($product->get_description(), 0, 127),
|
||||
$tax,
|
||||
$product->get_sku(),
|
||||
($product->is_downloadable()) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
|
||||
);
|
||||
},
|
||||
$order->get_items('line_item')
|
||||
);
|
||||
|
||||
$total = new Money((float) $order->get_total(), $currency);
|
||||
$itemsTotal = new Money(array_reduce(
|
||||
$items,
|
||||
function (float $total, Item $item) : float {
|
||||
return $total + $item->quantity() * $item->unitAmount()->value();
|
||||
},
|
||||
0
|
||||
), $currency);
|
||||
$shipping = new Money(
|
||||
(float) $order->get_shipping_total() + (float) $order->get_shipping_tax(),
|
||||
$currency
|
||||
);
|
||||
$taxes = new Money(array_reduce(
|
||||
$items,
|
||||
function (float $total, Item $item) : float {
|
||||
return $total + $item->quantity() * $item->tax()->value();
|
||||
},
|
||||
0
|
||||
), $currency);
|
||||
|
||||
$discount = null;
|
||||
if ((float) $order->get_total_discount()) {
|
||||
$discount = new Money(
|
||||
(float) $order->get_total_discount(false),
|
||||
$currency
|
||||
);
|
||||
}
|
||||
|
||||
//ToDo: Evaluate if more is needed? Fees?
|
||||
$breakdown = new AmountBreakdown(
|
||||
$itemsTotal,
|
||||
$shipping,
|
||||
$taxes,
|
||||
null, // insurance?
|
||||
null, // handling?
|
||||
null, //shipping discounts?
|
||||
$discount
|
||||
);
|
||||
$amount = new Amount(
|
||||
$total,
|
||||
$breakdown
|
||||
);
|
||||
$amount = $this->amountFactory->fromWcOrder($order);
|
||||
$items = $this->itemFactory->fromWcOrder($order);
|
||||
$shipping = $this->shippingFactory->fromWcOrder($order);
|
||||
if (empty($shipping->address()->countryCode()) ||
|
||||
($shipping->address()->countryCode() && !$shipping->address()->postalCode())
|
||||
) {
|
||||
$shipping = null;
|
||||
}
|
||||
|
||||
$referenceId = 'default';
|
||||
$description = '';
|
||||
|
||||
//ToDo: We need to create a Payee.
|
||||
$payee = null;
|
||||
|
||||
$payee = $this->payeeRepository->payee();
|
||||
$customId = '';
|
||||
$invoiceId = '';
|
||||
$softDescriptor = '';
|
||||
|
@ -130,69 +58,13 @@ class PurchaseUnitFactory
|
|||
$invoiceId,
|
||||
$softDescriptor
|
||||
);
|
||||
|
||||
return $purchaseUnit;
|
||||
}
|
||||
|
||||
public function fromWcCart(\WC_Cart $cart) : PurchaseUnit
|
||||
{
|
||||
$currency = get_woocommerce_currency();
|
||||
$total = new Money((float) $cart->get_total('numeric'), $currency);
|
||||
$itemsTotal = $cart->get_cart_contents_total() + $cart->get_discount_total();
|
||||
$itemsTotal = new Money((float) $itemsTotal, $currency);
|
||||
$shipping = new Money(
|
||||
(float) $cart->get_shipping_total() + $cart->get_shipping_tax(),
|
||||
$currency
|
||||
);
|
||||
|
||||
$taxes = new Money((float) $cart->get_cart_contents_tax() + (float) $cart->get_discount_tax(), $currency);
|
||||
|
||||
$discount = null;
|
||||
if ($cart->get_discount_total()) {
|
||||
$discount = new Money(
|
||||
(float) $cart->get_discount_total() + $cart->get_discount_tax(),
|
||||
$currency
|
||||
);
|
||||
}
|
||||
//ToDo: Evaluate if more is needed? Fees?
|
||||
$breakdown = new AmountBreakdown(
|
||||
$itemsTotal,
|
||||
$shipping,
|
||||
$taxes,
|
||||
null, // insurance?
|
||||
null, // handling?
|
||||
null, //shipping discounts?
|
||||
$discount
|
||||
);
|
||||
$amount = new Amount(
|
||||
$total,
|
||||
$breakdown
|
||||
);
|
||||
$items = array_map(
|
||||
function (array $item) use ($currency): Item {
|
||||
$product = $item['data'];
|
||||
/**
|
||||
* @var \WC_Product $product
|
||||
*/
|
||||
$quantity = (int) $item['quantity'];
|
||||
|
||||
$price = (float) wc_get_price_including_tax($product);
|
||||
$priceWithoutTax = (float) wc_get_price_excluding_tax($product);
|
||||
$priceWithoutTaxRounded = round($priceWithoutTax, 2);
|
||||
$tax = round($price - $priceWithoutTaxRounded, 2);
|
||||
$tax = new Money($tax, $currency);
|
||||
return new Item(
|
||||
mb_substr($product->get_name(), 0, 127),
|
||||
new Money($priceWithoutTaxRounded, $currency),
|
||||
$quantity,
|
||||
mb_substr($product->get_description(), 0, 127),
|
||||
$tax,
|
||||
$product->get_sku(),
|
||||
($product->is_downloadable()) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
|
||||
);
|
||||
},
|
||||
$cart->get_cart_contents()
|
||||
);
|
||||
$amount = $this->amountFactory->fromWcCart($cart);
|
||||
$items = $this->itemFactory->fromWcCart($cart);
|
||||
|
||||
/**
|
||||
* // ToDo:
|
||||
|
@ -202,9 +74,10 @@ class PurchaseUnitFactory
|
|||
* Maybe discuss.
|
||||
*/
|
||||
$shipping = null;
|
||||
if (is_a(\WC()->customer, \WC_Customer::class)) {
|
||||
$customer = \WC()->customer;
|
||||
if (is_a($customer, \WC_Customer::class)) {
|
||||
$shipping = $this->shippingFactory->fromWcCustomer(\WC()->customer);
|
||||
if ($shipping->address()->countryCode() && !$shipping->address()->postalCode()) {
|
||||
if (! $shipping->address()->countryCode() || ($shipping->address()->countryCode() && !$shipping->address()->postalCode())) {
|
||||
$shipping = null;
|
||||
}
|
||||
}
|
||||
|
@ -212,8 +85,7 @@ class PurchaseUnitFactory
|
|||
$referenceId = 'default';
|
||||
$description = '';
|
||||
|
||||
//ToDo: We need to create a Payee.
|
||||
$payee = null;
|
||||
$payee = $this->payeeRepository->payee();
|
||||
|
||||
$customId = '';
|
||||
$invoiceId = '';
|
||||
|
|
|
@ -19,6 +19,7 @@ class ShippingFactory
|
|||
{
|
||||
// Replicates the Behavior of \WC_Order::get_formatted_shipping_full_name()
|
||||
$fullName = sprintf(
|
||||
// translators: %1$s is the first name and %2$s is the second name. wc translation.
|
||||
_x('%1$s %2$s', 'full name', 'woocommerce'),
|
||||
$customer->get_shipping_first_name(),
|
||||
$customer->get_shipping_last_name()
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Repository;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Config\Config;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payee;
|
||||
|
||||
class PayeeRepository
|
||||
{
|
||||
|
||||
private $config;
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function payee() : Payee
|
||||
{
|
||||
$merchantEmail = $this->config->get('merchant_email');
|
||||
$merchantId = ($this->config->has('merchant_id')) ? $this->config->get('merchant_id') : '';
|
||||
|
||||
return new Payee(
|
||||
$merchantEmail,
|
||||
$merchantId
|
||||
);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
|
||||
class AddressTest extends TestCase
|
||||
{
|
||||
|
|
|
@ -4,31 +4,32 @@ declare(strict_types=1);
|
|||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class AmountBreakdownTest extends TestCase
|
||||
{
|
||||
|
||||
public function test() {
|
||||
$itemTotal = \Mockery::mock(Money::class);
|
||||
$itemTotal = Mockery::mock(Money::class);
|
||||
$itemTotal
|
||||
->expects('toArray')->andReturn(['itemTotal']);
|
||||
$shipping = \Mockery::mock(Money::class);
|
||||
$shipping = Mockery::mock(Money::class);
|
||||
$shipping
|
||||
->expects('toArray')->andReturn(['shipping']);
|
||||
$taxTotal = \Mockery::mock(Money::class);
|
||||
$taxTotal = Mockery::mock(Money::class);
|
||||
$taxTotal
|
||||
->expects('toArray')->andReturn(['taxTotal']);
|
||||
$handling = \Mockery::mock(Money::class);
|
||||
$handling = Mockery::mock(Money::class);
|
||||
$handling
|
||||
->expects('toArray')->andReturn(['handling']);
|
||||
$insurance = \Mockery::mock(Money::class);
|
||||
$insurance = Mockery::mock(Money::class);
|
||||
$insurance
|
||||
->expects('toArray')->andReturn(['insurance']);
|
||||
$shippingDiscount = \Mockery::mock(Money::class);
|
||||
$shippingDiscount = Mockery::mock(Money::class);
|
||||
$shippingDiscount
|
||||
->expects('toArray')->andReturn(['shippingDiscount']);
|
||||
$discount = \Mockery::mock(Money::class);
|
||||
$discount = Mockery::mock(Money::class);
|
||||
$discount
|
||||
->expects('toArray')->andReturn(['discount']);
|
||||
$testee = new AmountBreakdown(
|
||||
|
@ -67,27 +68,27 @@ class AmountBreakdownTest extends TestCase
|
|||
*/
|
||||
public function testDropArrayKeyIfNoValueGiven($keyMissing, $methodName) {
|
||||
|
||||
$itemTotal = \Mockery::mock(Money::class);
|
||||
$itemTotal = Mockery::mock(Money::class);
|
||||
$itemTotal
|
||||
->expects('toArray')->andReturn(['itemTotal']);
|
||||
$shipping = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['itemTotal']);
|
||||
$shipping = Mockery::mock(Money::class);
|
||||
$shipping
|
||||
->expects('toArray')->andReturn(['shipping']);
|
||||
$taxTotal = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['shipping']);
|
||||
$taxTotal = Mockery::mock(Money::class);
|
||||
$taxTotal
|
||||
->expects('toArray')->andReturn(['taxTotal']);
|
||||
$handling = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['taxTotal']);
|
||||
$handling = Mockery::mock(Money::class);
|
||||
$handling
|
||||
->expects('toArray')->andReturn(['handling']);
|
||||
$insurance = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['handling']);
|
||||
$insurance = Mockery::mock(Money::class);
|
||||
$insurance
|
||||
->expects('toArray')->andReturn(['insurance']);
|
||||
$shippingDiscount = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['insurance']);
|
||||
$shippingDiscount = Mockery::mock(Money::class);
|
||||
$shippingDiscount
|
||||
->expects('toArray')->andReturn(['shippingDiscount']);
|
||||
$discount = \Mockery::mock(Money::class);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['shippingDiscount']);
|
||||
$discount = Mockery::mock(Money::class);
|
||||
$discount
|
||||
->expects('toArray')->andReturn(['discount']);
|
||||
->shouldReceive('toArray')->zeroOrMoreTimes()->andReturn(['discount']);
|
||||
|
||||
|
||||
$items = [
|
||||
|
|
|
@ -4,24 +4,25 @@ declare(strict_types=1);
|
|||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class AmountTest extends TestCase
|
||||
{
|
||||
|
||||
public function test() {
|
||||
$money = \Mockery::mock(Money::class);
|
||||
$money->expects('currencyCode')->andReturn('currencyCode');
|
||||
$money->expects('value')->andReturn(1.10);
|
||||
$money = Mockery::mock(Money::class);
|
||||
$money->shouldReceive('currencyCode')->andReturn('currencyCode');
|
||||
$money->shouldReceive('value')->andReturn(1.10);
|
||||
$testee = new Amount($money);
|
||||
|
||||
$this->assertEquals('currencyCode', $testee->currencyCode());
|
||||
$this->assertEquals(1.10, $testee->value());
|
||||
}
|
||||
public function testBreakdownIsNull() {
|
||||
$money = \Mockery::mock(Money::class);
|
||||
$money->expects('currencyCode')->andReturn('currencyCode');
|
||||
$money->expects('value')->andReturn(1.10);
|
||||
$money = Mockery::mock(Money::class);
|
||||
$money->shouldReceive('currencyCode')->andReturn('currencyCode');
|
||||
$money->shouldReceive('value')->andReturn(1.10);
|
||||
$testee = new Amount($money);
|
||||
|
||||
$this->assertNull($testee->breakdown());
|
||||
|
@ -33,11 +34,11 @@ class AmountTest extends TestCase
|
|||
$this->assertEquals($expectedArray, $testee->toArray());
|
||||
}
|
||||
public function testBreakdown() {
|
||||
$money = \Mockery::mock(Money::class);
|
||||
$money->expects('currencyCode')->andReturn('currencyCode');
|
||||
$money->expects('value')->andReturn(1.10);
|
||||
$breakdown = \Mockery::mock(AmountBreakdown::class);
|
||||
$breakdown->expects('toArray')->andReturn([1]);
|
||||
$money = Mockery::mock(Money::class);
|
||||
$money->shouldReceive('currencyCode')->andReturn('currencyCode');
|
||||
$money->shouldReceive('value')->andReturn(1.10);
|
||||
$breakdown = Mockery::mock(AmountBreakdown::class);
|
||||
$breakdown->shouldReceive('toArray')->andReturn([1]);
|
||||
$testee = new Amount($money, $breakdown);
|
||||
|
||||
$this->assertEquals($breakdown, $testee->breakdown());
|
||||
|
|
|
@ -3,19 +3,19 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class ErrorResponseCollectionTest extends TestCase
|
||||
{
|
||||
|
||||
public function testHasErrorCode() {
|
||||
$error1 = \Mockery::mock(ErrorResponse::class);
|
||||
$error1 = Mockery::mock(ErrorResponse::class);
|
||||
$error1
|
||||
->expects('code')
|
||||
->times(3)
|
||||
->andReturn('code-1');
|
||||
$error2 = \Mockery::mock(ErrorResponse::class);
|
||||
$error2 = Mockery::mock(ErrorResponse::class);
|
||||
$error2
|
||||
->expects('code')
|
||||
->times(3)
|
||||
|
@ -26,11 +26,11 @@ class ErrorResponseCollectionTest extends TestCase
|
|||
$this->assertFalse($testee->hasErrorCode('code-3'), 'code-3 should not return true');
|
||||
}
|
||||
public function testCodes() {
|
||||
$error1 = \Mockery::mock(ErrorResponse::class);
|
||||
$error1 = Mockery::mock(ErrorResponse::class);
|
||||
$error1
|
||||
->expects('code')
|
||||
->andReturn('code-1');
|
||||
$error2 = \Mockery::mock(ErrorResponse::class);
|
||||
$error2 = Mockery::mock(ErrorResponse::class);
|
||||
$error2
|
||||
->expects('code')
|
||||
->andReturn('code-2');
|
||||
|
@ -39,8 +39,8 @@ class ErrorResponseCollectionTest extends TestCase
|
|||
$this->assertEquals($expected, $testee->codes());
|
||||
}
|
||||
public function testErrors() {
|
||||
$error1 = \Mockery::mock(ErrorResponse::class);
|
||||
$error2 = \Mockery::mock(ErrorResponse::class);
|
||||
$error1 = Mockery::mock(ErrorResponse::class);
|
||||
$error2 = Mockery::mock(ErrorResponse::class);
|
||||
$testee = new ErrorResponseCollection($error1, $error2);
|
||||
|
||||
$errors = $testee->errors();
|
||||
|
|
|
@ -3,8 +3,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class ErrorResponseTest extends TestCase
|
||||
{
|
||||
|
|
|
@ -3,15 +3,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class ItemTest extends TestCase
|
||||
{
|
||||
|
||||
public function test() {
|
||||
$unitAmount = \Mockery::mock(Money::class);
|
||||
$tax = \Mockery::mock(Money::class);
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$testee = new Item(
|
||||
'name',
|
||||
$unitAmount,
|
||||
|
@ -32,8 +32,8 @@ class ItemTest extends TestCase
|
|||
}
|
||||
|
||||
public function testDigitalGoodsCategory() {
|
||||
$unitAmount = \Mockery::mock(Money::class);
|
||||
$tax = \Mockery::mock(Money::class);
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$testee = new Item(
|
||||
'name',
|
||||
$unitAmount,
|
||||
|
@ -49,11 +49,11 @@ class ItemTest extends TestCase
|
|||
|
||||
public function testToArray() {
|
||||
|
||||
$unitAmount = \Mockery::mock(Money::class);
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$unitAmount
|
||||
->expects('toArray')
|
||||
->andReturn([1]);
|
||||
$tax = \Mockery::mock(Money::class);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$tax
|
||||
->expects('toArray')
|
||||
->andReturn([2]);
|
||||
|
|
|
@ -3,8 +3,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
|
||||
class MoneyTest extends TestCase
|
||||
{
|
||||
|
|
|
@ -3,8 +3,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class PurchaseUnitTest extends TestCase
|
||||
|
@ -13,14 +12,14 @@ class PurchaseUnitTest extends TestCase
|
|||
public function test() {
|
||||
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amount->expects('breakdown')->andReturnNull();
|
||||
$amount->expects('toArray')->andReturn(['amount']);
|
||||
$amount->shouldReceive('breakdown')->andReturnNull();
|
||||
$amount->shouldReceive('toArray')->andReturn(['amount']);
|
||||
$item1 = Mockery::mock(Item::class);
|
||||
$item1->expects('toArray')->andReturn(['item1']);
|
||||
$item1->shouldReceive('toArray')->andReturn(['item1']);
|
||||
$item2 = Mockery::mock(Item::class);
|
||||
$item2->expects('toArray')->andReturn(['item2']);
|
||||
$item2->shouldReceive('toArray')->andReturn(['item2']);
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping->expects('toArray')->andReturn(['shipping']);
|
||||
$shipping->shouldReceive('toArray')->andReturn(['shipping']);
|
||||
|
||||
$testee = new PurchaseUnit(
|
||||
$amount,
|
||||
|
@ -382,34 +381,34 @@ class PurchaseUnitTest extends TestCase
|
|||
$items = [];
|
||||
foreach ($test['items'] as $key => $item) {
|
||||
$unitAmount = Mockery::mock(Money::class);
|
||||
$unitAmount->expects('value')->andReturn($item['value']);
|
||||
$unitAmount->shouldReceive('value')->andReturn($item['value']);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$tax->expects('value')->andReturn($item['tax']);
|
||||
$tax->shouldReceive('value')->andReturn($item['tax']);
|
||||
$items[$key] = Mockery::mock(Item::class);
|
||||
$items[$key]->expects('unitAmount')->andReturn($unitAmount);
|
||||
$items[$key]->expects('tax')->andReturn($tax);
|
||||
$items[$key]->expects('quantity')->andReturn($item['quantity']);
|
||||
$items[$key]->expects('toArray')->andReturn([]);
|
||||
$items[$key]->shouldReceive('unitAmount')->andReturn($unitAmount);
|
||||
$items[$key]->shouldReceive('tax')->andReturn($tax);
|
||||
$items[$key]->shouldReceive('quantity')->andReturn($item['quantity']);
|
||||
$items[$key]->shouldReceive('toArray')->andReturn([]);
|
||||
}
|
||||
$breakdown = null;
|
||||
if ($test['breakdown']) {
|
||||
$breakdown = Mockery::mock(AmountBreakdown::class);
|
||||
foreach ($test['breakdown'] as $method => $value) {
|
||||
$breakdown->expects($method)->andReturnUsing(function() use ($value) {
|
||||
$breakdown->shouldReceive($method)->andReturnUsing(function() use ($value) {
|
||||
if (! is_numeric($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$money = Mockery::mock(Money::class);
|
||||
$money->expects('value')->andReturn($value);
|
||||
$money->shouldReceive('value')->andReturn($value);
|
||||
return $money;
|
||||
});
|
||||
}
|
||||
}
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amount->expects('toArray')->andReturn(['value' => [], 'breakdown' => []]);
|
||||
$amount->expects('value')->andReturn($test['amount']);
|
||||
$amount->expects('breakdown')->andReturn($breakdown);
|
||||
$amount->shouldReceive('toArray')->andReturn(['value' => [], 'breakdown' => []]);
|
||||
$amount->shouldReceive('value')->andReturn($test['amount']);
|
||||
$amount->shouldReceive('breakdown')->andReturn($breakdown);
|
||||
|
||||
$values[$testKey] = [
|
||||
$items,
|
||||
|
@ -425,16 +424,16 @@ class PurchaseUnitTest extends TestCase
|
|||
public function testPayee() {
|
||||
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amount->expects('breakdown')->andReturnNull();
|
||||
$amount->expects('toArray')->andReturn(['amount']);
|
||||
$amount->shouldReceive('breakdown')->andReturnNull();
|
||||
$amount->shouldReceive('toArray')->andReturn(['amount']);
|
||||
$item1 = Mockery::mock(Item::class);
|
||||
$item1->expects('toArray')->andReturn(['item1']);
|
||||
$item1->shouldReceive('toArray')->andReturn(['item1']);
|
||||
$item2 = Mockery::mock(Item::class);
|
||||
$item2->expects('toArray')->andReturn(['item2']);
|
||||
$item2->shouldReceive('toArray')->andReturn(['item2']);
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping->expects('toArray')->andReturn(['shipping']);
|
||||
$shipping->shouldReceive('toArray')->andReturn(['shipping']);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payee->expects('toArray')->andReturn(['payee']);
|
||||
$payee->shouldReceive('toArray')->andReturn(['payee']);
|
||||
$testee = new PurchaseUnit(
|
||||
$amount,
|
||||
[],
|
||||
|
|
|
@ -0,0 +1,370 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Address;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Amount;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payee;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Shipping;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\TestCase;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use Mockery;
|
||||
|
||||
class PurchaseUnitFactoryTest extends TestCase
|
||||
{
|
||||
|
||||
public function testWcOrder() {
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->shouldReceive('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->shouldReceive('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->shouldReceive('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn([]);
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->shouldReceive('countryCode')
|
||||
->twice()
|
||||
->andReturn('DE');
|
||||
$address
|
||||
->shouldReceive('postalCode')
|
||||
->andReturn('12345');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->shouldReceive('address')
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->shouldReceive('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($shipping);
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcOrder($wcOrder);
|
||||
$this->assertTrue(is_a($unit, PurchaseUnit::class));
|
||||
$this->assertEquals($payee, $unit->payee());
|
||||
$this->assertEquals('', $unit->description());
|
||||
$this->assertEquals('default', $unit->referenceId());
|
||||
$this->assertEquals('', $unit->customId());
|
||||
$this->assertEquals('', $unit->softDescriptor());
|
||||
$this->assertEquals('', $unit->invoiceId());
|
||||
$this->assertEquals([], $unit->items());
|
||||
$this->assertEquals($amount, $unit->amount());
|
||||
$this->assertEquals($shipping, $unit->shipping());
|
||||
}
|
||||
|
||||
public function testWcOrderShippingGetsDroppedWhenNoPostalCode() {
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn([]);
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->expects('countryCode')
|
||||
->twice()
|
||||
->andReturn('DE');
|
||||
$address
|
||||
->expects('postalCode')
|
||||
->andReturn('');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->expects('address')
|
||||
->times(3)
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($shipping);
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcOrder($wcOrder);
|
||||
$this->assertEquals(null, $unit->shipping());
|
||||
}
|
||||
|
||||
public function testWcOrderShippingGetsDroppedWhenNoCountryCode() {
|
||||
|
||||
$wcOrder = Mockery::mock(\WC_Order::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn([]);
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->expects('countryCode')
|
||||
->andReturn('');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->expects('address')
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->expects('fromWcOrder')
|
||||
->with($wcOrder)
|
||||
->andReturn($shipping);
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcOrder($wcOrder);
|
||||
$this->assertEquals(null, $unit->shipping());
|
||||
}
|
||||
|
||||
public function testWcCartDefault()
|
||||
{
|
||||
|
||||
$wcCustomer = Mockery::mock(\WC_Customer::class);
|
||||
expect('WC')
|
||||
->andReturn((object) ['customer' => $wcCustomer]);
|
||||
|
||||
$wcCart = Mockery::mock(\WC_Cart::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn([]);
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->shouldReceive('countryCode')
|
||||
->andReturn('DE');
|
||||
$address
|
||||
->shouldReceive('postalCode')
|
||||
->andReturn('12345');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->shouldReceive('address')
|
||||
->zeroOrMoreTimes()
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->expects('fromWcCustomer')
|
||||
->with($wcCustomer)
|
||||
->andReturn($shipping);
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcCart($wcCart);
|
||||
$this->assertTrue(is_a($unit, PurchaseUnit::class));
|
||||
$this->assertEquals($payee, $unit->payee());
|
||||
$this->assertEquals('', $unit->description());
|
||||
$this->assertEquals('default', $unit->referenceId());
|
||||
$this->assertEquals('', $unit->customId());
|
||||
$this->assertEquals('', $unit->softDescriptor());
|
||||
$this->assertEquals('', $unit->invoiceId());
|
||||
$this->assertEquals([], $unit->items());
|
||||
$this->assertEquals($amount, $unit->amount());
|
||||
$this->assertEquals($shipping, $unit->shipping());
|
||||
}
|
||||
|
||||
public function testWcCartShippingGetsDroppendWhenNoCustomer() {
|
||||
|
||||
expect('WC')
|
||||
->andReturn((object) ['customer' => null]);
|
||||
|
||||
$wcCart = Mockery::mock(\WC_Cart::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn([]);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcCart($wcCart);
|
||||
$this->assertNull($unit->shipping());
|
||||
}
|
||||
public function testWcCartShippingGetsDroppendWhenNoPostalCode() {
|
||||
|
||||
expect('WC')
|
||||
->andReturn((object) ['customer' => Mockery::mock(\WC_Customer::class)]);
|
||||
|
||||
$wcCart = Mockery::mock(\WC_Cart::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn([]);
|
||||
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->shouldReceive('countryCode')
|
||||
->andReturn('DE');
|
||||
$address
|
||||
->shouldReceive('postalCode')
|
||||
->andReturn('');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->shouldReceive('address')
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->expects('fromWcCustomer')
|
||||
->andReturn($shipping);
|
||||
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcCart($wcCart);
|
||||
$this->assertNull($unit->shipping());
|
||||
}
|
||||
public function testWcCartShippingGetsDroppendWhenNoCountryCode() {
|
||||
|
||||
expect('WC')
|
||||
->andReturn((object) ['customer' => Mockery::mock(\WC_Customer::class)]);
|
||||
|
||||
$wcCart = Mockery::mock(\WC_Cart::class);
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amountFactory = Mockery::mock(AmountFactory::class);
|
||||
$amountFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn($amount);
|
||||
$payeeFactory = Mockery::mock(PayeeFactory::class);
|
||||
$payeeRepository = Mockery::mock(PayeeRepository::class);
|
||||
$payee = Mockery::mock(Payee::class);
|
||||
$payeeRepository
|
||||
->expects('payee')->andReturn($payee);
|
||||
$itemFactory = Mockery::mock(ItemFactory::class);
|
||||
$itemFactory
|
||||
->expects('fromWcCart')
|
||||
->with($wcCart)
|
||||
->andReturn([]);
|
||||
|
||||
|
||||
$address = Mockery::mock(Address::class);
|
||||
$address
|
||||
->shouldReceive('countryCode')
|
||||
->andReturn('');
|
||||
$shipping = Mockery::mock(Shipping::class);
|
||||
$shipping
|
||||
->shouldReceive('address')
|
||||
->andReturn($address);
|
||||
$shippingFactory = Mockery::mock(ShippingFactory::class);
|
||||
$shippingFactory
|
||||
->expects('fromWcCustomer')
|
||||
->andReturn($shipping);
|
||||
|
||||
$testee = new PurchaseUnitFactory(
|
||||
$amountFactory,
|
||||
$payeeRepository,
|
||||
$payeeFactory,
|
||||
$itemFactory,
|
||||
$shippingFactory
|
||||
);
|
||||
|
||||
$unit = $testee->fromWcCart($wcCart);
|
||||
$this->assertNull($unit->shipping());
|
||||
}
|
||||
|
||||
}
|
25
modules.local/ppcp-api-client/tests/PHPUnit/TestCase.php
Normal file
25
modules.local/ppcp-api-client/tests/PHPUnit/TestCase.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\ApiClient;
|
||||
|
||||
|
||||
use function Brain\Monkey\setUp;
|
||||
use function Brain\Monkey\tearDown;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use Mockery;
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
setUp();
|
||||
}
|
||||
public function tearDown(): void
|
||||
{
|
||||
tearDown();
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
}
|
|
@ -17,11 +17,13 @@ use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
|
|||
return [
|
||||
'button.smart-button' => function (ContainerInterface $container): SmartButtonInterface {
|
||||
$settings = $container->get('wcgateway.settings');
|
||||
$payeeRepository = $container->get('api.repository.payee');
|
||||
if (wc_string_to_bool($settings->get('enabled'))) {
|
||||
return new SmartButton(
|
||||
$container->get('button.url'),
|
||||
$container->get('session.handler'),
|
||||
$settings
|
||||
$settings,
|
||||
$payeeRepository
|
||||
);
|
||||
}
|
||||
return new DisabledSmartButton();
|
||||
|
@ -42,12 +44,12 @@ return [
|
|||
$cart = WC()->cart;
|
||||
$shipping = WC()->shipping();
|
||||
$requestData = $container->get('button.request-data');
|
||||
$repository = $container->get('api.cart-repository');
|
||||
$repository = $container->get('api.repository.cart');
|
||||
return new ChangeCartEndpoint($cart, $shipping, $requestData, $repository);
|
||||
},
|
||||
'button.endpoint.create-order' => function (ContainerInterface $container): CreateOrderEndpoint {
|
||||
$requestData = $container->get('button.request-data');
|
||||
$repository = $container->get('api.cart-repository');
|
||||
$repository = $container->get('api.repository.cart');
|
||||
$apiClient = $container->get('api.endpoint.order');
|
||||
return new CreateOrderEndpoint($requestData, $repository, $apiClient);
|
||||
},
|
||||
|
|
|
@ -3,6 +3,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Assets;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
|
||||
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
|
||||
|
@ -14,16 +15,19 @@ class SmartButton implements SmartButtonInterface
|
|||
private $moduleUrl;
|
||||
private $sessionHandler;
|
||||
private $settings;
|
||||
private $payeeRepository;
|
||||
|
||||
public function __construct(
|
||||
string $moduleUrl,
|
||||
SessionHandler $sessionHandler,
|
||||
Settings $settings
|
||||
Settings $settings,
|
||||
PayeeRepository $payeeRepository
|
||||
) {
|
||||
|
||||
$this->moduleUrl = $moduleUrl;
|
||||
$this->sessionHandler = $sessionHandler;
|
||||
$this->settings = $settings;
|
||||
$this->payeeRepository = $payeeRepository;
|
||||
}
|
||||
|
||||
public function renderWrapper(): bool
|
||||
|
@ -69,22 +73,16 @@ class SmartButton implements SmartButtonInterface
|
|||
$this->moduleUrl . '/assets/js/button.js'
|
||||
);
|
||||
|
||||
$params = [
|
||||
//ToDo: Add the correct client id, toggle when settings is set to sandbox
|
||||
'client-id' => 'AcVzowpNCpTxFzLG7onQI4JD0sVcA0BkZv-D42qRZPv_gZ8cNfX9zGL_8bXmSu7cbJ5B2DH7sot8vDpw',
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'locale' => get_user_locale(),
|
||||
//'debug' => (defined('WP_DEBUG') && WP_DEBUG) ? 'true' : 'false',
|
||||
//ToDo: Update date on releases.
|
||||
'integration-date' => date('Y-m-d'),
|
||||
'components' => 'marks,buttons',
|
||||
//ToDo: Probably only needed, when DCC
|
||||
'vault' => 'true',
|
||||
'commit' => is_checkout() ? 'true' : 'false',
|
||||
'intent' => $this->settings->get('intent')
|
||||
];
|
||||
$smartButtonUrl = add_query_arg($params, 'https://www.paypal.com/sdk/js');
|
||||
wp_localize_script(
|
||||
'paypal-smart-button',
|
||||
'PayPalCommerceGateway',
|
||||
$this->localizeScript()
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
private function localizeScript() : array
|
||||
{
|
||||
$localize = [
|
||||
'redirect' => wc_get_checkout_url(),
|
||||
'context' => $this->context(),
|
||||
|
@ -106,7 +104,7 @@ class SmartButton implements SmartButtonInterface
|
|||
'wrapper' => '#ppc-button',
|
||||
'mini_cart_wrapper' => '#ppc-button-minicart',
|
||||
'cancel_wrapper' => '#ppcp-cancel',
|
||||
'url' => $smartButtonUrl,
|
||||
'url' => $this->url(),
|
||||
'style' => [
|
||||
'layout' => 'vertical',
|
||||
'color' => $this->settings->get('button_color'),
|
||||
|
@ -115,12 +113,31 @@ class SmartButton implements SmartButtonInterface
|
|||
],
|
||||
],
|
||||
];
|
||||
wp_localize_script(
|
||||
'paypal-smart-button',
|
||||
'PayPalCommerceGateway',
|
||||
$localize
|
||||
);
|
||||
return true;
|
||||
return $localize;
|
||||
}
|
||||
|
||||
private function url() : string
|
||||
{
|
||||
$params = [
|
||||
//ToDo: Add the correct client id, toggle when settings is set to sandbox
|
||||
'client-id' => 'AcVzowpNCpTxFzLG7onQI4JD0sVcA0BkZv-D42qRZPv_gZ8cNfX9zGL_8bXmSu7cbJ5B2DH7sot8vDpw',
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'locale' => get_user_locale(),
|
||||
//'debug' => (defined('WP_DEBUG') && WP_DEBUG) ? 'true' : 'false',
|
||||
//ToDo: Update date on releases.
|
||||
'integration-date' => date('Y-m-d'),
|
||||
'components' => 'marks,buttons',
|
||||
//ToDo: Probably only needed, when DCC
|
||||
'vault' => 'true',
|
||||
'commit' => is_checkout() ? 'true' : 'false',
|
||||
'intent' => $this->settings->get('intent')
|
||||
];
|
||||
$payee = $this->payeeRepository->payee();
|
||||
if ($payee->merchantId()) {
|
||||
$params['merchant-id'] = $payee->merchantId();
|
||||
}
|
||||
$smartButtonUrl = add_query_arg($params, 'https://www.paypal.com/sdk/js');
|
||||
return $smartButtonUrl;
|
||||
}
|
||||
|
||||
private function context(): string
|
||||
|
|
|
@ -18,7 +18,7 @@ return [
|
|||
},
|
||||
'wcgateway.gateway' => function (ContainerInterface $container) : WcGateway {
|
||||
$sessionHandler = $container->get('session.handler');
|
||||
$cartRepository = $container->get('api.cart-repository');
|
||||
$cartRepository = $container->get('api.repository.cart');
|
||||
$endpoint = $container->get('api.endpoint.order');
|
||||
$orderFactory = $container->get('api.factory.order');
|
||||
$settingsFields = $container->get('wcgateway.settings.fields');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue