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

Commit

Permalink
Release version 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
afterpayplugins committed Feb 16, 2021
1 parent 61fc03f commit 5ccd43e
Show file tree
Hide file tree
Showing 30 changed files with 711 additions and 92 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Afterpay Magento 1 Extension Changelog

## Version 3.2.0

_Wed 17 Feb 2021 (AEST)_

### Supported Editions & Versions

- Magento Community Edition (CE) version 1.7 and later.
- Magento Enterprise Edition (EE) version 1.13 and later.

### Highlights

- Introduced the Express Checkout feature.

---

## Version 3.1.1

_Wed 16 Dec 2020 (AEST)_
Expand Down
60 changes: 51 additions & 9 deletions src/app/code/community/Afterpay/Afterpay/Block/Onetouch.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Afterpay_Afterpay_Model_Method_Base as Afterpay_Base;
use Afterpay_Afterpay_Model_System_Config_Source_CartMode as CartMode;
use Afterpay_Afterpay_Model_System_Config_Source_ApiMode as ApiMode;

class Afterpay_Afterpay_Block_Onetouch extends Mage_Core_Block_Template
{
/**
Expand All @@ -9,15 +13,12 @@ class Afterpay_Afterpay_Block_Onetouch extends Mage_Core_Block_Template
*/
protected function _toHtml()
{
$total = $this->getTotalAmount();
if ( Mage::getStoreConfigFlag('afterpay/payovertime_cart/show_onetouch')
&& Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_ENABLED_FIELD)
&& Mage::helper('afterpay/checkout')->noConflict()
&& Mage::getModel('afterpay/method_payovertime')->canUseForCheckoutSession()
&& $total > 0
&& $total >= Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_MIN_ORDER_TOTAL_FIELD)
&& $total <= Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Afterpay_Model_Method_Base::API_MAX_ORDER_TOTAL_FIELD)
) {
if ( Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_ENABLED_FIELD) &&
Mage::getStoreConfig('afterpay/payovertime_cart/show_onetouch') != CartMode::NO &&
Mage::helper('afterpay/checkout')->noConflict() &&
Mage::getModel('afterpay/method_payovertime')->canUseForCheckoutSession() &&
$this->isQuoteWithinLimits()
) {
return parent::_toHtml();
} else {
return '';
Expand All @@ -43,4 +44,45 @@ public function getTotalAmount()
{
return Mage::helper('afterpay')->calculateTotal();
}

public function isQuoteWithinLimits()
{
$total = $this->getTotalAmount();
$min = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MIN_ORDER_TOTAL_FIELD);
$max = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MAX_ORDER_TOTAL_FIELD);

return ($total > 0 && $total >= $min && $total <= $max);
}

public function isExpress()
{
return Mage::getStoreConfig('afterpay/payovertime_cart/show_onetouch') == CartMode::EXPRESS;
}

public function isShippingRequired()
{
$quote = Mage::getSingleton('checkout/session')->getQuote();
return !$quote->isVirtual();
}

public function getCountryCode()
{
$countryCode = '';
$currency = ApiMode::getCurrencyCode();

if (array_key_exists($currency, Afterpay_Base::CURRENCY_PROPERTIES)){
$countryCode = Afterpay_Base::CURRENCY_PROPERTIES[$currency]['jsCountry'];
}

return $countryCode;
}

