PageRenderTime 76ms CodeModel.GetById 47ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/Axis/PaymentPaypal/Model/Standard.php

https://github.com/azdcgo/axiscommerce
PHP | 229 lines | 161 code | 21 blank | 47 comment | 28 complexity | 9441312a42f615098a027444d3180759 MD5 | raw file
  1. <?php
  2. /**
  3. * Axis
  4. *
  5. * This file is part of Axis.
  6. *
  7. * Axis is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Axis is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Axis. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Axis
  21. * @package Axis_PaymentPaypal
  22. * @subpackage Axis_PaymentPaypal_Model
  23. * @copyright Copyright 2008-2011 Axis
  24. * @license GNU Public License V3.0
  25. */
  26. /**
  27. *
  28. * @category Axis
  29. * @package Axis_PaymentPaypal
  30. * @subpackage Axis_PaymentPaypal_Model
  31. * @author Axis Core Team <core@axiscommerce.com>
  32. */
  33. class Axis_PaymentPaypal_Model_Standard extends Axis_Method_Payment_Model_Abstract
  34. {
  35. protected $_code = 'Paypal_Standard';
  36. protected $_title = 'PayPal Standard';
  37. public function postProcess(Axis_Sales_Model_Order_Row $order)
  38. {
  39. $view = Axis::app()->getBootstrap()->getResource('layout')->getView();
  40. $delivery = $order->getDelivery();
  41. $formFields = array(
  42. 'business' => $this->_config->email,
  43. 'return' => $view->href('paymentpaypal/standard/success'),
  44. 'cancel_return' => $view->href('paymentpaypal/standard/cancel'),
  45. 'notify_url' => $view->href('paymentpaypal/standard/ipn'),
  46. 'invoice' => $order->id,
  47. 'address_override' => 1,
  48. 'first_name' => $delivery->getFirstname(),
  49. 'last_name' => $delivery->getLastname(),
  50. 'address1' => $delivery->getStreetAddress(),
  51. 'address2' => $delivery->getSuburb(),
  52. 'city' => $delivery->getCity(),
  53. 'state' => $delivery->getZone()->getCode(),
  54. 'country' => $delivery->getCountry()->getIsoCode2(),
  55. 'zip' => $delivery->getPostcode(),
  56. );
  57. if ($this->_config->logo) {
  58. $formFields['cpp_header_image'] = $this->_config->logo;
  59. }
  60. if ($this->_config->paymentAction) {
  61. $formFields['paymentaction'] = strtolower($this->_config->paymentAction);
  62. }
  63. $transaciton_type = $this->_config->transactionType;
  64. /*
  65. O=aggregate cart amount to paypal
  66. I=individual items to paypal
  67. */
  68. if ($transaciton_type == 'Aggregate Cart') {
  69. $businessName = $this->_config->name;
  70. $formFields = array_merge($formFields, array(
  71. 'cmd' => '_ext-enter',
  72. 'redirect_cmd' => '_xclick',
  73. 'item_name' => $businessName ?
  74. $businessName : Axis::config()->core->store->name,
  75. 'currency_code' => $this->getBaseCurrencyCode(),
  76. 'amount' => sprintf('%.2f', $this->getAmountInBaseCurrency(
  77. $order->getSubTotal(), $order->currency
  78. )),
  79. ));
  80. $tax = $order->getTax();
  81. $shippingTax = $order->getShippingTax();
  82. $tax = sprintf('%.2f', $tax + $shippingTax);
  83. if ($tax > 0) {
  84. $formFields['tax'] = $tax;
  85. }
  86. } else {
  87. $formFields = array_merge($formFields, array(
  88. 'cmd' => '_cart',
  89. 'upload' => '1',
  90. ));
  91. $products = $order->getProducts();
  92. if ($products) {
  93. $i = 1;
  94. foreach($products as $product) {
  95. $formFields = array_merge($formFields, array(
  96. 'item_name_' . $i => $product['name'],
  97. 'item_number_' . $i => $product['sku'],
  98. 'quantity_' . $i => intval($product['quantity']),
  99. 'amount_' . $i => sprintf('%.2f', $product['final_price']),
  100. ));
  101. if($product['tax'] > 0) {
  102. $formFields = array_merge($formFields, array(
  103. 'tax_' . $i => sprintf('%.2f', $product['tax']/*/$item['quantity']*/),
  104. ));
  105. }
  106. $i++;
  107. }
  108. }
  109. }
  110. $totals = $order->getTotals();
  111. $shipping = sprintf('%.2f', $totals['shipping']['value']);
  112. if ($shipping > 0) {
  113. if ($transaciton_type == 'Aggregate Cart') {
  114. $formFields['shipping'] = $shipping;
  115. } else {
  116. $shippingTax = $totals['shipping_tax']['value'];
  117. $formFields = array_merge($formFields, array(
  118. 'item_name_' . $i => $order->shipping_method,
  119. 'quantity_' . $i => 1,
  120. 'amount_' . $i => $shipping,
  121. 'tax_' . $i => sprintf('%.2f', $shippingTax),
  122. ));
  123. $i++;
  124. }
  125. }
  126. $sReq = '';
  127. $rArr = array();
  128. foreach ($formFields as $k => $v) {
  129. /*
  130. replacing & char with and. otherwise it will break the post
  131. */
  132. $value = str_replace('&', 'and', $v);
  133. $rArr[$k] = $value;
  134. $sReq .= '&' . $k . '=' . $value;
  135. }
  136. $this->getStorage()->formFields = $rArr;
  137. return array(
  138. 'redirect' => $view->href('paymentpaypal/standard/submit')
  139. );
  140. }
  141. /**
  142. * "processing" actually
  143. *
  144. * @param array $post
  145. * @return null
  146. */
  147. public function ipnSubmit($post)
  148. {
  149. if (isset($post['module'])) { unset($post['module']);}
  150. if (isset($post['controller'])) { unset($post['controller']);}
  151. if (isset($post['action'])) { unset($post['action']);}
  152. if (isset($post['locale'])) { unset($post['locale']);}
  153. $request = '';
  154. foreach($post as $key => $value) {
  155. $request .= '&' . $key . '=' . urlencode(stripslashes($value));
  156. }
  157. //append ipn commdn
  158. $request .= "&cmd=_notify-validate";
  159. $request = substr($request, 1);
  160. $httpClient = new Zend_Http_Client();
  161. $uri = $this->_config->url . '?' . $request;
  162. $httpClient->setUri($uri);
  163. $response = $httpClient->request('POST')->getBody();
  164. $order = Axis::single('sales/order')->find($post['invoice'])->current();
  165. if (!$order) {
  166. return;
  167. }
  168. if ($response != 'VERIFIED') {
  169. /* Canceled_Reversal, Completed, Denied, Expired, Failed
  170. Pending, Processed, Refunded, Reversed, Voided*/
  171. $comment = $post['payment_status'];
  172. if ($post['payment_status'] == 'Pending') {
  173. $comment .= ' - ' . $post['pending_reason'];
  174. } elseif ( ($post['payment_status'] == 'Reversed')
  175. || ($post['payment_status'] == 'Refunded') ) {
  176. $comment .= ' - ' . $post['reason_code'];
  177. }
  178. $order->addComment(
  179. Axis::translate('checkout')->__(
  180. "Paypal IPN Invalid %s.", $comment
  181. )
  182. );
  183. return;
  184. }
  185. if ($post['mc_gross'] != $order->order_total) {
  186. $order->addComment(
  187. Axis::translate('checkout')->__(
  188. 'Order total amount does not match paypal gross total amount'
  189. )
  190. );
  191. return;
  192. }
  193. Axis::single('paymentPaypal/standard_order')->insert(array(
  194. 'order_id' => $order->id,
  195. 'trans_id' => $post['txn_id'],
  196. 'status' => $post['payment_status']
  197. ));
  198. $message = Axis::translate('checkout')->__(
  199. "Received IPN verification"
  200. );
  201. if ($post['payment_status'] == 'Completed') {
  202. $message = Axis::translate('checkout')->__(
  203. "Transaction in sale mode"
  204. );
  205. }
  206. $order->setStatus('processing', $message, true);
  207. }
  208. }