Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAYSHIP-2647] Payment source and supplementary data in order payload builder #1193

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class CardFieldsComponent extends BaseComponent {
.postCreateOrder({
...data,
fundingSource: this.data.name,
isCardFields: true,
isHostedFields: true,
// vault: storeCardInVault
})
.then(data => {
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/check.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function postProcess()

if (false === empty($psCheckoutCart->paypal_order)) {
$paypalOrder = new CreatePaypalOrderHandler($this->context);
$response = $paypalOrder->handle($isExpressCheckout || empty($this->context->cart->id_address_delivery), true, $psCheckoutCart->paypal_order);
$response = $paypalOrder->handle($isExpressCheckout || empty($this->context->cart->id_address_delivery), $psCheckoutCart->paypal_funding === 'card', true, $psCheckoutCart->paypal_order);

if (false === $response['status']) {
$this->module->getLogger()->error(
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function postProcess()
}

$paypalOrder = new CreatePaypalOrderHandler($this->context);
$response = $paypalOrder->handle($isExpressCheckout);
$response = $paypalOrder->handle($isExpressCheckout, isset($bodyValues['fundingSource']) && $bodyValues['fundingSource'] === 'card');

if (false === $response['status']) {
throw new PsCheckoutException($response['exceptionMessage'], (int) $response['exceptionCode']);
Expand Down
90 changes: 73 additions & 17 deletions src/Builder/Payload/CreateOrderPayloadBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function buildFullPayload()
if (empty($this->data['ps_checkout']['isUpdate'])) {
$this->buildApplicationContextNode();
}

if ($this->data['ps_checkout']['isCard']) {
$this->buildPaymentSourceNode();
$this->buildSupplementaryDataNode();
}
}

/**
Expand Down Expand Up @@ -127,14 +132,7 @@ public function buildShippingNode()
. (!empty($this->data['deliveryAddress']['lastname']) ? $this->data['deliveryAddress']['lastname'] : '')
),
],
'address' => [
'address_line_1' => !empty($this->data['deliveryAddress']['address1']) ? $this->data['deliveryAddress']['address1'] : '',
'address_line_2' => !empty($this->data['deliveryAddress']['address2']) ? $this->data['deliveryAddress']['address2'] : '',
'admin_area_1' => !empty($this->data['deliveryAddressState']['name']) ? $this->data['deliveryAddressState']['name'] : '',
'admin_area_2' => !empty($this->data['deliveryAddress']['city']) ? $this->data['deliveryAddress']['city'] : '',
'country_code' => !empty($this->data['deliveryAddressCountry']['iso_code']) ? $this->data['deliveryAddressCountry']['iso_code'] : '',
'postal_code' => !empty($this->data['deliveryAddress']['postcode']) ? $this->data['deliveryAddress']['postcode'] : '',
],
'address' => $this->getAddressPortable('deliveryAddress'),
];

$this->getPayload()->addAndMergeItems($node);
Expand All @@ -151,14 +149,7 @@ public function buildPayerNode()
'surname' => !empty($this->data['invoiceAddress']['lastname']) ? $this->data['invoiceAddress']['lastname'] : '',
],
'email_address' => !empty($this->data['customer']['email']) ? $this->data['customer']['email'] : '',
'address' => [
'address_line_1' => !empty($this->data['invoiceAddress']['address1']) ? $this->data['invoiceAddress']['address1'] : '',
'address_line_2' => !empty($this->data['invoiceAddress']['address2']) ? $this->data['invoiceAddress']['address2'] : '',
'admin_area_1' => !empty($this->data['invoiceAddressState']['name']) ? $this->data['invoiceAddressState']['name'] : '',
'admin_area_2' => !empty($this->data['invoiceAddress']['city']) ? $this->data['invoiceAddress']['city'] : '',
'country_code' => !empty($this->data['invoiceAddressCountry']['iso_code']) ? $this->data['invoiceAddressCountry']['iso_code'] : '',
'postal_code' => !empty($this->data['invoiceAddress']['postcode']) ? $this->data['invoiceAddress']['postcode'] : '',
],
'address' => $this->getAddressPortable('invoiceAddress'),
];

// Add optional birthdate if provided
Expand Down Expand Up @@ -282,6 +273,71 @@ public function buildAmountBreakdownNode()
$this->getPayload()->addAndMergeItems($node);
}

private function buildPaymentSourceNode()
{
$node = [
'payment_source' => [
'card' => [
'name' => $this->data['invoiceAddress']['firstname'] . ' ' . $this->data['invoiceAddress']['lastname'],
'billing_address' => $this->getAddressPortable('invoiceAddress'),
'attributes' => [
'verification' => [
'method' => $this->data['ps_checkout']['3DS'],
],
],
],
],
];

$this->getPayload()->addAndMergeItems($node);
}

private function buildSupplementaryDataNode()
{
$payload = $this->getPayload()->getArray();
$node = [
'supplementary_data' => [
'card' => [
'level_2' => [
// 'invoice_id' => '',
'tax_total' => $payload['amount']['breakdown']['tax_total'],
],
'level_3' => [
'shipping_amount' => $payload['amount']['breakdown']['shipping'],
'duty_amount' => [
'currency_code' => $payload['amount']['currency_code'],
'value' => $payload['amount']['value'],
],
'discount_amount' => $payload['amount']['breakdown']['discount'],
'shipping_address' => $this->getAddressPortable('deliveryAddress'),
'line_items' => $payload['items'],
],
],
],
];

$this->getPayload()->addAndMergeItems($node);
}

/**
* @param "deliveryAddress"|"invoiceAddress" $addressType
*
* @return string[]
*/
private function getAddressPortable($addressType)
{
$address = $this->data[$addressType];

return [
'address_line_1' => !empty($address['address1']) ? $address['address1'] : '',
'address_line_2' => !empty($address['address2']) ? $address['address2'] : '',
'admin_area_1' => !empty($this->data["{$addressType}State"]['name']) ? $this->data["{$addressType}State"]['name'] : '',
'admin_area_2' => !empty($address['city']) ? $address['city'] : '',
'country_code' => !empty($this->data["{$addressType}Country"]['iso_code']) ? $this->data["{$addressType}Country"]['iso_code'] : '',
'postal_code' => !empty($address['postcode']) ? $address['postcode'] : '',
];
}

/**
* 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 All @@ -305,7 +361,7 @@ private function getNbDecimalToRound()
*/
private function formatAmount($amount)
{
return sprintf("%01.{$this->getNbDecimalToRound()}f", $amount);
return sprintf("%01.{$this->getNbDecimalToRound()}F", $amount);
}

/**
Expand Down
127 changes: 106 additions & 21 deletions src/Builder/Payload/OrderPayloadBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class OrderPayloadBuilder extends Builder implements PayloadBuilderInterface
*/
private $isPatch;

/**
* @var bool
*/
private $isCard = false;

/**
* @param array $cart
* @param bool $isPatch
Expand Down Expand Up @@ -102,6 +107,11 @@ public function buildFullPayload()
if (false === $this->isUpdate) {
$this->buildApplicationContextNode();
}

if ($this->isCard) {
$this->buildPaymentSourceNode();
$this->buildSupplementaryDataNode();
}
}

/**
Expand All @@ -128,6 +138,11 @@ public function buildMinimalPayload()
if (false === $this->isUpdate) {
$this->buildApplicationContextNode();
}

if ($this->isCard) {
$this->buildPaymentSourceNode();
$this->buildSupplementaryDataNode();
}
}

/**
Expand Down Expand Up @@ -190,24 +205,14 @@ public function buildBaseNode()
*/
public function buildShippingNode()
{
$countryCodeMatrice = new PaypalCountryCodeMatrice();
$shippingCountryIsoCode = $this->getCountryIsoCodeById($this->cart['addresses']['shipping']->id_country);

$gender = new \Gender($this->cart['customer']->id_gender, $this->cart['language']->id);
$genderName = $gender->name;

$node['shipping'] = [
'name' => [
'full_name' => $genderName . ' ' . $this->cart['addresses']['shipping']->lastname . ' ' . $this->cart['addresses']['shipping']->firstname,
],
'address' => [
'address_line_1' => (string) $this->cart['addresses']['shipping']->address1,
'address_line_2' => (string) $this->cart['addresses']['shipping']->address2,
'admin_area_1' => (string) $this->getStateNameById($this->cart['addresses']['shipping']->id_state),
'admin_area_2' => (string) $this->cart['addresses']['shipping']->city,
'country_code' => (string) $countryCodeMatrice->getPaypalIsoCode($shippingCountryIsoCode),
'postal_code' => (string) $this->cart['addresses']['shipping']->postcode,
],
'address' => $this->getAddressPortable('shipping'),
];

$this->getPayload()->addAndMergeItems($node);
Expand All @@ -218,7 +223,6 @@ public function buildShippingNode()
*/
public function buildPayerNode()
{
$countryCodeMatrice = new PaypalCountryCodeMatrice();
$payerCountryIsoCode = $this->getCountryIsoCodeById($this->cart['addresses']['invoice']->id_country);
/** @var \Ps_checkout $module */
$module = \Module::getInstanceByName('ps_checkout');
Expand All @@ -229,14 +233,7 @@ public function buildPayerNode()
'surname' => (string) $this->cart['addresses']['invoice']->lastname,
],
'email_address' => (string) $this->cart['customer']->email,
'address' => [
'address_line_1' => (string) $this->cart['addresses']['invoice']->address1,
'address_line_2' => (string) $this->cart['addresses']['invoice']->address2,
'admin_area_1' => (string) $this->getStateNameById($this->cart['addresses']['invoice']->id_state), //The highest level sub-division in a country, which is usually a province, state, or ISO-3166-2 subdivision.
'admin_area_2' => (string) $this->cart['addresses']['invoice']->city, // A city, town, or village. Smaller than admin_area_level_1
'country_code' => (string) $countryCodeMatrice->getPaypalIsoCode($payerCountryIsoCode),
'postal_code' => (string) $this->cart['addresses']['invoice']->postcode,
],
'address' => $this->getAddressPortable('invoice'),
];

// Add optional birthdate if provided
Expand Down Expand Up @@ -406,6 +403,78 @@ public function buildAmountBreakdownNode()
$this->getPayload()->addAndMergeItems($node);
}

private function buildPaymentSourceNode()
{
/** @var \Ps_checkout $module */
$module = \Module::getInstanceByName('ps_checkout');
/** @var PayPalConfiguration $paypalConfiguration */
$paypalConfiguration = $module->getService('ps_checkout.paypal.configuration');

$node = [
'payment_source' => [
'card' => [
'name' => $this->cart['addresses']['invoice']->firstname . ' ' . $this->cart['addresses']['invoice']->lastname,
'billing_address' => $this->getAddressPortable('invoice'),
'attributes' => [
'verification' => [
'method' => $paypalConfiguration->getHostedFieldsContingencies(),
],
],
],
],
];

$this->getPayload()->addAndMergeItems($node);
}

private function buildSupplementaryDataNode()
{
$payload = $this->getPayload()->getArray();
$node = [
'supplementary_data' => [
'card' => [
'level_2' => [
// 'invoice_id' => '',
'tax_total' => $payload['amount']['breakdown']['tax_total'],
],
'level_3' => [
'shipping_amount' => $payload['amount']['breakdown']['shipping'],
'duty_amount' => [
'currency_code' => $payload['amount']['currency_code'],
'value' => $payload['amount']['value'],
],
'discount_amount' => $payload['amount']['breakdown']['discount'],
'shipping_address' => $this->getAddressPortable('shipping'),
'line_items' => $payload['items'],
],
],
],
];

$this->getPayload()->addAndMergeItems($node);
}

/**
* @param "shipping"|"invoice" $addressType
*
* @return string[]
*/
private function getAddressPortable($addressType)
{
$countryCodeMatrice = new PaypalCountryCodeMatrice();
$address = $this->cart['addresses'][$addressType];
$payerCountryIsoCode = $this->getCountryIsoCodeById($address->id_country);

return [
'address_line_1' => $address->address1,
'address_line_2' => $address->address2,
'admin_area_1' => (string) $this->getStateNameById($address->id_state),
'admin_area_2' => $address->city,
'country_code' => (string) $countryCodeMatrice->getPaypalIsoCode($payerCountryIsoCode),
'postal_code' => $address->postcode,
];
}

/**
* Function that allow to truncate fields to match the
* paypal api requirements
Expand Down Expand Up @@ -447,7 +516,7 @@ private function getNbDecimalToRound()
*/
private function formatAmount($amount)
{
return sprintf("%01.{$this->getNbDecimalToRound()}f", $amount);
return sprintf("%01.{$this->getNbDecimalToRound()}F", $amount);
}

/**
Expand Down Expand Up @@ -514,6 +583,22 @@ public function setPaypalOrderId($id)
$this->paypalOrderId = $id;
}

/**
* @return bool
*/
public function isCard()
{
return $this->isCard;
}

/**
* @param bool $isCard
*/
public function setIsCard($isCard)
{
$this->isCard = $isCard;
}

/**
* Getter $paypalOrderId
*/
Expand Down
Loading
Loading