Add Payments to the PurchaseUnit

This commit is contained in:
Mészáros Róbert 2020-04-14 19:18:12 +03:00
parent 6c1dec1c3a
commit 7d0e3d2e38
5 changed files with 97 additions and 10 deletions

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient;
@ -11,13 +12,14 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AddressFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AmountFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationsFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\AuthorizationFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ErrorResponseCollectionFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ItemFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayeeFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PaymentsFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use Inpsyde\PayPalCommerce\ApiClient\Factory\ShippingFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
@ -48,7 +50,7 @@ return [
$container->get('api.secret')
);
},
'api.endpoint.payments' => function (ContainerInterface $container) : PaymentsEndpoint {
'api.endpoint.payments' => function (ContainerInterface $container): PaymentsEndpoint {
$authorizationFactory = $container->get('api.factory.authorization');
$errorResponseFactory = $container->get('api.factory.response-error');
@ -59,7 +61,7 @@ return [
$errorResponseFactory
);
},
'api.endpoint.order' => function (ContainerInterface $container) : OrderEndpoint {
'api.endpoint.order' => function (ContainerInterface $container): OrderEndpoint {
$orderFactory = $container->get('api.factory.order');
$patchCollectionFactory = $container->get('api.factory.patch-collection-factory');
$errorResponseFactory = $container->get('api.factory.response-error');
@ -110,12 +112,15 @@ return [
$payeeFactory = $container->get('api.factory.payee');
$itemFactory = $container->get('api.factory.item');
$shippingFactory = $container->get('api.factory.shipping');
$paymentsFactory = $container->get('api.factory.payments');
return new PurchaseUnitFactory(
$amountFactory,
$payeeRepository,
$payeeFactory,
$itemFactory,
$shippingFactory
$shippingFactory,
$paymentsFactory
);
},
'api.factory.patch-collection-factory' => function (ContainerInterface $container)
@ -151,7 +156,11 @@ return [
$payerFactory = $container->get('api.factory.payer');
return new OrderFactory($purchaseUnitFactory, $payerFactory);
},
'api.factory.authorization' => function (ContainerInterface $container) : AuthorizationsFactory {
return new AuthorizationsFactory();
'api.factory.payments' => function (ContainerInterface $container): PaymentsFactory {
$authorizationFactory = $container->get('api.factory.authorization');
return new PaymentsFactory($authorizationFactory);
},
'api.factory.authorization' => function (ContainerInterface $container): AuthorizationFactory {
return new AuthorizationFactory();
},
];

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
class Payments
{
private $authorizations;
public function __construct(array $authorizations)
{
$this->authorizations = $authorizations;
}
public function toArray()
{
return [
];
}
public function authorizations() : array
{
return $this->authorizations;
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Entity;
@ -15,6 +16,8 @@ class PurchaseUnit
private $customId;
private $invoiceId;
private $softDescriptor;
private $payments;
public function __construct(
Amount $amount,
array $items = [],
@ -24,7 +27,8 @@ class PurchaseUnit
Payee $payee = null,
string $customId = '',
string $invoiceId = '',
string $softDescriptor = ''
string $softDescriptor = '',
Payments $payments = null
) {
$this->amount = $amount;
@ -43,6 +47,7 @@ class PurchaseUnit
$this->customId = $customId;
$this->invoiceId = $invoiceId;
$this->softDescriptor = $softDescriptor;
$this->payments = $payments;
}
public function amount() : Amount
@ -114,6 +119,10 @@ class PurchaseUnit
$purchaseUnit['payee'] = $this->payee()->toArray();
}
if ($this->payments()) {
$purchaseUnit['payments'] = $this->payments()->toArray();
}
if ($this->shipping()) {
$purchaseUnit['shipping'] = $this->shipping()->toArray();
}

View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Authorization;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Payments;
class PaymentsFactory
{
private $authorizationsFactory;
public function __construct(
AuthorizationFactory $authorizationsFactory
) {
$this->authorizationsFactory = $authorizationsFactory;
}
public function fromPayPalResponse(\stdClass $data)
{
$authorizations = array_map(
function (\stdClass $authorization): Authorization {
return $this->authorizationsFactory->fromPayPalRequest($authorization);
},
$data->authorizations
);
$payments = new Payments($authorizations);
return $payments;
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\ApiClient\Factory;
@ -16,12 +17,15 @@ class PurchaseUnitFactory
private $payeeFactory;
private $itemFactory;
private $shippingFactory;
private $paymentsFactory;
public function __construct(
AmountFactory $amountFactory,
PayeeRepository $payeeRepository,
PayeeFactory $payeeFactory,
ItemFactory $itemFactory,
ShippingFactory $shippingFactory
ShippingFactory $shippingFactory,
PaymentsFactory $paymentsFactory
) {
$this->amountFactory = $amountFactory;
@ -29,6 +33,7 @@ class PurchaseUnitFactory
$this->payeeFactory = $payeeFactory;
$this->itemFactory = $itemFactory;
$this->shippingFactory = $shippingFactory;
$this->$paymentsFactory = $paymentsFactory;
}
public function fromWcOrder(\WC_Order $order) : PurchaseUnit
@ -129,7 +134,11 @@ class PurchaseUnitFactory
$shipping = isset($data->shipping) ?
$this->shippingFactory->fromPayPalResponse($data->shipping)
: null;
return new PurchaseUnit(
$payments = isset($data->payments) ?
$this->paymentsFactory->fromPayPalResponse($data->payments) :
null;
$purchaseUnit = new PurchaseUnit(
$amount,
$items,
$shipping,
@ -138,7 +147,9 @@ class PurchaseUnitFactory
$payee,
$customId,
$invoiceId,
$softDescriptor
$softDescriptor,
$payments
);
return $purchaseUnit;
}
}