Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 5ccd43e

Browse files
Release version 3.2.0
1 parent 61fc03f commit 5ccd43e

File tree

30 files changed

+711
-92
lines changed

30 files changed

+711
-92
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Afterpay Magento 1 Extension Changelog
22

3+
## Version 3.2.0
4+
5+
_Wed 17 Feb 2021 (AEST)_
6+
7+
### Supported Editions & Versions
8+
9+
- Magento Community Edition (CE) version 1.7 and later.
10+
- Magento Enterprise Edition (EE) version 1.13 and later.
11+
12+
### Highlights
13+
14+
- Introduced the Express Checkout feature.
15+
16+
---
17+
318
## Version 3.1.1
419

520
_Wed 16 Dec 2020 (AEST)_

src/app/code/community/Afterpay/Afterpay/Block/Onetouch.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
use Afterpay_Afterpay_Model_Method_Base as Afterpay_Base;
4+
use Afterpay_Afterpay_Model_System_Config_Source_CartMode as CartMode;
5+
use Afterpay_Afterpay_Model_System_Config_Source_ApiMode as ApiMode;
6+
37
class Afterpay_Afterpay_Block_Onetouch extends Mage_Core_Block_Template
48
{
59
/**
@@ -9,15 +13,12 @@ class Afterpay_Afterpay_Block_Onetouch extends Mage_Core_Block_Template
913
*/
1014
protected function _toHtml()
1115
{
12-
$total = $this->getTotalAmount();
13-
if ( Mage::getStoreConfigFlag('afterpay/payovertime_cart/show_onetouch')
14-
&& Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_ENABLED_FIELD)
15-
&& Mage::helper('afterpay/checkout')->noConflict()
16-
&& Mage::getModel('afterpay/method_payovertime')->canUseForCheckoutSession()
17-
&& $total > 0
18-
&& $total >= Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_MIN_ORDER_TOTAL_FIELD)
19-
&& $total <= Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_MAX_ORDER_TOTAL_FIELD)
20-
) {
16+
if ( Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_ENABLED_FIELD) &&
17+
Mage::getStoreConfig('afterpay/payovertime_cart/show_onetouch') != CartMode::NO &&
18+
Mage::helper('afterpay/checkout')->noConflict() &&
19+
Mage::getModel('afterpay/method_payovertime')->canUseForCheckoutSession() &&
20+
$this->isQuoteWithinLimits()
21+
) {
2122
return parent::_toHtml();
2223
} else {
2324
return '';
@@ -43,4 +44,45 @@ public function getTotalAmount()
4344
{
4445
return Mage::helper('afterpay')->calculateTotal();
4546
}
47+
48+
public function isQuoteWithinLimits()
49+
{
50+
$total = $this->getTotalAmount();
51+
$min = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MIN_ORDER_TOTAL_FIELD);
52+
$max = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MAX_ORDER_TOTAL_FIELD);
53+
54+
return ($total > 0 && $total >= $min && $total <= $max);
55+
}
56+
57+
public function isExpress()
58+
{
59+
return Mage::getStoreConfig('afterpay/payovertime_cart/show_onetouch') == CartMode::EXPRESS;
60+
}
61+
62+
public function isShippingRequired()
63+
{
64+
$quote = Mage::getSingleton('checkout/session')->getQuote();
65+
return !$quote->isVirtual();
66+
}
67+
68+
public function getCountryCode()
69+
{
70+
$countryCode = '';
71+
$currency = ApiMode::getCurrencyCode();
72+
73+
if (array_key_exists($currency, Afterpay_Base::CURRENCY_PROPERTIES)){
74+
$countryCode = Afterpay_Base::CURRENCY_PROPERTIES[$currency]['jsCountry'];
75+
}
76+
77+
return $countryCode;
78+
}
79+
80+
public function getJsUrl()
81+
{
82+
$apiMode = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MODE_CONFIG_FIELD);
83+
$settings = ApiMode::getEnvironmentSettings($apiMode);
84+
$key = urlencode(Mage::getStoreConfig('afterpay/payovertime_cart/express_key'));
85+
86+
return $settings[ApiMode::KEY_WEB_URL] . 'afterpay.js?merchant_key=' . $key;
87+
}
4688
}

