Skip to content

Commit

Permalink
Force checkout preorder to always redirect to the affirm one, also ad…
Browse files Browse the repository at this point in the history
…ded config value to force xhr.
  • Loading branch information
crazybuster committed Dec 1, 2014
1 parent 4d724de commit 2918d2b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
15 changes: 15 additions & 0 deletions extension/app/code/community/Affirm/Affirm/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Affirm_Affirm_Model_Payment extends Mage_Payment_Model_Method_Abstract
// TODO(brian): extract this along with API client
const API_CHARGES_PATH = '/api/v2/charges/';

const CHECKOUT_XHR_AUTO = 'auto';
const CHECKOUT_XHR = 'xhr';
const CHECKOUT_REDIRECT = 'redirect';

/**
* Form block type
*/
Expand Down Expand Up @@ -424,6 +428,17 @@ private static function _getMetadata()
"version" => Mage::getConfig()->getModuleConfig('Affirm_Affirm')->version
)
);

if (Mage::app()->getStore()->isAdmin()) {
//this is in the admin area..
$meta["source"]["merchant_user_initiated"] = 1;
$user = Mage::getSingleton('admin/session')->getUser();
if ($user) {
$meta["source"]["data"]["merchant_logged_in"] = 1;
$meta["source"]["data"]["merchant_username"] = $user->getUsername();
}
}

if ($session->isLoggedIn()) {
$customerId = $session->getCustomerId();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
class Affirm_Affirm_Model_Source_PaymentCheckoutXhr
{
public function toOptionArray()
{
return array(
array(
'value' => Affirm_Affirm_Model_Payment::CHECKOUT_XHR_AUTO,
'label' => Mage::helper('affirm')->__('Auto Detect')
),
array(
'value' => Affirm_Affirm_Model_Payment::CHECKOUT_XHR,
'label' => Mage::helper('affirm')->__('Checkout uses xhr')
),
array(
'value' => Affirm_Affirm_Model_Payment::CHECKOUT_REDIRECT,
'label' => Mage::helper('affirm')->__('Checkout uses redirect')
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,36 @@ public function redirectAction()
$session->unsRedirectUrl();
}

private function isXhrRequest($proxy_request)
{
$detected_xhr = isset($proxy_request["xhr"]) && $proxy_request["xhr"];
$config_xhr = Mage::getStoreConfig("payment/affirm/detect_xhr_checkout");

if ($config_xhr == Affirm_Affirm_Model_Payment::CHECKOUT_REDIRECT)
{
return false;
}
elseif ($config_xhr == Affirm_Affirm_Model_Payment::CHECKOUT_XHR)
{
return true;
}
else
{
return $detected_xhr;
}
}

public function renderPreOrderAction()
{
$order = $this->getRequest()->getParam("order");
$string = $this->getLayout()->createBlock('affirm/payment_redirect')->setOrder($order)->toHtml();
$serialized_request = Mage::getSingleton('checkout/session')->getAffirmOrderRequest();
$proxy_request = unserialize($serialized_request);

if (isset($proxy_request["xhr"]) && $proxy_request["xhr"])
if ($this->isXhrRequest($proxy_request))
{
$this->_getCheckoutSession()->setPreOrderRender($string);
$result = array("redirect"=>Mage::getUrl('*/*/redirectPreOrder'));
$result = array("redirect"=>Mage::getUrl('affirm/payment/redirectPreOrder'));
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
else
Expand Down Expand Up @@ -83,8 +102,7 @@ public function confirmAction()
Mage::register("affirm_token_code", $checkout_token);
$this->_forward($proxy_request["action"], $proxy_request["controller"], $proxy_request["module"], $proxy_request["params"]);


if ((isset($proxy_request["xhr"]) && $proxy_request["xhr"]))
if ($this->isXhrRequest($proxy_request))
{
#need to actually execute the forward!
$front = Mage::app()->getFrontController();
Expand All @@ -95,6 +113,15 @@ public function confirmAction()
}
}

//
//It's already redirecting... so let it do so
//This should never happen, but if it does we should probably let it go through
//
if ($this->getResponse()->getHttpResponseCode() == 302)
{
return;
}

try {
$orderResult = Mage::helper('core')->jsonDecode($this->getResponse()->getBody());
} catch (Exception $e) {
Expand Down
9 changes: 9 additions & 0 deletions extension/app/code/community/Affirm/Affirm/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@
<show_in_website>1</show_in_website>
<sort_order>210</sort_order>
</plain_text_title_enabled>
<detect_xhr_checkout translate="label">
<label>Checkout uses xhr</label>
<frontend_type>select</frontend_type>
<source_model>affirm/source_paymentCheckoutXhr</source_model>
<sort_order>220</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</detect_xhr_checkout>
</fields>
</affirm>
</groups>
Expand Down

0 comments on commit 2918d2b

Please sign in to comment.