Skip to content

Commit

Permalink
Dev 3.5.3 (#36)
Browse files Browse the repository at this point in the history
* Initial commit for modal checkout flow support - Beta feature
  • Loading branch information
nandadubey authored Mar 21, 2018
1 parent 96188bd commit f3d220f
Show file tree
Hide file tree
Showing 14 changed files with 1,379 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:

package: validate_version
mkdir -p ./var/
cd ./extension && tar -cvf ../var/Affirm_Affirm-3.5.2.tgz *
cd ./extension && tar -cvf ../var/Affirm_Affirm-3.5.3.tgz *
cd ./build && ./magento-tar-to-connect.phar affirm_tar_to_connect_config.php

clean:
Expand Down
8 changes: 4 additions & 4 deletions build/affirm_tar_to_connect_config.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
return array(
'base_dir' => realpath('../var/'),
'archive_files' => 'Affirm_Affirm-3.5.2.tgz',
'archive_files' => 'Affirm_Affirm-3.5.3.tgz',
'extension_name' => 'Affirm_Magento',
'skip_version_compare' => true,
'extension_version' => '3.5.2',
'archive_connect' => 'Affirm_Affirm-3.5.2.tgz',
'extension_version' => '3.5.3',
'archive_connect' => 'Affirm_Affirm-3.5.3.tgz',
'path_output' => realpath('../var/'),

'stability' => 'stable',
'license' => 'MIT',
'license' => 'BSD-3-Clause',
'channel' => 'community',
'summary' => 'Affirm provides instant, flexible financing for your customers at checkout',
'description' => 'Affirm provides flexible financing for your customers at checkout to increase sales and conversion. Your customers will be instantly approved for a loan with Affirm during checkout and will use Affirm as their form of payment. They can then pay off their balance in easy monthly payments with a low interest rate. Affirm assumes all credit risk and will settle full payment with you right away. You will need an Affirm account to use this extension. For more information and to sign up for an Affirm account, please visit https://www.affirm.com/merchants/',
Expand Down
111 changes: 111 additions & 0 deletions extension/app/code/community/Affirm/Affirm/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Affirm_Affirm_Helper_Data extends Mage_Core_Helper_Abstract
*/
const PAYMENT_AFFIRM_DISABLE_BACK_ORDERED_ITEMS = 'payment/affirm/disable_for_backordered_items';

/**
* Checkout Flow Type
*/
const PAYMENT_AFFIRM_CHECKOUT_FLOW_TYPE = 'payment/affirm/checkout_flow_type';

/**
* Disabled module
*
Expand Down Expand Up @@ -260,6 +265,36 @@ public function isPreOrder($store = null)
return Mage::getStoreConfig(self::PAYMENT_AFFIRM_PRE_ORDER, $store);
}

/**
* Checkout flow type
*
* @param Mage_Core_Model_Store $store
* @return string
*/
public function getCheckoutFlowType($store = null)
{
if($store == null) {
$store = Mage::app()->getStore()->getStoreId();
}
return Mage::getStoreConfig(self::PAYMENT_AFFIRM_CHECKOUT_FLOW_TYPE, $store);
}

/**
* Is Checkout flow type Modal
*
* @param Mage_Core_Model_Store $store
* @return bool
*/
public function isCheckoutFlowTypeModal($store = null)
{
$configCheckoutType = Mage::helper('affirm')->getCheckoutFlowType();
if ($configCheckoutType == Affirm_Affirm_Model_Payment::CHECKOUT_FLOW_MODAL) {
return true;
} else {
return false;
}
}

/**
* Get affirm js url
*
Expand All @@ -279,6 +314,24 @@ public function getAffirmJsUrl()
return 'https://' . $prefix . '' . $domain . '/js/v2/affirm.js';
}

/**
* Get affirm js text
*
* @return string
*/
public function getAffirmJs()
{
$affirmJs = '<script type="text/javascript">
if (!AFFIRM_AFFIRM.promos.getIsInitialized()) {
AFFIRM_AFFIRM.promos.initialize("'. $this->getApiKey() .'","'. $this->getAffirmJsUrl() .'");
}
if (!AFFIRM_AFFIRM.promos.getIsScriptLoaded()) {
AFFIRM_AFFIRM.promos.loadScript();
}
</script>';
return $affirmJs;
}

/**
* Is xhr request
*
Expand Down Expand Up @@ -484,6 +537,64 @@ public function getAffirmAssetsUrl()
return 'https://' . $prefix . '.' . $domain . '/' . $assetPath ;
}

/**
* Get template for button in order review page if Affirm method was selected and checkout flow type is modal
*
* @param string $name template name
* @param string $block buttons block name
* @return string
*/
public function getReviewButtonTemplate($name, $block)
{
$quote = Mage::getSingleton('checkout/session')->getQuote();
if ($quote) {
$payment = $quote->getPayment();
if ($payment && ($payment->getMethod() == Affirm_Affirm_Model_Payment::METHOD_CODE) && $this->isCheckoutFlowTypeModal()) {
return $name;
}
}

if ($blockObject = Mage::getSingleton('core/layout')->getBlock($block)) {
return $blockObject->getTemplate();
}

return '';
}

/**
* Get Affirm modal checkout js
*
* @return string
*/
public function getAffirmCheckoutJsScript()
{
if (Mage::helper('affirm')->isCheckoutFlowTypeModal()) {
return 'js/affirm/checkout.js';
}
return '';
}

/**
* Returns a checkout object instance
*
* @return Mage_Checkout_Model_Type_Onepage
*/
public function _getCheckout()
{
return Mage::getSingleton('checkout/type_onepage');
}

/**
* Get OPC save order URL
*
* @return string
*/
public function getOPCCheckoutUrl()
{
$paramHttps = (Mage::app()->getStore()->isCurrentlySecure()) ? array('_forced_secure' => true) : array();
return Mage::getUrl('checkout/onepage/saveOrder/form_key/' . Mage::getSingleton('core/session')->getFormKey(), $paramHttps);
}

public function getAllGroups()
{
$customerGroups = Mage::getResourceModel('customer/group_collection')
Expand Down
152 changes: 119 additions & 33 deletions extension/app/code/community/Affirm/Affirm/Model/Order/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,45 +85,43 @@ protected function _callToPreOrderActionAndExit($order, $quote)
}

/**
* Pre order
* Pre order to be executed only for redirect checkout flow type
*
* @param Varien_Event_Observer $observer
*/
public function preOrder($observer)
{
$order = $observer->getEvent()->getOrder();
$quote = $observer->getEvent()->getQuote();
$methodInst = $order->getPayment()->getMethodInstance();
if (Mage::helper('affirm')->getAffirmTokenCode()) {
$methodInst->setAffirmCheckoutToken(Mage::helper('affirm')->getAffirmTokenCode());
}

if ($this->_isCreateOrderAfterConf($methodInst)) {
if (!Mage::helper('affirm')->getAffirmTokenCode()) {
#ok record the current controller that we are using...
$request = Mage::app()->getRequest();
$orderRequest = array('action' => $request->getActionName(),
'controller' => $request->getControllerName(),
'module' => $request->getModuleName(),
'params' => $request->getParams(),
'method' => $request->getMethod(),
'xhr' => $request->isXmlHttpRequest(),
'POST' => Mage::app()->getRequest()->getPost(), //need post for some cross site issues
'quote_id' => $quote->getId()
);

$orderRequest['routing_info'] = array(
'requested_route' => $request->getRequestedRouteName(),
'requested_controller' => $request->getRequestedControllerName(),
'requested_action' => $request->getRequestedActionName()
);

Mage::helper('affirm')->getCheckoutSession()->setAffirmOrderRequest(serialize($orderRequest));

$this->_callToPreOrderActionAndExit($order, $quote);
if (!Mage::helper('affirm')->isCheckoutFlowTypeModal()) {
$order = $observer->getEvent()->getOrder();
$quote = $observer->getEvent()->getQuote();
$methodInst = $order->getPayment()->getMethodInstance();
if (Mage::helper('affirm')->getAffirmTokenCode()) {
$methodInst->setAffirmCheckoutToken(Mage::helper('affirm')->getAffirmTokenCode());
}
if ($this->_isCreateOrderAfterConf($methodInst)) {
if (!Mage::helper('affirm')->getAffirmTokenCode()) {
#ok record the current controller that we are using...
$request = Mage::app()->getRequest();
$orderRequest = array('action' => $request->getActionName(),
'controller' => $request->getControllerName(),
'module' => $request->getModuleName(),
'params' => $request->getParams(),
'method' => $request->getMethod(),
'xhr' => $request->isXmlHttpRequest(),
'POST' => Mage::app()->getRequest()->getPost(), //need post for some cross site issues
'quote_id' => $quote->getId()
);
$orderRequest['routing_info'] = array(
'requested_route' => $request->getRequestedRouteName(),
'requested_controller' => $request->getRequestedControllerName(),
'requested_action' => $request->getRequestedActionName()
);
Mage::helper('affirm')->getCheckoutSession()->setAffirmOrderRequest(serialize($orderRequest));
$this->_callToPreOrderActionAndExit($order, $quote);
}
} elseif ($this->_isCreateOrderBeforeConf($methodInst)) {
Mage::helper('affirm')->getCheckoutSession()->setAffirmOrderRequest(null);
}
} elseif ($this->_isCreateOrderBeforeConf($methodInst)) {
Mage::helper('affirm')->getCheckoutSession()->setAffirmOrderRequest(null);
}
}

Expand All @@ -142,4 +140,92 @@ public function reactivateQuote($observer)
$quote->save();
}
}

/**
* Modal checkout Before save order
*
* @param Varien_Event_Observer $observer
* @void
*/
public function preDispatchSaveOrderAction(Varien_Event_Observer $observer)
{
if (Mage::helper('affirm')->isCheckoutFlowTypeModal()) {
/* @var $controller Mage_Core_Controller_Front_Action */
$controller = $observer->getEvent()->getControllerAction();
$payment = Mage::helper('affirm')->getCheckoutSession()->getQuote()->getPayment();
$paymentMethod = $payment->getMethod();
if($paymentMethod){
$methodInst = $payment->getMethodInstance();
} else {
$dataSavePayment = $controller->getRequest()->getPost('payment', array());
try {
Mage::getSingleton('checkout/type_onepage')->savePayment($dataSavePayment);
$payment = Mage::helper('affirm')->getCheckoutSession()->getQuote()->getPayment();
$paymentMethod = $payment->getMethod();
$methodInst = $payment->getMethodInstance();
} catch (Exception $e) {
$message = $e->getMessage();
$controller->setFlag('', Mage_Core_Controller_Front_Action::FLAG_NO_DISPATCH, true);
$response = array('error' => -1, 'message' => $message);
$controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
$controller->getRequest()->setDispatched(true);
return;
}
}
if (!Mage::helper('affirm')->getAffirmTokenCode()) {
$requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds();
if ($requiredAgreements && $controller->getRequest()->getPost('agreement', array())) {
$postedAgreements = array_keys($controller->getRequest()->getPost('agreement', array()));
$diff = array_diff($requiredAgreements, $postedAgreements);
if ($diff) {
$result['success'] = false;
$result['error'] = true;
$result['error_messages'] = 'Please agree to all the terms and conditions before placing the order.';
$controller->setFlag('', Mage_Core_Controller_Front_Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setBody(
Mage::helper('core')->jsonEncode($result)
);
$controller->getRequest()->setDispatched(true);
return;
}
}

$data = $controller->getRequest()->getPost('payment', array());
if ($data) {
$data['checks'] = Mage_Payment_Model_Method_Abstract::CHECK_USE_CHECKOUT
| Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_COUNTRY
| Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_CURRENCY
| Mage_Payment_Model_Method_Abstract::CHECK_ORDER_TOTAL_MIN_MAX
| Mage_Payment_Model_Method_Abstract::CHECK_ZERO_TOTAL;
Mage::helper('affirm')->getCheckoutSession()->getQuote()->getPayment()->importData($data);
}

if ($controller->getRequest()->getPost('newsletter')) {
Mage::getSingleton('checkout/session')
->setNewsletterSubsribed(true)
->setNewsletterEmail(
Mage::helper('affirm')->getCheckoutSession()->getQuote()->getCustomerEmail()
);
}
#ok record the current controller that we are using...
$request = Mage::app()->getRequest();
$post = (Mage::app()->getRequest()->getPost()) ? Mage::app()->getRequest()->getPost() : null;
$orderRequest = array('action' => $request->getActionName(),
'controller' => $request->getControllerName(),
'module' => $request->getModuleName(),
'params' => $request->getParams(),
'method' => $request->getMethod(),
'xhr' => $request->isXmlHttpRequest(),
'POST' => $post, //need post for some cross site issues
'quote_id' => Mage::helper('affirm')->getCheckoutSession()->getQuote()->getId()
);
Mage::helper('affirm')->getCheckoutSession()->setAffirmOrderRequest(serialize($orderRequest));
$controller->setFlag('', Mage_Core_Controller_Front_Action::FLAG_NO_DISPATCH, true);
$controller->getRequest()->setDispatched(true);
return;
} else {
$methodInst->setAffirmCheckoutToken(Mage::helper('affirm')->getAffirmTokenCode());
}
}
}
}
Loading

0 comments on commit f3d220f

Please sign in to comment.