src/app/code/community/Afterpay/Afterpay/Model/Api/Adapters/Adapterv1.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,70 @@ public function buildOrderTokenRequest($object, $override = array(), $afterPayPa
150150
return $params;
151151
}
152152

153-
public function buildDirectCaptureRequest($orderToken, $merchantOrderId)
153+
public function buildExpressOrderTokenRequest($object)
154154
{
155+
$precision = 2;
156+
157+
$params = array(
158+
'mode' => 'express',
159+
'totalAmount' => array(
160+
'amount' => round((float)$object->getSubtotalWithDiscount(), $precision),
161+
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode(),
162+
),
163+
'merchant' => array(
164+
'redirectConfirmUrl' => $this->getApiRouter()->getConfirmOrderUrl(),
165+
'redirectCancelUrl' => $this->getApiRouter()->getCancelOrderUrl(),
166+
),
167+
'merchantReference' => (string)$object->getReservedOrderId(),
168+
'items' => array(),
169+
'discounts' => array(),
170+
);
171+
172+
foreach ($object->getAllVisibleItems() as $item) {
173+
/** @var Mage_Sales_Model_Order_Item $item */
174+
$params['items'][] = array(
175+
'name' => (string)$item->getName(),
176+
'sku' => $this->_truncateString( (string)$item->getSku() ),
177+
'quantity' => (int)$item->getQty(),
178+
'price' => array(
179+
'amount' => round((float)$item->getPriceInclTax(), $precision),
180+
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode()
181+
)
182+
);
183+
//get the total discount amount
184+
$discount_amount = $item->getDiscountAmount();
185+
if ( !empty($discount_amount) && round((float)$discount_amount, $precision) > 0 ) {
186+
$discount_name = (string)$object->getCouponCode();
187+
if( empty($discount_name) || strlen(trim($discount_name)) == '' ) {
188+
$discount_name = 'Discount:';
189+
}
190+
$params['discounts'][] = array(
191+
'displayName' => substr( $discount_name . ' - ' . (string)$item->getName(), 0, 128 ),
192+
'amount' => array(
193+
'amount' => round((float)$item->getDiscountAmount(), $precision),
194+
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode()
195+
),
196+
);
197+
}
198+
}
199+
return $params;
200+
}
201+
202+
public function buildDirectCaptureRequest($orderToken, $merchantOrderId, $quote)
203+
{
204+
if ($quote->getData('afterpay_express_checkout') ) {
205+
$params = array(
206+
'amount' => json_decode($quote->getData('afterpay_express_amount'), true),
207+
'isCheckoutAdjusted' => true,
208+
'shipping' => json_decode($quote->getData('afterpay_express_shipping'), true),
209+
);
210+
} else {
211+
$params = array();
212+
}
213+
155214
$params['token'] = $orderToken;
156215
$params['merchantReference'] = $merchantOrderId;
157-
$params['webhookEventUrl'] = "";
216+
$params['webhookEventUrl'] = '';
158217

159218
return $params;
160219
}

