Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. elseif($status == 'CANCELLED'){
  2. //user cancel, let them try again?
  3.  
  4. Mage::log("LAYBUY: CUSTOMER CANCELLED");
  5.  
  6. if ($this->getLastQuoteId()) {
  7.  
  8. $lastQuoteId = $this->getLastQuoteId();
  9. $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
  10. $quote->setIsActive(TRUE)->save();
  11.  
  12.  
  13. // Let customer know what's happened
  14. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled.");
  15.  
  16. $this->_redirect('checkout/onepage'); //Redirect to cart
  17. return;
  18. }
  19.  
  20. // could not get a valid quote, send customer to fail
  21. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled, sorry we could not find your cart.");
  22.  
  23. $this->_redirect('checkout/onepage/failure', ['_secure' => TRUE] );
  24.  
  25. }
  26.  
  27. // The response action is triggered when Laybuy sends back a response after processing the customer's payment
  28. // GET /laybuypayments/payment/response/?status=SUCCESS&token=z8jFQf31BbRN3fEmjUbrxYZhQ6bwTtNNXoyCTpjo
  29.  
  30. public function responseAction() {
  31. $session = Mage::getSingleton('checkout/session');
  32. /* @var $laybuyPayment Laybuy_Payments_Model_Payments */
  33. $laybuyPayment = Mage::getSingleton('payments/payments'); // Laybuy_Payments_Model_Payments
  34.  
  35. // we are always in session with Laybuy
  36. // Mage::log("response action ");
  37.  
  38. // GET /laybuypayments/payment/response/?status=SUCCESS&token=z8jFQf31BbRN3fEmjUbrxYZhQ6bwTtNNXoyCTpjo HTTP/1.1
  39. $status = $this->getRequest()->getParam('status');
  40.  
  41. //
  42. // This action is used for all returns, handle each one
  43. //
  44.  
  45. // returning from a successful payment
  46. //
  47. if($status == 'SUCCESS') {
  48.  
  49. Mage::log("LAYBUY: CUSTOMER RETURNS WITH SUCCESSFUL STATUS");
  50.  
  51. // setup our client to talk with Laybuy
  52. $client = $this->getLaybuyClient();
  53.  
  54. // finalise the order
  55. $laybuy = new stdClass();
  56. $laybuy->token = $this->getRequest()->getParam('token');
  57.  
  58. //Mage::log(json_encode($laybuy));
  59.  
  60. // finiase order
  61. $response = $client->restPost('/order/confirm', json_encode($laybuy));
  62. $body = json_decode($response->getBody());
  63.  
  64. //Mage::log($body);
  65.  
  66. // laybuy confirmed our order
  67. // we can setup teh invoice and mark the order as processing
  68. if ($body->result == 'SUCCESS') {
  69.  
  70. Mage::log("LAYBUY: ORDER SUCCESSFULLY CONFIRMED");
  71.  
  72. $layby_order_id = $body->orderId;
  73.  
  74. $response = $client->restGet('/order/'. $layby_order_id );
  75. $laybuy_order = json_decode($response->getBody());
  76.  
  77. //Mage::log($laybuy_order);
  78.  
  79. $order_id = $laybuy_order->merchantReference;
  80.  
  81. $session->setLastOrderId($order_id)
  82. ->setLastRealOrderId((string) $order_id);
  83.  
  84. /* @var $order Mage_Sales_Model_Order */
  85. $order = Mage::getModel('sales/order');
  86. $order->loadByIncrementId($order_id);
  87. $order->addStatusHistoryComment("Laybuy payment approved." , FALSE);
  88.  
  89. $order->sendNewOrderEmail();
  90. $order->setEmailSent(TRUE);
  91.  
  92.  
  93. $invoice = $order->prepareInvoice();
  94.  
  95. if ($invoice->getTotalQty()) { // incase its a zero order? not sure what Laybuy would do here
  96.  
  97. $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
  98.  
  99. $invoice->register();
  100.  
  101. Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
  102.  
  103. $invoice->sendEmail(TRUE); //Convert this into a config option? DOes this get sent if Mage is set not to send?
  104.  
  105. }
  106.  
  107. $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, TRUE, 'Laybuy has approved payment, with Laybuy order id: ' . $layby_order_id );
  108.  
  109. $order->save();
  110.  
  111.  
  112. Mage::getSingleton('checkout/session')->unsQuoteId();
  113. Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', ['_secure' => FALSE]);
  114.  
  115. }
  116.  
  117.  
  118. }
  119.  
  120. // USER cncelled their payment process at Laybuy, they may wont to continue
  121. elseif($status == 'CANCELLED'){
  122. //user cancel, let them try again?
  123.  
  124. Mage::log("LAYBUY: CUSTOMER CANCELLED");
  125.  
  126. if ($this->getLastQuoteId()) {
  127.  
  128. $lastQuoteId = $this->getLastQuoteId();
  129. $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
  130. $quote->setIsActive(TRUE)->save();
  131.  
  132.  
  133. // Let customer know what's happened
  134. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled.");
  135.  
  136. $this->_redirect('checkout/onepage'); //Redirect to cart
  137. return;
  138. }
  139.  
  140. // could not get a valid quote, send customer to fail
  141. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled, sorry we could not find your cart.");
  142.  
  143. $this->_redirect('checkout/onepage/failure', ['_secure' => TRUE] );
  144.  
  145. }
  146. else {
  147.  
  148. Mage::log("LAYBUY: PAYMENT DELCINED OR CREDIT CHECK FAILED");
  149.  
  150.  
  151. if ($this->getLastQuoteId()) {
  152.  
  153. $lastQuoteId = $this->getLastQuoteId();
  154. $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
  155. $quote->setIsActive(TRUE)->save();
  156.  
  157.  
  158. // Let customer know its bad
  159. Mage::getSingleton('core/session')->addError("Sorry, you have been declined a Laybuy.");
  160.  
  161. $this->_redirect('checkout/onepage'); //Redirect to cart
  162. return;
  163. }
  164.  
  165. Mage::getSingleton('core/session')->addError("Sorry, you have been declined a Laybuy and we could not find your cart.");
  166. $this->_redirect('checkout/onepage/failure', ['_secure' => TRUE]);
  167.  
  168. }
  169.  
  170.  
  171. }
  172.  
  173. /**
  174. * Get quote of checkout session
  175. *
  176. * @return Mage_Sales_Model_Quote
  177. */
  178. private function getLastQuoteId() {
  179.  
  180. return Mage::getSingleton('checkout/session')->getLastQuoteId();
  181. }
  182.  
  183. /**
  184. * Cancel order
  185. */
  186. public function cancelAction() {
  187.  
  188. //user cancel, let them try again?
  189.  
  190. Mage::log("LAYBUY: CUSTOMER CANCELLED");
  191.  
  192. if ($this->getLastQuoteId()) {
  193.  
  194. $lastQuoteId = $this->getLastQuoteId();
  195. $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
  196. $quote->setIsActive(TRUE)->save();
  197.  
  198.  
  199. // Let customer know what's happened
  200. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled.");
  201.  
  202. $this->_redirect('checkout/onepage'); //Redirect to cart
  203. return;
  204. }
  205.  
  206. // could not get a valid quote, send customer to fail
  207. Mage::getSingleton('core/session')->addError("Your Laybuy payment has been cancelled, sorry we could not find your cart.");
  208.  
  209. $this->_redirect('checkout/onepage/failure', ['_secure' => TRUE]);
  210.  
  211. }
  212.  
  213.  
  214.  
  215. private function getLaybuyClient() {
  216.  
  217. $laybuy_sandbox = $this->getConfigData('sandbox_mode') == 1;
  218.  
  219. if ($laybuy_sandbox) {
  220. $laybuy_merchantid = $this->getConfigData('sandbox_merchantid');
  221. $laybuy_apikey = $this->getConfigData('sandbox_apikey');
  222. $url = self::LAYBUY_SANDBOX_URL;
  223. }
  224. else {
  225. $laybuy_merchantid = $this->getConfigData('live_merchantid');
  226. $laybuy_apikey = $this->getConfigData('live_apikey');
  227. $url = self::LAYBUY_LIVE_URL;
  228. }
  229.  
  230.  
  231. try {
  232. $client = new Zend_Rest_Client( $url );
  233. $client->getHttpClient()->setAuth(
  234. $laybuy_merchantid,
  235. $laybuy_apikey,
  236. Zend_Http_Client::AUTH_BASIC);
  237.  
  238. } catch (Exception $e) {
  239.  
  240. Mage::logException($e);
  241. //Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
  242.  
  243. Mage::log(__METHOD__ . ': LAYBUY CLIENT FAILED: ' . $laybuy_merchantid . ":< apikey >");
  244.  
  245. $result['success'] = FALSE;
  246. $result['error'] = TRUE;
  247. $result['error_messages'] = $this->__('There was an error processing your order. Please contact us or try again later. [Laybuy connect]');
  248.  
  249. // Let customer know its real bad
  250. Mage::getSingleton('core/session')->addError($result['error_messages'] );
  251. }
  252.  
  253. return $client;
  254.  
  255.  
  256. }
  257.  
  258. private function getConfigData($field, $storeId = NULL) {
  259.  
  260. $path = 'payment/laybuy_payments/' . $field;
  261. return Mage::getStoreConfig($path, $storeId);
  262.  
  263. }
  264.  
  265. /**
  266. * Get quote of checkout session
  267. *
  268. * @return Mage_Sales_Model_Quote
  269. */
  270. private function getQuote() {
  271. if (is_null($this->quote)) {
  272. $this->quote = Mage::getSingleton('checkout/session')->getQuote();
  273. }
  274. return $this->quote;
  275. }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement