update
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
|
||||
class PaynowPaymentDataBuilder
|
||||
{
|
||||
private const MAX_ORDER_ITEM_NAME_LENGTH = 120;
|
||||
private $context;
|
||||
|
||||
/**
|
||||
@@ -94,6 +95,7 @@ class PaynowPaymentDataBuilder
|
||||
): array {
|
||||
$currency = Currency::getCurrency($id_currency);
|
||||
$customer = new Customer((int)$id_customer);
|
||||
$paymentMethodId = Tools::getValue('paymentMethodId');
|
||||
|
||||
$request = [
|
||||
'amount' => number_format($total_to_paid * 100, 0, '', ''),
|
||||
@@ -114,8 +116,68 @@ class PaynowPaymentDataBuilder
|
||||
)
|
||||
];
|
||||
|
||||
if (! empty(Tools::getValue('paymentMethodId'))) {
|
||||
$request['paymentMethodId'] = (int)Tools::getValue('paymentMethodId');
|
||||
try {
|
||||
$address = new Address($this->context->cart->id_address_delivery);
|
||||
$invoiceAddress = new Address($this->context->cart->id_address_invoice);
|
||||
|
||||
try {
|
||||
$state = new State($address->id_state);
|
||||
} catch (Throwable $e) {
|
||||
$state = null;
|
||||
}
|
||||
|
||||
try {
|
||||
$invoiceState = new State($invoiceAddress->id_state);
|
||||
} catch (Throwable $e) {
|
||||
$invoiceState = null;
|
||||
}
|
||||
|
||||
try {
|
||||
$country = Country::getIsoById($address->id_country);
|
||||
} catch (Throwable $e) {
|
||||
$country = null;
|
||||
}
|
||||
|
||||
try {
|
||||
$invoiceCountry = Country::getIsoById($invoiceAddress->id_country);
|
||||
} catch (Throwable $e) {
|
||||
$invoiceCountry = null;
|
||||
}
|
||||
|
||||
$request['buyer']['address'] = [
|
||||
'billing' => [
|
||||
'street' => $invoiceAddress->address1,
|
||||
'houseNumber' => $invoiceAddress->address2,
|
||||
'apartmentNumber' => '',
|
||||
'zipcode' => $invoiceAddress->postcode,
|
||||
'city' => $invoiceAddress->city,
|
||||
'county' => $invoiceState ? $invoiceState->name : '',
|
||||
'country' => $invoiceCountry ?: '',
|
||||
],
|
||||
'shipping' => [
|
||||
'street' => $address->address1,
|
||||
'houseNumber' => $address->address2,
|
||||
'apartmentNumber' => '',
|
||||
'zipcode' => $address->postcode,
|
||||
'city' => $address->city,
|
||||
'county' => $state ? $state->name : '',
|
||||
'country' => $country ?: '',
|
||||
]
|
||||
];
|
||||
} catch (Throwable $exception) {
|
||||
PaynowLogger::error('Cannot add addresses to payment data', ['msg' => $exception->getMessage()]);
|
||||
}
|
||||
|
||||
if (!empty($id_customer) && $this->context->customer){
|
||||
if (method_exists($this->context->customer, 'isGuest') && !$this->context->customer->isGuest()) {
|
||||
$request['buyer']['externalId'] = PaynowKeysGenerator::generateBuyerExternalId($id_customer, $this->module);
|
||||
} elseif ($this->context->customer->is_guest === '0') {
|
||||
$request['buyer']['externalId'] = PaynowKeysGenerator::generateBuyerExternalId($id_customer, $this->module);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($paymentMethodId)) {
|
||||
$request['paymentMethodId'] = (int)$paymentMethodId;
|
||||
}
|
||||
|
||||
if (Configuration::get('PAYNOW_PAYMENT_VALIDITY_TIME_ENABLED')) {
|
||||
@@ -126,12 +188,20 @@ class PaynowPaymentDataBuilder
|
||||
$request['authorizationCode'] = (int)preg_replace('/\s+/', '', Tools::getValue('blikCode'));
|
||||
}
|
||||
|
||||
if (!empty(Tools::getValue('paymentMethodToken'))) {
|
||||
$request['paymentMethodToken'] = Tools::getValue('paymentMethodToken');
|
||||
}
|
||||
|
||||
if (! empty(Tools::getValue('paymentMethodFingerprint'))) {
|
||||
$request['buyer']['deviceFingerprint'] = Tools::getValue('paymentMethodFingerprint');
|
||||
}
|
||||
|
||||
if (Configuration::get('PAYNOW_SEND_ORDER_ITEMS')) {
|
||||
$products = $this->context->cart->getProducts(true);
|
||||
$order_items = [];
|
||||
foreach ($products as $product) {
|
||||
$order_items[] = [
|
||||
'name' => $product['name'],
|
||||
'name' => self::truncateOrderItemName($product['name']),
|
||||
'category' => $this->getCategoriesNames($product['id_category_default']),
|
||||
'quantity' => $product['quantity'],
|
||||
'price' => number_format($product['price'] * 100, 0, '', '')
|
||||
@@ -161,4 +231,15 @@ class PaynowPaymentDataBuilder
|
||||
}
|
||||
return implode(", ", $categoriesNames);
|
||||
}
|
||||
|
||||
public static function truncateOrderItemName(string $name): string
|
||||
{
|
||||
$name = trim($name);
|
||||
|
||||
if(strlen($name) <= self::MAX_ORDER_ITEM_NAME_LENGTH) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
return substr($name, 0, self::MAX_ORDER_ITEM_NAME_LENGTH - 3) . '...';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user