diff --git a/modules.local/ppcp-api-client/services.php b/modules.local/ppcp-api-client/services.php index 4c2312000..d597993db 100644 --- a/modules.local/ppcp-api-client/services.php +++ b/modules.local/ppcp-api-client/services.php @@ -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 diff --git a/modules.local/ppcp-api-client/src/Authentication/Bearer.php b/modules.local/ppcp-api-client/src/Authentication/Bearer.php index 2307cbb60..d485fdb43 100644 --- a/modules.local/ppcp-api-client/src/Authentication/Bearer.php +++ b/modules.local/ppcp-api-client/src/Authentication/Bearer.php @@ -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; } diff --git a/modules.local/ppcp-api-client/src/Config/Config.php b/modules.local/ppcp-api-client/src/Config/Config.php new file mode 100644 index 000000000..0b8ba8714 --- /dev/null +++ b/modules.local/ppcp-api-client/src/Config/Config.php @@ -0,0 +1,39 @@ + '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); + } +} diff --git a/modules.local/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules.local/ppcp-api-client/src/Endpoint/OrderEndpoint.php index 4872bec59..9699fb1e2 100644 --- a/modules.local/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules.local/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -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( diff --git a/modules.local/ppcp-api-client/src/Entity/Payee.php b/modules.local/ppcp-api-client/src/Entity/Payee.php index ed222466d..3b4429b3e 100644 --- a/modules.local/ppcp-api-client/src/Entity/Payee.php +++ b/modules.local/ppcp-api-client/src/Entity/Payee.php @@ -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; } } diff --git a/modules.local/ppcp-api-client/src/Exception/NotFoundException.php b/modules.local/ppcp-api-client/src/Exception/NotFoundException.php new file mode 100644 index 000000000..aca6fa24e --- /dev/null +++ b/modules.local/ppcp-api-client/src/Exception/NotFoundException.php @@ -0,0 +1,12 @@ +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 = ''; diff --git a/modules.local/ppcp-api-client/src/Factory/ShippingFactory.php b/modules.local/ppcp-api-client/src/Factory/ShippingFactory.php index 0167ce142..37de41642 100644 --- a/modules.local/ppcp-api-client/src/Factory/ShippingFactory.php +++ b/modules.local/ppcp-api-client/src/Factory/ShippingFactory.php @@ -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() diff --git a/modules.local/ppcp-api-client/src/Repository/PayeeRepository.php b/modules.local/ppcp-api-client/src/Repository/PayeeRepository.php new file mode 100644 index 000000000..5f46b745c --- /dev/null +++ b/modules.local/ppcp-api-client/src/Repository/PayeeRepository.php @@ -0,0 +1,28 @@ +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 + ); + } +} diff --git a/modules.local/ppcp-button/services.php b/modules.local/ppcp-button/services.php index 10933dc12..78fe3e2ac 100644 --- a/modules.local/ppcp-button/services.php +++ b/modules.local/ppcp-button/services.php @@ -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); }, diff --git a/modules.local/ppcp-button/src/Assets/SmartButton.php b/modules.local/ppcp-button/src/Assets/SmartButton.php index aaaaef3cc..482fdf293 100644 --- a/modules.local/ppcp-button/src/Assets/SmartButton.php +++ b/modules.local/ppcp-button/src/Assets/SmartButton.php @@ -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 diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index 7bfd23152..fb4a45680 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -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');