Skip to content

Commit

Permalink
Afterpay v3.4.5
Browse files Browse the repository at this point in the history
- Upgraded from regional APIs to a single Global API

- Added support for the ES, FR and IT countries and EUR currency

- Improved retention of consumer-facing checkout notifications in the event of a payment decline

- Minor improvements to cron jobs

- Various other improvements
  • Loading branch information
afterpayplugins committed Feb 7, 2023
1 parent 52bfb4d commit 99009b1
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 220 deletions.
11 changes: 9 additions & 2 deletions Block/Catalog/Installments.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function canShow(): bool

if ($this->_getPaymentIsActive() &&
$this->afterpayConfig->getCurrencyCode() &&
$this->afterpayPayovertime->canUseForCurrency($this->afterpayConfig->getCurrencyCode() &&
$product->isSalable())
$this->afterpayPayovertime->canUseForCurrency($this->afterpayConfig->getCurrencyCode())
) {
$excluded_categories = $this->afterpayConfig->getExcludedCategories();
if ($excluded_categories != "") {
Expand Down Expand Up @@ -166,4 +165,12 @@ public function isProductVirtual()

return $isVirtual;
}
/**
* @retrun boolean
*/
public function isProductOutOfStock(){

$product = $this->registry->registry('product');
return $product && !$product->isSalable();
}
}
6 changes: 3 additions & 3 deletions Controller/Payment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ private function _captureVirtual($order = null,$payment = null)

foreach($order->getAllItems() as $items) {
if($items->getIsVirtual()) {
$itemPrice = ($items->getQtyOrdered() * $items->getPrice())+$items->getBaseTaxAmount();
$totalCaptureAmount = $totalCaptureAmount + ($itemPrice - $items->getDiscountAmount());
$itemPrice = ($items->getQtyOrdered() * $items->getBasePrice())+$items->getBaseTaxAmount();
$totalCaptureAmount = $totalCaptureAmount + ($itemPrice - $items->getBaseDiscountAmount());
}
}

Expand All @@ -424,7 +424,7 @@ private function _captureVirtual($order = null,$payment = null)
if($totalCaptureAmount >= 1){
$afterpay_order_id = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ADDITIONAL_INFORMATION_KEY_ORDERID);
$merchant_order_id = $order->getIncrementId();
$currencyCode = $order->getOrderCurrencyCode();
$currencyCode = $order->getBaseCurrencyCode();

