Skip to content

Commit

Permalink
Added payment source and supplementary data
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Mar 6, 2024
1 parent c970358 commit aa674e7
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 197 deletions.
106 changes: 90 additions & 16 deletions src/PayPal/Order/Builder/Payload/CreatePayPalOrderPayloadBuilder.php
Expand Up @@ -27,14 +27,23 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\AmountBreakdown;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\AmountWithBreakdown;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\ApplicationContextRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\CardAttributesRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\CardRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\CardSupplementaryDataRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\CardVerification;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\CreatePayPalOrderRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\ItemRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Level2CardProcessingDataRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Level3CardProcessingDataRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\LineItemRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Name;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\PayeeRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Payer;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\PaymentSourceRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\PurchaseUnitRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\ShippingRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\StoredPaymentSourceRequest;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\SupplementaryDataRequest;

class CreatePayPalOrderPayloadBuilder implements CreatePayPalOrderPayloadBuilderInterface
{
Expand All @@ -52,7 +61,12 @@ public function build(CartInterface $cart, $fundingSource, $data)
{
$this->data = $data;
$this->buildBaseNode();
$this->buildPurchaseUnitsNode();
$this->buildPurchaseUnitsNode($fundingSource);

if ($fundingSource === 'card') {
$this->buildCardPaymentSourceNode();
}

if (empty($this->data['ps_checkout']['isExpressCheckout']) && empty($this->data['ps_checkout']['isUpdate'])) {
$this->buildPayerNode();
}
Expand All @@ -67,19 +81,19 @@ public function build(CartInterface $cart, $fundingSource, $data)
/**
* Build payload without cart details
*/
public function buildMinimalPayload()
{
$this->buildBaseNode();
$this->buildPurchaseUnitsNode();

if (empty($this->data['ps_checkout']['isExpressCheckout']) && empty($this->data['ps_checkout']['isUpdate'])) {
$this->buildPayerNode();
}

if (empty($this->data['ps_checkout']['isUpdate'])) {
$this->buildApplicationContextNode();
}
}
// public function buildMinimalPayload()
// {
// $this->buildBaseNode();
// $this->buildPurchaseUnitsNode();
//
// if (empty($this->data['ps_checkout']['isExpressCheckout']) && empty($this->data['ps_checkout']['isUpdate'])) {
// $this->buildPayerNode();
// }
//
// if (empty($this->data['ps_checkout']['isUpdate'])) {
// $this->buildApplicationContextNode();
// }
// }

/**
* Build the basic payload
Expand Down Expand Up @@ -122,7 +136,7 @@ public function buildPayerNode()
(new Name())
->setGivenName(!empty($this->data['invoiceAddress']['firstname']) ? $this->data['invoiceAddress']['firstname'] : '')
->setSurname(!empty($this->data['invoiceAddress']['lastname']) ? $this->data['invoiceAddress']['lastname'] : '')
)
)
->setEmailAddress(!empty($this->data['customer']['email']) ? $this->data['customer']['email'] : '')
->setAddress($this->buildAddress($this->data['invoiceAddress'], $this->data['invoiceAddressCountry'], $this->data['invoiceAddressState']));

Expand Down Expand Up @@ -173,7 +187,12 @@ public function buildApplicationContextNode()
$this->payload->setApplicationContext($applicationContext);
}

public function buildPurchaseUnitsNode()
/**
* @param string $fundingSource
*
* @return void
*/
public function buildPurchaseUnitsNode($fundingSource)
{
$purchaseUnit = (new PurchaseUnitRequest())
->setPayee((new PayeeRequest())->setMerchantId($this->data['ps_checkout']['merchant_id']))
Expand All @@ -193,6 +212,10 @@ public function buildPurchaseUnitsNode()
$this->buildShippingNode($purchaseUnit);
}

if ($fundingSource === 'card') {
$this->buildSupplementaryDataNode($purchaseUnit);
}

$this->payload->setPurchaseUnits([$purchaseUnit]);
}

Expand Down Expand Up @@ -280,6 +303,57 @@ public function buildAmountBreakdownNode(PurchaseUnitRequest $purchaseUnit)
);
}

private function buildCardPaymentSourceNode()
{
$paymentSource = (new PaymentSourceRequest())
->setCard(
(new CardRequest())
->setName($this->data['invoiceAddress']['firstname'] . ' ' . $this->data['invoiceAddress']['lastname'])
->setBillingAddress($this->buildAddress($this->data['invoiceAddress'], $this->data['invoiceAddressCountry'], $this->data['invoiceAddressState']))
->setAttributes(
(new CardAttributesRequest())
->setVerification((new CardVerification())->setMethod($this->data['ps_checkout']['3DS']))
)
);

$this->payload->setPaymentSource($paymentSource);
}