public function getJsUrl()
{
$apiMode = Mage::getStoreConfig('payment/afterpaypayovertime/' . Afterpay_Base::API_MODE_CONFIG_FIELD);
$settings = ApiMode::getEnvironmentSettings($apiMode);
$key = urlencode(Mage::getStoreConfig('afterpay/payovertime_cart/express_key'));

return $settings[ApiMode::KEY_WEB_URL] . 'afterpay.js?merchant_key=' . $key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,70 @@ public function buildOrderTokenRequest($object, $override = array(), $afterPayPa
return $params;
}

public function buildDirectCaptureRequest($orderToken, $merchantOrderId)
public function buildExpressOrderTokenRequest($object)
{
$precision = 2;

$params = array(
'mode' => 'express',
'totalAmount' => array(
'amount' => round((float)$object->getSubtotalWithDiscount(), $precision),
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode(),
),
'merchant' => array(
'redirectConfirmUrl' => $this->getApiRouter()->getConfirmOrderUrl(),
'redirectCancelUrl' => $this->getApiRouter()->getCancelOrderUrl(),
),
'merchantReference' => (string)$object->getReservedOrderId(),
'items' => array(),
'discounts' => array(),
);

foreach ($object->getAllVisibleItems() as $item) {
/** @var Mage_Sales_Model_Order_Item $item */
$params['items'][] = array(
'name' => (string)$item->getName(),
'sku' => $this->_truncateString( (string)$item->getSku() ),
'quantity' => (int)$item->getQty(),
'price' => array(
'amount' => round((float)$item->getPriceInclTax(), $precision),
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode()
)
);
//get the total discount amount
$discount_amount = $item->getDiscountAmount();
if ( !empty($discount_amount) && round((float)$discount_amount, $precision) > 0 ) {
$discount_name = (string)$object->getCouponCode();
if( empty($discount_name) || strlen(trim($discount_name)) == '' ) {
$discount_name = 'Discount:';
}
$params['discounts'][] = array(
'displayName' => substr( $discount_name . ' - ' . (string)$item->getName(), 0, 128 ),
'amount' => array(
'amount' => round((float)$item->getDiscountAmount(), $precision),
'currency' => (string)Mage::app()->getStore()->getCurrentCurrencyCode()
),
);
}
}
return $params;
}

public function buildDirectCaptureRequest($orderToken, $merchantOrderId, $quote)
{
if ($quote->getData('afterpay_express_checkout') ) {
$params = array(
'amount' => json_decode($quote->getData('afterpay_express_amount'), true),
'isCheckoutAdjusted' => true,
'shipping' => json_decode($quote->getData('afterpay_express_shipping'), true),
);
} else {
$params = array();
}

$params['token'] = $orderToken;
$params['merchantReference'] = $merchantOrderId;
$params['webhookEventUrl'] = "";
$params['webhookEventUrl'] = '';

return $params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ function capture(Varien_Object $payment, $amount)
*/
public function resetTransactionToken($quote) {

$quote
->setData('afterpay_express_checkout', false)
->setData('afterpay_express_amount', null)
->setData('afterpay_express_shipping', null)
->save();

Mage::getSingleton("checkout/session")->getQuote()->getPayment()->setData('afterpay_token', NULL)->save();

if( Mage::getEdition() == Mage::EDITION_ENTERPRISE ) {
Expand Down
71 changes: 68 additions & 3 deletions src/app/code/community/Afterpay/Afterpay/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,71 @@ public function start($quote)

}

/**
* Start Express Checkout
*
* @param $quote
*
* @return mixed
* @throws Mage_Core_Exception
*/
public function startExpress($quote)
{
// Magento calculate the totals
$quote->collectTotals();

// Check if total is 0 and Afterpay won't processing it
if (!$quote->getGrandTotal() && !$quote->hasNominalItems()) {
Mage::throwException(Mage::helper('afterpay')->__('Afterpay does not support processing orders with zero amount. To complete your purchase, proceed to the standard checkout process.'));
}

// Reserved order Id and save it to quote
$quote->reserveOrderId()->save();

// Afterpay build order token request - accommodate both Ver 0 and 1
$postData = $this->getApiAdapter()->buildExpressOrderTokenRequest($quote);

$gatewayUrl = $this->getApiAdapter()->getApiRouter()->getOrdersApiUrl();

// Request order token to API
$result = $this->_sendRequest($gatewayUrl, $postData, 'POST', 'StartAfterpayExpress');
$resultObject = json_decode($result);

// Check if token is NOT in response
if ( empty($resultObject->token) ) {
throw Mage::exception('Afterpay_Afterpay', 'Afterpay API Gateway Error.');
} else {
// Save token to the sales_flat_quote_payment

$orderToken = $resultObject->token;

try {
$payment = $quote->getPayment();
$payment->setData('afterpay_token', $orderToken);
$payment->save();

// Added to log
Mage::helper('afterpay')->log(
sprintf('Token successfully saved for reserved order %s. token=%s', $quote->getReservedOrderId(), $orderToken),
Zend_Log::NOTICE
);
}
catch (Exception $e) {
// Add error message
$message = 'Exception during initial Afterpay Token saving.';

$this->helper()->log($this->__($message . ' %s', $e->getMessage()), Zend_Log::ERR);

Mage::throwException(
Mage::helper('afterpay')->__($message)
);
}

return $orderToken;
}

}

/**
* Start creating order for Afterpay
*
Expand All @@ -134,9 +199,9 @@ public function start($quote)
* @return mixed
* @throws Afterpay_Afterpay_Exception
*/
public function directCapture( $orderToken, $merchantOrderId, $quote ) {

$postData = $this->getApiAdapter()->buildDirectCaptureRequest($orderToken,$merchantOrderId);
public function directCapture( $orderToken, $merchantOrderId, $quote )
{
$postData = $this->getApiAdapter()->buildDirectCaptureRequest($orderToken,$merchantOrderId,$quote);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class Afterpay_Afterpay_Model_System_Config_Source_CartMode
{
const MAGENTO = 1;
const EXPRESS = 2;
const NO = 0;

/**
* Convert to option array
*
* @return array
*/
public function toOptionArray()
{
return array(
self::MAGENTO => 'Yes - Magento Checkout',
self::EXPRESS => 'Yes - Afterpay Express Checkout',
self::NO => 'No',
);
}
}
Loading

0 comments on commit 5ccd43e

Please sign in to comment.