$totalAmount= [
'amount' => number_format($totalCaptureAmount, 2, '.', ''),
Expand Down
6 changes: 3 additions & 3 deletions Model/Adapter/AfterpayExpressPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public function captureVirtual($order = null, $payment = null)

foreach ($order->getAllItems() as $items) {
if ($items->getIsVirtual()) {
$itemPrice = ($items->getQtyOrdered() * $items->getPrice()) + $items->getBaseTaxAmount();
$totalCaptureAmount = $totalCaptureAmount + ($itemPrice - $items->getDiscountAmount());
$itemPrice = ($items->getQtyOrdered() * $items->getBasePrice()) + $items->getBaseTaxAmount();
$totalCaptureAmount = $totalCaptureAmount + ($itemPrice - $items->getBaseDiscountAmount());
}
}

Expand All @@ -302,7 +302,7 @@ public function captureVirtual($order = null, $payment = null)
if ($totalCaptureAmount >= 1) {
$afterpay_order_id = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ADDITIONAL_INFORMATION_KEY_ORDERID);
$merchant_order_id = $order->getIncrementId();
$currencyCode = $order->getOrderCurrencyCode();
$currencyCode = $order->getBaseCurrencyCode();

$totalAmount = [
'amount' => number_format($totalCaptureAmount, 2, '.', ''),
Expand Down
40 changes: 11 additions & 29 deletions Model/Config/Payovertime.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,38 +194,20 @@ public function getSiteConfig($apiMode, $type, $websiteId)
}

$url ="";
if ($type=='api_url') {
if ($apiMode == 'Sandbox') {
if ($currency == 'USD' || $currency == 'CAD') {
$url = 'https://api.us-sandbox.afterpay.com/';
} else {
$url = 'https://api-sandbox.afterpay.com/';
}
} elseif ($apiMode == 'Production') {
if ($currency == 'USD' || $currency == 'CAD') {
$url = 'https://api.us.afterpay.com/';
} else {
$url = 'https://api.afterpay.com/';
}
}
}
switch($type){
case "api_url":
$url=($apiMode == 'Production')?"https://global-api.afterpay.com/" : "https://global-api-sandbox.afterpay.com/" ;
break;
case "web_url":
$url=($apiMode == 'Production')?"https://portal.afterpay.com/" : "https://portal.sandbox.afterpay.com/" ;
break;
case "js_lib_url":
// get JS Library URL
$url=($apiMode == 'Production')?"https://js.afterpay.com/" : "https://js.sandbox.afterpay.com/" ;
break;

if ($type=='web_url') {
if ($apiMode == 'Sandbox') {
$url = 'https://portal.sandbox.afterpay.com/';
} elseif ($apiMode == 'Production') {
$url = 'https://portal.afterpay.com/';
}
}

// get JS Library URL
if ($type=='js_lib_url') {
if ($apiMode == 'Sandbox') {
$url = 'https://js.sandbox.afterpay.com/';
} elseif ($apiMode == 'Production') {
$url = 'https://js.afterpay.com/';
}
}
return $url;
}

Expand Down
5 changes: 4 additions & 1 deletion Model/ConfigProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class ConfigProvider implements ConfigProviderInterface
* ConfigProvider constructor.
* @param Config\Payovertime $config
*/
public function __construct(\Afterpay\Afterpay\Model\Config\Payovertime $config,\Afterpay\Afterpay\Model\Payovertime $afterpayPayovertime)
private $localeResolver;
public function __construct(\Afterpay\Afterpay\Model\Config\Payovertime $config,\Afterpay\Afterpay\Model\Payovertime $afterpayPayovertime,\Magento\Framework\Locale\Resolver $localeResolver)
{
$this->afterpayConfig = $config;
$this->afterpayPayovertime = $afterpayPayovertime;
$this->localeResolver = $localeResolver;
}

/**
Expand All @@ -57,6 +59,7 @@ public function getConfig()
'termsConditionUrl' => self::TERMS_CONDITION_LINK,
'currencyCode' => $this->afterpayConfig->getCurrencyCode(),
'baseCurrencyCode' => $this->afterpayPayovertime->getStoreCurrencyCode(),
'storeLocale' =>$this->localeResolver->getLocale()
],
],
]);
Expand Down
16 changes: 8 additions & 8 deletions Model/Cron/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Framework\App\Config\Storage\WriterInterface as WriterInterface;
use Magento\Config\Model\ResourceModel\Config as RequestConfig;
use Magento\Framework\Message\ManagerInterface as MessageManager;
use Psr\Log\LoggerInterface as logger;

class Limit
{
Expand All @@ -26,7 +26,7 @@ class Limit
protected $_jsonHelper;
protected $_resourceConfig;
protected $_writerInterface;
protected $_messageManager;
private $_logger;

/**
* Limit constructor.
Expand All @@ -41,15 +41,15 @@ public function __construct(
JsonHelper $jsonHelper,
WriterInterface $writerInterface,
RequestConfig $resourceConfig,
MessageManager $messageManager
logger $logger
) {
$this->_afterpayTotalLimit = $afterpayTotalLimit;
$this->_storeManager = $storeManager;
$this->_jsonHelper = $jsonHelper;
$this->_resourceConfig = $resourceConfig;
$this->_helper = $helper;
$this->_writerInterface = $writerInterface;
$this->_messageManager = $messageManager;
$this->_logger=$logger;
}

/**
Expand Down Expand Up @@ -93,8 +93,8 @@ private function _updateDefault()


if (array_key_exists('errorCode', $response)) {
//Unfortunately Message Manager is not working with CRON jobs yet
$this->_messageManager->addWarningMessage('Afterpay Update Limits Failed. Please check Merchant ID and Key. Default Config');
//Log the error
$this->_logger->warning('Afterpay Update Limits Failed. Please check Merchant ID and Key. Default Config');
return false;
} else {
// default min and max if not provided
Expand Down Expand Up @@ -141,8 +141,8 @@ private function _updateWebsite($website)
$response = $this->_jsonHelper->jsonDecode($response->getBody());

if (array_key_exists('errorCode', $response)) {
//Unfortunately Message Manager is not working with CRON jobs yet
$this->_messageManager->addWarningMessage('Afterpay Update Limits Failed. Please check Merchant ID and Key.' . $website["name"]);
//Log the error
$this->_logger->warning('Afterpay Update Limits Failed. Please check Merchant ID and Key.' . $website["name"]);
return false;
} else {
// default min and max if not provided
Expand Down
29 changes: 14 additions & 15 deletions Model/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function calculateRefund($payment, $amount)
$order = $payment->getOrder();
$creditmemo = $payment->getCreditmemo();
$amountToCapture = 0.00;
$storeCredit = $creditmemo->getCustomerBalanceAmount();
$storeCredit = $creditmemo->getBaseCustomerBalanceAmount();
$override = ["website_id" => $order->getStore()->getWebsiteId()];

$afterpayPaymentStatus = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::PAYMENT_STATUS);
Expand All @@ -337,10 +337,10 @@ public function calculateRefund($payment, $amount)
}
elseif($afterpayPaymentStatus == self::PAYMENT_STATUS_PARTIALLY_CAPTURED || $afterpayPaymentStatus == self::PAYMENT_STATUS_AUTH_APPROVED){

$orderTotal = $order->getGrandTotal();
$shippingApplied = $creditmemo->getShippingInclTax();
$adjustmentPositive = $creditmemo->getAdjustmentPositive();
$adjustmentNegative = $creditmemo->getAdjustmentNegative();
$orderTotal = $order->getBaseGrandTotal();
$shippingApplied = $creditmemo->getBaseShippingInclTax();
$adjustmentPositive = $creditmemo->getBaseAdjustmentPositive();
$adjustmentNegative = $creditmemo->getBaseAdjustmentNegative();
$amountCaptured = 0.00;
$amountNotCaptured = 0.00;
$amountToRefund = 0.00;
Expand All @@ -350,16 +350,16 @@ public function calculateRefund($payment, $amount)
$refundAmountAvailable = $orderTotal - $openToCaptureAmount;
$appliedDiscount = 0.00;
$refundedDiscount = 0.00;
$orderDiscount = $order->getCustomerBalanceAmount() + $order->getGiftCardsAmount();
$orderDiscount = $order->getBaseCustomerBalanceAmount() + $order->getBaseGiftCardsAmount();
$rolloverDiscount = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ROLLOVER_DISCOUNT);
$capturedDiscount = $orderDiscount - $rolloverDiscount;
$orderShippingAmount = $order->getShippingInclTax();
$actualOpenToCaptureAmount = $openToCaptureAmount - ($rolloverRefund + $rolloverAmount);
$shippingRefunded = ($order->getShippingRefunded() + $order->getShippingTaxRefunded()) - $shippingApplied;
$shippingRefunded = ($order->getBaseShippingRefunded() + $order->getBaseShippingTaxRefunded()) - $shippingApplied;

if($orderDiscount > 0){
$refundedDiscount = $order->getCustomerBalanceRefunded() + $order->getGiftCardsRefunded();
$appliedDiscount = $creditmemo->getCustomerBalanceAmount() + $creditmemo->getGiftCardsAmount();
$refundedDiscount = $order->getBaseCustomerBalanceRefunded() + $order->getBaseGiftCardsRefunded();
$appliedDiscount = $creditmemo->getBaseCustomerBalanceAmount() + $creditmemo->getBaseGiftCardsAmount();
}

foreach ($creditmemo->getAllItems() as $item) {
Expand Down Expand Up @@ -512,7 +512,6 @@ public function calculateRefund($payment, $amount)
if($storeCredit > 0){
$storeCredit = $storeCredit + $order->getBaseCustomerBalanceRefunded();
$order->setBaseCustomerBalanceRefunded($storeCredit);
$order->setCustomerBalanceRefunded($storeCredit);
}

}
Expand Down Expand Up @@ -546,7 +545,7 @@ public function lastShipmentProcessRefund($payment, $amount)
}
elseif($afterpayPaymentStatus == self::PAYMENT_STATUS_PARTIALLY_CAPTURED || $afterpayPaymentStatus == self::PAYMENT_STATUS_AUTH_APPROVED){

$orderTotal = $order->getGrandTotal();
$orderTotal = $order->getBaseGrandTotal();
$openToCaptureAmount = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::OPEN_TOCAPTURE_AMOUNT);
$rolloverRefund = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ROLLOVER_REFUND);
$refundAmountAvailable = $orderTotal - $openToCaptureAmount;
Expand Down Expand Up @@ -596,7 +595,7 @@ public function afterpayProcessRefund($payment,$order,$amountToCapture,$afterpay
$merchant_order_id = $order->getIncrementId();
$totalAmount= [
'amount' => number_format($amountToCapture, 2, '.', ''),
'currency' => $order->getOrderCurrencyCode()
'currency' => $order->getBaseCurrencyCode()
];

$captureResponse = $this->paymentCapture->send($totalAmount,$merchant_order_id,$orderId,$override);
Expand All @@ -617,7 +616,7 @@ public function afterpayProcessRefund($payment,$order,$amountToCapture,$afterpay
//Refund reqest
if($afterpayRefund && $amount > 0){

$refundResponse = $this->afterpayApiPayment->refund(number_format($amount, 2, '.', ''),$orderId,$order->getOrderCurrencyCode(),$override);
$refundResponse = $this->afterpayApiPayment->refund(number_format($amount, 2, '.', ''),$orderId,$order->getBaseCurrencyCode(),$override);

$refundResponse = $this->jsonHelper->jsonDecode($refundResponse->getBody());

Expand Down Expand Up @@ -666,12 +665,12 @@ public function afterpayProcessRefund($payment,$order,$amountToCapture,$afterpay
public function calculateItemPrice($item,$qty){
$totalQtyOrdered = $item->getQtyOrdered();
$totalTaxAmount = $item->getBaseTaxAmount();
$totalDiscount = $item->getDiscountAmount();
$totalDiscount = $item->getBaseDiscountAmount();

$taxPerItem = $totalTaxAmount/$totalQtyOrdered;
$discountPerItem = $totalDiscount / $totalQtyOrdered;

$pricePerItem = $item->getPrice() + $taxPerItem;
$pricePerItem = $item->getBasePrice() + $taxPerItem;
$itemPrice = $qty * ($pricePerItem - $discountPerItem);
return $itemPrice;
}
Expand Down
13 changes: 6 additions & 7 deletions Observer/BeforeShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$rolloverAmount = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ROLLOVER_AMOUNT);
$rolloverRefund = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ROLLOVER_REFUND);

if($order->getShippingInclTax() > 0 && $order->getShipmentsCollection()->count()==0){
$shippingAmount = $order->getShippingInclTax();
if($order->getBaseShippingInclTax() > 0 && $order->getShipmentsCollection()->count()==0){
$shippingAmount = $order->getBaseShippingInclTax();

if($order->getShippingRefunded() > 0)
if($order->getBaseShippingRefunded() > 0)
{
$shippingAmount = $shippingAmount - ($order->getShippingRefunded() + $order->getShippingTaxRefunded());
$shippingAmount = $shippingAmount - ($order->getBaseShippingRefunded() + $order->getBaseShippingTaxRefunded());
}
$totalCaptureAmount = $totalCaptureAmount + $shippingAmount;
}
Expand All @@ -86,7 +86,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)

if($totalDiscountAmount!=0){
if($totalCaptureAmount >= $totalDiscountAmount){
$this->_helper->debug("totalDiscountAmount : ".$totalDiscountAmount);
$totalCaptureAmount = $totalCaptureAmount - $totalDiscountAmount;
$totalDiscountAmount = 0.00;
}
Expand All @@ -101,7 +100,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
if($totalCaptureAmount > 1){
$afterpay_order_id = $payment->getAdditionalInformation(\Afterpay\Afterpay\Model\Payovertime::ADDITIONAL_INFORMATION_KEY_ORDERID);
$merchant_order_id = $order->getIncrementId();
$currencyCode = $order->getOrderCurrencyCode();
$currencyCode = $order->getBaseCurrencyCode();
$override = ["website_id" => $payment->getOrder()->getStore()->getWebsiteId()];

$totalAmount= [
Expand Down Expand Up @@ -142,4 +141,4 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
}
}
?>
?>
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ Complete the below steps to configure the merchant’s Afterpay Merchant Credent
<li> In CLI, run the below command to update Afterpay module:
<br/>The right installation command is dependent on your Magento 2 version:


| Magento version | Command to run |
|-----------------|-------------------------------------------------------------|
| 2.4.* | composer require afterpay-global/module-afterpay:^5 |
| 2.3.* | composer require afterpay-global/module-afterpay:^4 |
| < 2.3.0 | composer require afterpay-global/module-afterpay:^3 |
</li>

</li>
<li> Make sure that Composer finished the update without errors </li>
<li> Run Magento install/upgrade scripts: <code><em>php bin/magento setup:upgrade</em></code> </li>
<li> Compile dependency injection: <code><em>php bin/magento setup:di:compile</em></code> </li>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license" : "OSL-3.0",
"type" : "magento2-module",
"description" : "Magento 2 Afterpay Payment Module",
"version" : "3.4.4",
"version" : "3.4.5",
"authors" : [{
"name" : "Afterpay",
"homepage" : "https://www.afterpay.com"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Afterpay_Afterpay" setup_version="3.4.4">
<module name="Afterpay_Afterpay" setup_version="3.4.5">
<sequence>
<module name="Magento_Checkout"/>
<module name="Magento_Sales"/>
Expand Down
3 changes: 1 addition & 2 deletions view/frontend/requirejs-config.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
var config = {
map: {
'*': {
afterpay: 'https://www-dev.secure-afterpay.com.au/afterpay.js', // @todo change to use dynamic js window.checkoutConfig.payment.afterpay.afterpayJs
transparent: 'Magento_Payment/transparent'
afterpay: 'https://www-dev.secure-afterpay.com.au/afterpay.js' // @todo change to use dynamic js window.checkoutConfig.payment.afterpay.afterpayJs
}
}
};
2 changes: 1 addition & 1 deletion view/frontend/templates/afterpay/product.phtml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if ($block->isPaymentMethodActive() && $product_type != "grouped" && $block->can
}
}
</script>
<?php if ($block->isDisplayEConProductPage() && $afterpay_eligible != "false") { ?>
<?php if ($block->isDisplayEConProductPage() && $afterpay_eligible != "false" && !$block->isProductOutOfStock()) { ?>
<script type="text/x-magento-init">
{
"*": {
Expand Down
Loading

0 comments on commit 99009b1

Please sign in to comment.