private function buildSupplementaryDataNode(PurchaseUnitRequest $purchaseUnit)
{
$level3CardLineItems = array_map(function (ItemRequest $item) {
return (new LineItemRequest())
->setName($item->getName())
->setUnitAmount($item->getUnitAmount())
->setTax($item->getTax())
->setQuantity($item->getQuantity())
->setDescription($item->getDescription())
->setSku($item->getSku())
->setCategory($item->getCategory());
}, $purchaseUnit->getItems());

$supplementaryData = (new SupplementaryDataRequest())->setCard(
(new CardSupplementaryDataRequest())
->setLevel2(
(new Level2CardProcessingDataRequest())->setTaxTotal($purchaseUnit->getAmount()->getBreakdown()->getTaxTotal())
)
->setLevel3(
(new Level3CardProcessingDataRequest())
->setShippingAmount($purchaseUnit->getAmount()->getBreakdown()->getShipping())
->setDutyAmount(
(new Amount())
->setValue($purchaseUnit->getAmount()->getValue())
->setCurrencyCode($purchaseUnit->getAmount()->getCurrencyCode())
)
->setDiscountAmount($purchaseUnit->getAmount()->getBreakdown()->getDiscount())
->setShippingAddress($purchaseUnit->getShipping()->getAddress())
->setLineItems($level3CardLineItems)

Check failure on line 350 in src/PayPal/Order/Builder/Payload/CreatePayPalOrderPayloadBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Parameter #1 $line_items of method PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Level3CardProcessingDataRequest::setLineItems() expects array<PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\LineItemRequest>, array<PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\ItemRequest> given.
)
);

$purchaseUnit->setSupplementaryData($supplementaryData);
}

/**
* Get decimal to round correspondent to the payment currency used
* Advise from PayPal: Always round to 2 decimals except for HUF, JPY and TWD
Expand Down
12 changes: 9 additions & 3 deletions src/PayPal/Order/DTO/CardAttributesRequest.php
Expand Up @@ -46,11 +46,13 @@ public function getCustomer()
/**
* @param CustomerRequest $customer
*
* @return void
* @return $this
*/
public function setCustomer(CustomerRequest $customer)
{
$this->customer = $customer;

return $this;
}

/**
Expand All @@ -64,11 +66,13 @@ public function getVault()
/**
* @param VaultAttributesRequest $vault
*
* @return void
* @return $this
*/
public function setVault(VaultAttributesRequest $vault)
{
$this->vault = $vault;

return $this;
}

/**
Expand All @@ -82,10 +86,12 @@ public function getVerification()
/**
* @param CardVerification $verification
*
* @return void
* @return $this
*/
public function setVerification(CardVerification $verification)
{
$this->verification = $verification;

return $this;
}
}
24 changes: 18 additions & 6 deletions src/PayPal/Order/DTO/CardRequest.php
Expand Up @@ -58,11 +58,13 @@ public function getName()
/**
* @param string $name
*
* @return void
* @return $this
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
Expand All @@ -76,11 +78,13 @@ public function getBillingAddress()
/**
* @param AddressRequest $billing_address
*
* @return void
* @return $this
*/
public function setBillingAddress(AddressRequest $billing_address)
{
$this->billing_address = $billing_address;

return $this;
}

/**
Expand All @@ -94,11 +98,13 @@ public function getAttributes()
/**
* @param CardAttributesRequest $attributes
*
* @return void
* @return $this
*/
public function setAttributes(CardAttributesRequest $attributes)
{
$this->attributes = $attributes;

return $this;
}

/**
Expand All @@ -112,11 +118,13 @@ public function getVaultId()
/**
* @param string $vault_id
*
* @return void
* @return $this
*/
public function setVaultId($vault_id)
{
$this->vault_id = $vault_id;

return $this;
}

/**
Expand All @@ -130,11 +138,13 @@ public function getStoredCredentials()
/**
* @param CardStoredCredentialsRequest $stored_credentials
*
* @return void
* @return $this
*/
public function setStoredCredentials(CardStoredCredentialsRequest $stored_credentials)
{
$this->stored_credentials = $stored_credentials;

return $this;
}

/**
Expand All @@ -148,10 +158,12 @@ public function getExperienceContext()
/**
* @param CardExperienceContextRequest $experience_context
*
* @return void
* @return $this
*/
public function setExperienceContext(CardExperienceContextRequest $experience_context)
{
$this->experience_context = $experience_context;

return $this;
}
}
8 changes: 6 additions & 2 deletions src/PayPal/Order/DTO/CardSupplementaryDataRequest.php
Expand Up @@ -42,11 +42,13 @@ public function getLevel2()
/**
* @param Level2CardProcessingDataRequest $level_2
*
* @return void
* @return $this
*/
public function setLevel2(Level2CardProcessingDataRequest $level_2)
{
$this->level_2 = $level_2;

return $this;
}

/**
Expand All @@ -60,10 +62,12 @@ public function getLevel3()
/**
* @param Level3CardProcessingDataRequest $level_3
*
* @return void
* @return $this
*/
public function setLevel3(Level3CardProcessingDataRequest $level_3)
{
$this->level_3 = $level_3;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/PayPal/Order/DTO/CardVerification.php
Expand Up @@ -38,10 +38,12 @@ public function getMethod()
/**
* @param string $method
*
* @return void
* @return $this
*/
public function setMethod($method)
{
$this->method = $method;

return $this;
}
}
8 changes: 6 additions & 2 deletions src/PayPal/Order/DTO/Level2CardProcessingDataRequest.php
Expand Up @@ -42,11 +42,13 @@ public function getInvoiceId()
/**
* @param string $invoice_id
*
* @return void
* @return $this
*/
public function setInvoiceId($invoice_id)
{
$this->invoice_id = $invoice_id;

return $this;
}

/**
Expand All @@ -60,10 +62,12 @@ public function getTaxTotal()
/**
* @param Amount $tax_total
*
* @return void
* @return $this
*/
public function setTaxTotal(Amount $tax_total)
{
$this->tax_total = $tax_total;

return $this;
}
}

0 comments on commit aa674e7

Please sign in to comment.