resolve merge conflict

This commit is contained in:
David Remer 2020-04-14 08:24:45 +03:00
commit c7aa53a8e4
12 changed files with 157 additions and 36 deletions

View file

@ -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;
@ -19,6 +19,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ShippingFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
return [
@ -56,7 +57,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.
@ -73,14 +74,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

View file

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

View 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);
}
}

View file

@ -97,7 +97,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(

View file

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

View file

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

View file

@ -9,22 +9,26 @@ 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;
@ -93,8 +97,7 @@ class PurchaseUnitFactory
$referenceId = 'default';
$description = '';
//ToDo: We need to create a Payee.
$payee = null;
$payee = $this->payeeRepository->payee();
$customId = '';
$invoiceId = '';
@ -125,7 +128,10 @@ class PurchaseUnitFactory
$currency
);
$taxes = new Money((float) $cart->get_cart_contents_tax() + (float) $cart->get_discount_tax(), $currency);
$taxes = new Money(
(float) $cart->get_cart_contents_tax() + (float) $cart->get_discount_tax(),
$currency
);
$discount = null;
if ($cart->get_discount_total()) {
@ -192,8 +198,7 @@ class PurchaseUnitFactory
$referenceId = 'default';
$description = '';
//ToDo: We need to create a Payee.
$payee = null;
$payee = $this->payeeRepository->payee();
$customId = '';
$invoiceId = '';

View file

@ -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()

View file

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

View file

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

View file

@ -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,21 +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',
];
$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(),
@ -105,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'),
@ -114,12 +113,30 @@ 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',
];
$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

View file

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