Skip to content

Commit

Permalink
Merge pull request #16 from two-inc/release-1.3.0
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
dgjlindsay authored Nov 3, 2023
2 parents 6a67b81 + a17435d commit 7bf1646
Show file tree
Hide file tree
Showing 40 changed files with 5,426 additions and 3,493 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.json/
.idea
*.zip
.DS_Store
.DS_Store
3 changes: 2 additions & 1 deletion Controller/Payment/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function execute()
try {
$order = $this->orderService->getOrderByReference();
$this->orderService->cancelTwoOrder($order);
throw new LocalizedException(__('Order has been canceled'));
$message = 'Your invoice purchase with Two has been cancelled. The cart will be restored.';
throw new LocalizedException(__($message));
} catch (Exception $exception) {
$this->orderService->restoreQuote();
if (isset($order)) {
Expand Down
3 changes: 2 additions & 1 deletion Controller/Payment/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ public function execute()
$this->orderService->processOrder($order, $twoOrder['id']);
return $this->getResponse()->setRedirect($this->_url->getUrl('checkout/onepage/success'));
} else {
$message = 'Unable to retrieve the order payment information';
$message = 'Unable to retrieve payment information for your invoice purchase with Two.';
if (!empty($twoOrder['decline_reason'])) {
$message = $twoOrder['decline_reason'];
}
$message .= ' The cart will be restored.';
$this->orderService->addOrderComment($order, $message);
throw new LocalizedException(__($message));
}
Expand Down
3 changes: 2 additions & 1 deletion Controller/Payment/Verificationfailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function execute()
{
try {
$order = $this->orderService->getOrderByReference();
throw new LocalizedException(__('Verification Failed'));
$message = 'Your invoice purchase with Two failed verification. The cart will be restored.';
throw new LocalizedException(__($message));
} catch (Exception $exception) {
$this->orderService->restoreQuote();
if (isset($order)) {
Expand Down
56 changes: 50 additions & 6 deletions Model/Two.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public function authorize(InfoInterface $payment, $amount)
}

if ($response['status'] !== static::STATUS_APPROVED) {
$this->logRepository->addDebugLog('Order was not accepted', $response);
throw new LocalizedException(__('Your order was not accepted'));
$this->logRepository->addDebugLog('Order was not accepted by Two', $response);
throw new LocalizedException(__('Invoice purchase with Two is not available for this order.'));
}

$order->setTwoOrderReference($orderReference);
Expand Down Expand Up @@ -243,17 +243,38 @@ public function authorize(InfoInterface $payment, $amount)
*/
public function getErrorFromResponse(array $response): ?Phrase
{
$generalMessage = __('Something went wrong. Please try again.');
$generalMessage = __('Something went wrong with your request to Two. Please try again later.');
if (!$response || !is_array($response)) {
return $generalMessage;
}

// Custom errors
if (isset($response['error_code']) && $response['error_code'] == 'SAME_BUYER_SELLER_ERROR') {
return __('Your request to Two failed because the buyer and the seller are the same company.');
}

// Validation errors
if (isset($response['error_json']) && is_array($response['error_json'])) {
$errs = [];
foreach ($response['error_json'] as $err) {
if ($err && $err['loc']) {
$err_field = $this->getFieldFromLocStr(json_encode($err['loc']));
if ($err_field) {
array_push($errs, __($err_field));
}
}
}
if (count($errs) > 0) {
$message = __('Your request to Two failed due to the following issue(s):');
return __($message . ' ' . join(' ', $errs));
}
}

if (!empty($response['error_code'])) {
$message = $response['error_message'];
if (!empty($response['error_details'])) {
$message .= ' - ' . $response['error_details'];
if (!empty($response['error_trace_id'])) {
$message .= ' [Trace ID: ' . $response['error_trace_id'] . ']';
}

return __($message);
}

Expand All @@ -264,6 +285,29 @@ public function getErrorFromResponse(array $response): ?Phrase
return null;
}

/**
* Get validation message
*
* @param $loc_str
* @return string|null
*/
public function getFieldFromLocStr($loc_str): ?string
{
$loc_str = preg_replace('/\s+/', '', $loc_str);
$fieldLocStrMapping = [
'["buyer","representative","phone_number"]' => 'Phone Number is not valid (requires plus prefix).',
'["buyer","company","organization_number"]' => 'Company ID is not valid.',
'["buyer","representative","first_name"]' => 'First Name is not valid.',
'["buyer","representative","last_name"]' => 'Last Name is not valid.',
'["buyer","representative","email"]' => 'Email Address is not valid.',
'["billing_address","street_address"]' => 'Street Address is not valid.',
'["billing_address","city"]' => 'City is not valid.',
'["billing_address","country"]' => 'Country is not valid.',
'["billing_address","postal_code"]' => 'Zip/Postal Code is not valid.',
];
return $fieldLocStrMapping[$loc_str];
}

/**
* @inheritDoc
*/
Expand Down
4 changes: 2 additions & 2 deletions Model/Ui/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getConfig(): array
$internationalTelephoneConfig = [];
if ($this->configRepository->isInternationalTelephoneEnabled()) {
$internationalTelephoneConfig['utilsScript'] =
$this->assetRepository->getUrl('Two_Gateway::js/international-telephone/utils.js');
$this->assetRepository->getUrl('Two_Gateway::intl-tel-input-18.2.1/js/utils.js');
}

$companyAutoCompleteConfig = [];
Expand Down Expand Up @@ -88,7 +88,7 @@ public function getConfig(): array
'companyAutoCompleteConfig' => $companyAutoCompleteConfig,
'intentOrderConfig' => $intentOrderConfig,
'isAddressAutoCompleteEnabled' => $this->configRepository->isAddressAutocompleteEnabled(),
'supportedCountryCodes' => ['NO', 'GB', 'SE']
'supportedCountryCodes' => ['no', 'gb', 'se']
],
],
];
Expand Down
8 changes: 4 additions & 4 deletions Model/Webapi/SoleTrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ private function getDelegationToken(): string
self::DELEGATION_TOKEN_ENDPOINT,
['create_proposal' => true, 'read_current_business' => true]
);
if (isset($delegateResponse['token'])) {
return $delegateResponse['token'];
if (isset($delegateResponse['two-delegated-authority-token'])) {
return $delegateResponse['two-delegated-authority-token'];
} else {
return '';
}
Expand All @@ -57,8 +57,8 @@ private function getAutofillToken()
self::AUTOFILL_TOKEN_ENDPOINT,
['read_current_buyer' => true, 'write_current_buyer' => true]
);
if (isset($autofillResponse['token'])) {
return $autofillResponse['token'];
if (isset($autofillResponse['two-delegated-authority-token'])) {
return $autofillResponse['two-delegated-authority-token'];
} else {
return '';
}
Expand Down
14 changes: 2 additions & 12 deletions Service/Api/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ public function execute(string $endpoint, array $payload = [], string $method =
$this->curlClient->setOption(CURLOPT_SSL_VERIFYHOST, 0);
$this->curlClient->setOption(CURLOPT_SSL_VERIFYPEER, 0);
$this->curlClient->setOption(CURLOPT_TIMEOUT, 60);
if (isset($payload['two-delegated-authority-token'])) {
$this->curlClient->addHeader(
"two-delegated-authority-token",
$payload['two-delegated-authority-token']
);
$this->curlClient->setOption(CURLOPT_COOKIESESSION, true);
$this->curlClient->setOption(CURLOPT_COOKIEFILE, 'cookie.txt');
$this->curlClient->setOption(CURLOPT_COOKIEJAR, 'cookie.txt');
$payload = [];
}

if ($method == "POST" || $method == "PUT") {
$params = empty($payload) ? '' : json_encode($payload);
Expand All @@ -95,12 +85,12 @@ public function execute(string $endpoint, array $payload = [], string $method =
$body = trim($this->curlClient->getBody());

if (in_array($this->curlClient->getStatus(), [200, 201, 202])) {
$result = [];
if ((!$body || $body === '""')) {
if (($endpoint == SoleTraderInterface::DELEGATION_TOKEN_ENDPOINT) ||
($endpoint == SoleTraderInterface::AUTOFILL_TOKEN_ENDPOINT)) {
return ['token' => $this->curlClient->getHeaders()['two-delegated-authority-token']];
$result = $this->curlClient->getHeaders();
}
$result = [];
} else {
$result = json_decode($body, true);
}
Expand Down
31 changes: 13 additions & 18 deletions Service/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function getLineItemsOrder(OrderModel $order): array
'discount_amount' => $this->roundAmt($this->getDiscountAmountItem($item)),
'tax_rate' => $this->roundAmt(($item->getTaxPercent() / 100)),
'tax_class_name' => 'VAT ' . $this->roundAmt($item->getTaxPercent()) . '%',
'unit_price' => $this->roundAmt($this->getUnitPriceItem($item)),
'unit_price' => $this->roundAmt($this->getUnitPriceItem($item), 5),
'quantity' => $item->getQtyOrdered(),
'qty_to_ship' => $item->getQtyToShip(), //need for partial shipment
'quantity_unit' => $this->configRepository->getWeightUnit((int)$order->getStoreId()),
Expand Down Expand Up @@ -225,11 +225,12 @@ public function getProductImageUrl(Product $product): string
* Format price
*
* @param mixed $amt
* @param int $dp
* @return string
*/
public function roundAmt($amt): string
public function roundAmt($amt, $dp = 2): string
{
return number_format((float)$amt, 2, '.', '');
return number_format((float)$amt, $dp, '.', '');
}

/**
Expand All @@ -238,7 +239,7 @@ public function roundAmt($amt): string
*/
public function getGrossAmountItem($item): float
{
return (float)($this->getNetAmountItem($item) + $this->getTaxAmountItem($item));
return (float)$this->getNetAmountItem($item) + (float)$this->getTaxAmountItem($item);
}

/**
Expand All @@ -247,13 +248,7 @@ public function getGrossAmountItem($item): float
*/
public function getNetAmountItem($item): float
{
$qty = $item instanceof OrderItem
? $item->getQtyOrdered()
: $item->getQty();

return (float)(
($qty * $this->getUnitPriceItem($item)) - $this->getDiscountAmountItem($item)
);
return (float)$item->getRowTotal() - $this->getDiscountAmountItem($item);
}

/**
Expand All @@ -262,7 +257,7 @@ public function getNetAmountItem($item): float
*/
public function getUnitPriceItem($item): float
{
return $item->getPrice();
return (float)$item->getRowTotalInclTax() / (1 + $item->getTaxPercent() / 100) / $item->getQtyOrdered();
}

/**
Expand All @@ -280,7 +275,7 @@ public function getTaxAmountItem($item): float
*/
public function getDiscountAmountItem($item): float
{
return (float)$item->getDiscountAmount();
return abs((float)$item->getDiscountAmount()) - abs((float)$item->getDiscountTaxCompensationAmount());
}

/**
Expand Down Expand Up @@ -335,7 +330,7 @@ public function getShippingLineOrder(OrderModel $order): array
'tax_amount' => $this->roundAmt($this->getTaxAmountShipping($order)),
'discount_amount' => $this->roundAmt($this->getDiscountAmountShipping($order)),
'tax_rate' => $this->roundAmt($this->getTaxRateShipping($order)),
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($order)),
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($order), 5),
'tax_class_name' => 'VAT ' . $this->roundAmt($this->getTaxRateShipping($order) * 100) . '%',
'quantity' => 1,
'qty_to_ship' => 1, //need for partial shipment
Expand Down Expand Up @@ -369,7 +364,7 @@ public function getNetAmountShipping($entity): float
*/
public function getUnitPriceShipping($entity): float
{
return (float)$entity->getShippingInclTax() - $entity->getShippingTaxAmount();
return (float)$entity->getShippingAmount();
}

/**
Expand All @@ -378,7 +373,7 @@ public function getUnitPriceShipping($entity): float
*/
public function getDiscountAmountShipping($entity): float
{
return (float)$entity->getShippingDiscountAmount();
return (float)$entity->getShippingDiscountAmount() - (float)$entity->getShippingDiscountTaxCompensationAmount();
}

/**
Expand All @@ -396,7 +391,7 @@ public function getTaxAmountShipping($entity): float
*/
public function getTaxRateShipping($entity): float
{
return round(($entity->getShippingInclTax() / $entity->getShippingAmount()), 6) - 1;
return ($entity->getShippingInclTax() / $entity->getShippingAmount()) - 1;
}

/**
Expand All @@ -420,7 +415,7 @@ public function getAddress(OrderModel $order, ?array $additionalData, string $ty
'postal_code' => $address->getPostcode(),
'region' => $address->getRegion() != '' ? $address->getRegion() : '',
'street_address' => $address->getStreet()[0]
. (isset($address->getStreet()[1]) ? $address->getStreet()[1] : ''),
. (isset($address->getStreet()[1]) ? ', ' . $address->getStreet()[1] : ''),
];
}

Expand Down
4 changes: 2 additions & 2 deletions Service/Order/ComposeCapture.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getLineItemsInvoice(Order\Invoice $invoice, Order $order): array
'tax_amount' => $this->roundAmt($this->getTaxAmountItem($item)),
'tax_class_name' => 'VAT ' . $this->roundAmt($orderItem->getTaxPercent()) . '%',
'tax_rate' => $this->roundAmt(($orderItem->getTaxPercent() / 100)),
'unit_price' => $this->roundAmt($this->getUnitPriceItem($item)),
'unit_price' => $this->roundAmt($this->getUnitPriceItem($item), 5),
'quantity' => $item->getQty(),
'quantity_unit' => $this->configRepository->getWeightUnit((int)$order->getStoreId()),
'image_url' => $this->getProductImageUrl($product),
Expand Down Expand Up @@ -116,7 +116,7 @@ public function getLineItemsInvoice(Order\Invoice $invoice, Order $order): array
'tax_amount' => $this->roundAmt((float)$order->getShippingTaxAmount()),
'discount_amount' => $this->roundAmt($this->getDiscountAmountShipping($order)),
'tax_rate' => $this->roundAmt((1.0 * $order->getShippingTaxAmount() / $order->getShippingAmount())),
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($order)),
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($order), 5),
'tax_class_name' => 'VAT ' . $this->roundAmt($taxRate * 100) . '%',
'quantity' => 1,
'quantity_unit' => 'sc',
Expand Down
5 changes: 1 addition & 4 deletions Service/Order/ComposeOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ public function execute(Order $order, string $orderReference, array $additionalD
'buyer_project' => $additionalData['project'] ?? '',
'buyer_purchase_order_number' => $additionalData['poNumber'] ?? '',
'currency' => $order->getOrderCurrencyCode(),
'discount_amount' => $this->roundAmt(abs((float)$order->getDiscountAmount())),
'discount_amount' => $this->roundAmt($this->getDiscountAmountItem($order)),
'gross_amount' => $this->roundAmt($order->getGrandTotal()),
'net_amount' => $this->roundAmt($order->getGrandTotal() - $order->getTaxAmount()),
'tax_amount' => $this->roundAmt($order->getTaxAmount()),
'tax_rate' => $this->roundAmt(
(1.0 * $order->getTaxAmount() / ($order->getGrandTotal() - $order->getTaxAmount()))
),
'tax_subtotals' => $this->getTaxSubtotals($linesItems),
'invoice_type' => 'FUNDED_INVOICE',
'line_items' => $linesItems,
Expand Down
4 changes: 2 additions & 2 deletions Service/Order/ComposeRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getLineItemsCreditmemo(Order $order, Creditmemo $creditmemo): ar
'net_amount' => $this->roundAmt($this->getNetAmountItem($orderItem) / $part) * -1,
'tax_amount' => $this->roundAmt($this->getTaxAmountItem($orderItem) / $part) * -1,
'discount_amount' => $this->roundAmt($this->getDiscountAmountItem($orderItem) / $part) * -1,
'unit_price' => $this->roundAmt($this->getUnitPriceItem($orderItem)) * -1,
'unit_price' => $this->roundAmt($this->getUnitPriceItem($orderItem), 5) * -1,
'tax_rate' => $this->roundAmt(($orderItem->getTaxPercent() / 100)),
'tax_class_name' => 'VAT ' . $this->roundAmt($orderItem->getTaxPercent()) . '%',
'quantity' => $item->getQty(),
Expand Down Expand Up @@ -102,7 +102,7 @@ public function getLineItemsCreditmemo(Order $order, Creditmemo $creditmemo): ar
'net_amount' => $this->roundAmt($this->getNetAmountShipping($creditmemo)) * -1,
'tax_amount' => $this->roundAmt($this->getTaxAmountShipping($creditmemo)) * -1,
'discount_amount' => $this->roundAmt($this->getDiscountAmountShipping($creditmemo)) * -1,
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($creditmemo)) * -1,
'unit_price' => $this->roundAmt($this->getUnitPriceShipping($creditmemo), 5) * -1,
'tax_rate' => $this->roundAmt($this->getTaxRateShipping($creditmemo)),
'tax_class_name' => 'VAT ' . $this->roundAmt($this->getTaxRateShipping($creditmemo) * 100) . '%',
'quantity' => 1,
Expand Down
2 changes: 1 addition & 1 deletion Service/Order/ComposeShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getLineItemsShipment(Order $order, Order\Shipment $shipment): ar
'tax_amount' => $this->roundAmt($this->getTaxAmountItem($orderItem) / $part),
'tax_class_name' => 'VAT ' . $this->roundAmt($orderItem->getTaxPercent()) . '%',
'tax_rate' => $this->roundAmt(($orderItem->getTaxPercent() / 100)),
'unit_price' => $this->roundAmt($this->getUnitPriceItem($orderItem)),
'unit_price' => $this->roundAmt($this->getUnitPriceItem($orderItem), 5),
'quantity' => $item->getQty(),
'quantity_unit' => $this->configRepository->getWeightUnit((int)$order->getStoreId()),
'image_url' => $this->getProductImageUrl($product),
Expand Down
2 changes: 1 addition & 1 deletion bumpver.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpver]
current_version = "1.2.4"
current_version = "1.3.0"
version_pattern = "MAJOR.MINOR.PATCH[-TAGNUM]"
commit_message = "chore: Bump version {old_version} -> {new_version}"
commit = true
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "two-inc/magento2",
"description": "Two B2B BNPL payments extension",
"type": "magento2-module",
"version": "1.2.4",
"version": "1.3.0",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Title</label>
<comment>Descriptive title which gives the buyer a better understanding of Two payments e.g. Business invoice 14 days</comment>
<comment>Descriptive title which gives the buyer a better understanding of Two payments e.g. Business invoice 30 days</comment>
<validate>required-entry</validate>
<depends>
<field id="active">1</field>
Expand Down
Loading

0 comments on commit 7bf1646

Please sign in to comment.