move bn code into session

This commit is contained in:
David Remer 2020-07-16 09:28:00 +03:00
parent e4a4ec3acb
commit 68866f8ef2
3 changed files with 41 additions and 3 deletions

View file

@ -92,7 +92,8 @@ return [
$repository = $container->get('api.repository.cart');
$apiClient = $container->get('api.endpoint.order');
$payerFactory = $container->get('api.factory.payer');
return new CreateOrderEndpoint($requestData, $repository, $apiClient, $payerFactory);
$sessionHandler = $container->get('session.handler');
return new CreateOrderEndpoint($requestData, $repository, $apiClient, $payerFactory, $sessionHandler);
},
'button.endpoint.approve-order' => static function (ContainerInterface $container): ApproveOrderEndpoint {
$requestData = $container->get('button.request-data');

View file

@ -8,6 +8,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
class CreateOrderEndpoint implements EndpointInterface
{
@ -18,17 +19,20 @@ class CreateOrderEndpoint implements EndpointInterface
private $repository;
private $apiEndpoint;
private $payerFactory;
private $sessionHandler;
public function __construct(
RequestData $requestData,
CartRepository $repository,
OrderEndpoint $apiEndpoint,
PayerFactory $payerFactory
PayerFactory $payerFactory,
SessionHandler $sessionHandler
) {
$this->requestData = $requestData;
$this->repository = $repository;
$this->apiEndpoint = $apiEndpoint;
$this->payerFactory = $payerFactory;
$this->sessionHandler = $sessionHandler;
}
public static function nonce(): string
@ -53,9 +57,12 @@ class CreateOrderEndpoint implements EndpointInterface
$payer = $this->payerFactory->fromPayPalResponse(json_decode(json_encode($data['payer'])));
}
$bnCode = isset($data['bn_code']) ? (string) $data['bn_code'] : '';
if ($bnCode) {
$this->sessionHandler->replaceBnCode($bnCode);
$this->apiEndpoint->withBnCode($bnCode);
}
$order = $this->apiEndpoint->createForPurchaseUnits(
$purchaseUnits,
$bnCode,
$payer
);
wp_send_json_success($order->toArray());

View file

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\Woocommerce\Logging\Logger\NullLogger;
use Inpsyde\Woocommerce\Logging\Logger\WooCommerceLogger;
use Psr\Container\ContainerInterface;
@ -32,6 +35,33 @@ return [
$settings = $container->get('wcgateway.settings');
return $settings->has('client_secret') ? (string) $settings->get('client_secret') : '';
},
'api.endpoint.order' => static function (ContainerInterface $container): OrderEndpoint {
$orderFactory = $container->get('api.factory.order');
$patchCollectionFactory = $container->get('api.factory.patch-collection-factory');
$logger = $container->get('woocommerce.logger.woocommerce');
/**
* @var SessionHandler $sessionHandler
*/
$sessionHandler = $container->get('session.handler');
$bnCode = $sessionHandler->bnCode();
/**
* @var Settings $settings
*/
$settings = $container->get('wcgateway.settings');
$intent = $settings->has('intent') && strtoupper((string) $settings->get('intent')) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
$applicationContextRepository = $container->get('api.repository.application-context');
return new OrderEndpoint(
$container->get('api.host'),
$container->get('api.bearer'),
$orderFactory,
$patchCollectionFactory,
$intent,
$logger,
$applicationContextRepository,
$bnCode
);
},
'woocommerce.logger.woocommerce' => function (ContainerInterface $container): LoggerInterface {
$settings = $container->get('wcgateway.settings');
if (! $settings->has('logging_enabled') || ! $settings->get('logging_enabled')) {