src/app/code/community/Afterpay/Afterpay/Model/Method/Payovertime.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ function capture(Varien_Object $payment, $amount)
170170
*/
171171
public function resetTransactionToken($quote) {
172172

173+
$quote
174+
->setData('afterpay_express_checkout', false)
175+
->setData('afterpay_express_amount', null)
176+
->setData('afterpay_express_shipping', null)
177+
->save();
178+
173179
Mage::getSingleton("checkout/session")->getQuote()->getPayment()->setData('afterpay_token', NULL)->save();
174180

175181
if( Mage::getEdition() == Mage::EDITION_ENTERPRISE ) {

src/app/code/community/Afterpay/Afterpay/Model/Order.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,71 @@ public function start($quote)
124124

125125
}
126126

127+
/**
128+
* Start Express Checkout
129+
*
130+
* @param $quote
131+
*
132+
* @return mixed
133+
* @throws Mage_Core_Exception
134+
*/
135+
public function startExpress($quote)
136+
{
137+
// Magento calculate the totals
138+
$quote->collectTotals();
139+
140+
// Check if total is 0 and Afterpay won't processing it
141+
if (!$quote->getGrandTotal() && !$quote->hasNominalItems()) {
142+
Mage::throwException(Mage::helper('afterpay')->__('Afterpay does not support processing orders with zero amount. To complete your purchase, proceed to the standard checkout process.'));
143+
}
144+
145+
// Reserved order Id and save it to quote
146+
$quote->reserveOrderId()->save();
147+
148+
// Afterpay build order token request - accommodate both Ver 0 and 1
149+
$postData = $this->getApiAdapter()->buildExpressOrderTokenRequest($quote);
150+
151+
$gatewayUrl = $this->getApiAdapter()->getApiRouter()->getOrdersApiUrl();
152+
153+
// Request order token to API
154+
$result = $this->_sendRequest($gatewayUrl, $postData, 'POST', 'StartAfterpayExpress');
155+
$resultObject = json_decode($result);
156+
157+
// Check if token is NOT in response
158+
if ( empty($resultObject->token) ) {
159+
throw Mage::exception('Afterpay_Afterpay', 'Afterpay API Gateway Error.');
160+
} else {
161+
// Save token to the sales_flat_quote_payment
162+
163+
$orderToken = $resultObject->token;
164+
165+
try {
166+
$payment = $quote->getPayment();
167+
$payment->setData('afterpay_token', $orderToken);
168+
$payment->save();
169+
170+
// Added to log
171+
Mage::helper('afterpay')->log(
172+
sprintf('Token successfully saved for reserved order %s. token=%s', $quote->getReservedOrderId(), $orderToken),
173+
Zend_Log::NOTICE
174+
);
175+
}
176+
catch (Exception $e) {
177+
// Add error message
178+
$message = 'Exception during initial Afterpay Token saving.';
179+
180+
$this->helper()->log($this->__($message . ' %s', $e->getMessage()), Zend_Log::ERR);
181+
182+
Mage::throwException(
183+
Mage::helper('afterpay')->__($message)
184+
);
185+
}
186+
187+
return $orderToken;
188+
}
189+
190+
}
191+
127192
/**
128193
* Start creating order for Afterpay
129194
*
@@ -134,9 +199,9 @@ public function start($quote)
134199
* @return mixed
135200
* @throws Afterpay_Afterpay_Exception
136201
*/
137-
public function directCapture( $orderToken, $merchantOrderId, $quote ) {
138-
139-
$postData = $this->getApiAdapter()->buildDirectCaptureRequest($orderToken,$merchantOrderId);
202+
public function directCapture( $orderToken, $merchantOrderId, $quote )
203+
{
204+
$postData = $this->getApiAdapter()->buildDirectCaptureRequest($orderToken,$merchantOrderId,$quote);
140205

141206
$gatewayUrl = $this->getApiAdapter()->getApiRouter()->getDirectCaptureApiUrl();
142207

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class Afterpay_Afterpay_Model_System_Config_Source_CartMode
4+
{
5+
const MAGENTO = 1;
6+
const EXPRESS = 2;
7+
const NO = 0;
8+
9+
/**
10+
* Convert to option array
11+
*
12+
* @return array
13+
*/
14+
public function toOptionArray()
15+
{
16+
return array(
17+
self::MAGENTO => 'Yes - Magento Checkout',
18+
self::EXPRESS => 'Yes - Afterpay Express Checkout',
19+
self::NO => 'No',
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)