PayPal.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ uBw224WvjeTz4R4u5UgnuqAig8adbQ58ZcEFqI9hEBg=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * paypal payment gateway model class
  14. */
  15. namespace Ppb\Model\PaymentGateway;
  16. use Cube\Controller\Request\AbstractRequest,
  17. Cube\Controller\Front,
  18. Ppb\Service\Table\Relational\Locations as LocationsService,
  19. Ppb\Service\Transactions as TransactionsService;
  20. class PayPal extends AbstractPaymentGateway
  21. {
  22. /**
  23. * payment gateway name
  24. */
  25. const NAME = 'PayPal';
  26. /**
  27. * required settings
  28. */
  29. const BUSINESS = 'business';
  30. /**
  31. * form post url
  32. */
  33. const POST_URL = 'https://www.paypal.com/cgi-bin/webscr';
  34. /**
  35. * paypal description
  36. */
  37. protected $_description = 'Click to pay through PayPal.';
  38. public function __construct($userId = null)
  39. {
  40. parent::__construct(self::NAME, $userId);
  41. }
  42. /**
  43. *
  44. * check if the gateway is enabled
  45. *
  46. * @return bool
  47. */
  48. public function enabled()
  49. {
  50. if (!empty($this->_data[self::BUSINESS])) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. *
  57. * get paypal setup form elements
  58. *
  59. * @return array
  60. */
  61. public function getElements()
  62. {
  63. $translate = $this->getTranslate();
  64. return array(
  65. array(
  66. 'form_id' => 'PayPal',
  67. 'id' => self::BUSINESS,
  68. 'element' => 'text',
  69. 'label' => $this->_('PayPal Email Address'),
  70. 'description' => $translate->_('Enter your PayPal registered email address<br>'
  71. . 'PayPal IPN URL: <br>') . $this->getIpnUrl(),
  72. 'attributes' => array(
  73. 'class' => 'form-control input-medium',
  74. ),
  75. // 'validators' => array(
  76. // 'Email'
  77. // ),
  78. ),
  79. );
  80. }
  81. public function formElements()
  82. {
  83. $elements = array(
  84. array(
  85. 'id' => 'cmd',
  86. 'value' => '_xclick',
  87. 'element' => 'hidden',
  88. ),
  89. array(
  90. 'id' => 'bn',
  91. 'value' => 'wa_dw_2.0.4',
  92. 'element' => 'hidden',
  93. ),
  94. array(
  95. 'id' => self::BUSINESS,
  96. 'value' => $this->_data[self::BUSINESS],
  97. 'element' => 'hidden',
  98. ),
  99. array(
  100. 'id' => 'receiver_email',
  101. 'value' => $this->_data[self::BUSINESS],
  102. 'element' => 'hidden',
  103. ),
  104. array(
  105. 'id' => 'item_name',
  106. 'value' => $this->getName(),
  107. 'element' => 'hidden',
  108. ),
  109. array(
  110. 'id' => 'amount',
  111. 'value' => $this->getAmount(),
  112. 'element' => 'hidden',
  113. ),
  114. array(
  115. 'id' => 'currency_code',
  116. 'value' => $this->getCurrency(),
  117. 'element' => 'hidden',
  118. ),
  119. array(
  120. 'id' => 'custom',
  121. 'value' => $this->getTransactionId(),
  122. 'element' => 'hidden',
  123. ),
  124. array(
  125. 'id' => 'notify_url',
  126. 'value' => $this->getIpnUrl(),
  127. 'element' => 'hidden',
  128. ),
  129. array(
  130. 'id' => 'return',
  131. 'value' => $this->getSuccessUrl(),
  132. 'element' => 'hidden',
  133. ),
  134. array(
  135. 'id' => 'cancel_return',
  136. 'value' => $this->getFailureUrl(),
  137. 'element' => 'hidden',
  138. ),
  139. array(
  140. 'id' => 'undefined_quantity',
  141. 'value' => '0',
  142. 'element' => 'hidden',
  143. ),
  144. array(
  145. 'id' => 'no_note',
  146. 'value' => '1',
  147. 'element' => 'hidden',
  148. ),
  149. );
  150. $transactionsService = new TransactionsService();
  151. $transaction = $transactionsService->findBy('id', $this->getTransactionId());
  152. $addressId = null;
  153. if ($transaction->getData('sale_id')) {
  154. $sale = $transaction->findParentRow('\Ppb\Db\Table\Sales');
  155. $addressId = $sale->getData('shipping_address_id');
  156. }
  157. $user = $this->getUser();
  158. $shippingAddress = $user->getAddress($addressId);
  159. if ($shippingAddress !== null) {
  160. $user->setAddress($shippingAddress);
  161. $locationsService = new LocationsService();
  162. $country = $user['country'];
  163. if (is_numeric($country)) {
  164. $row = $locationsService->findBy('id', (int)$country);
  165. if ($row != null) {
  166. $country = strtoupper($row->getData('iso_code'));
  167. }
  168. }
  169. $elements[] = array(
  170. 'id' => 'address_override',
  171. 'value' => '1',
  172. 'element' => 'hidden',
  173. );
  174. $elements[] = array(
  175. 'id' => 'first_name',
  176. 'value' => $user['name']['first'],
  177. 'element' => 'hidden',
  178. );
  179. $elements[] = array(
  180. 'id' => 'last_name',
  181. 'value' => $user['name']['last'],
  182. 'element' => 'hidden',
  183. );
  184. $elements[] = array(
  185. 'id' => 'address1',
  186. 'value' => $shippingAddress['address'],
  187. 'element' => 'hidden',
  188. );
  189. $elements[] = array(
  190. 'id' => 'city',
  191. 'value' => $shippingAddress['city'],
  192. 'element' => 'hidden',
  193. );
  194. $elements[] = array(
  195. 'id' => 'country',
  196. 'value' => $country,
  197. 'element' => 'hidden',
  198. );
  199. $elements[] = array(
  200. 'id' => 'zip',
  201. 'value' => $user['zip_code'],
  202. 'element' => 'hidden',
  203. );
  204. $state = $user['state'];
  205. if (is_numeric($state)) {
  206. $row = $locationsService->findBy('id', (int)$state);
  207. if ($row != null) {
  208. $state = ($country == 'US') ? strtoupper($row->getData('iso_code')) : $row->getData('name');
  209. }
  210. }
  211. $elements[] = array(
  212. 'id' => 'state',
  213. 'value' => $state,
  214. 'element' => 'hidden',
  215. );
  216. $elements[] = array(
  217. 'id' => 'email',
  218. 'value' => $user['email'],
  219. 'element' => 'hidden',
  220. );
  221. }
  222. else {
  223. $elements[] = array(
  224. 'id' => 'no_shipping',
  225. 'value' => '1',
  226. 'element' => 'hidden',
  227. );
  228. }
  229. return $elements;
  230. }
  231. /**
  232. *
  233. * get gateway post url
  234. *
  235. * @return string
  236. */
  237. public function getPostUrl()
  238. {
  239. return self::POST_URL;
  240. }
  241. /**
  242. *
  243. * process ipn
  244. *
  245. * @param \Cube\Controller\Request\AbstractRequest $request
  246. *
  247. * @return bool return true if ipn returns a valid transaction
  248. */
  249. public function processIpn(AbstractRequest $request)
  250. {
  251. $errno = null;
  252. $errstr = null;
  253. $response = false;
  254. if ($request->isPost()) {
  255. $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
  256. if (!$fp) {
  257. $this->setGatewayPaymentStatus($errstr . ' (' . $errno . ')');
  258. }
  259. else {
  260. $content = 'cmd=_notify-validate';
  261. foreach ($request->getParams() as $key => $value) {
  262. $content .= '&' . $key . '=' . urlencode(stripslashes($value));
  263. }
  264. $header = "POST /cgi-bin/webscr HTTP/1.1\r\n"
  265. . "Content-Type: application/x-www-form-urlencoded\r\n"
  266. . "Host: www.paypal.com\r\n"
  267. . "Connection: close\r\n"
  268. . "Content-Length: " . strlen($content) . "\r\n\r\n";
  269. fputs($fp, $header . $content);
  270. $paymentStatus = $_POST['payment_status'];
  271. $this->setTransactionId($_POST['custom'])
  272. ->setAmount($_POST['mc_gross'])
  273. ->setCurrency($_POST['mc_currency'])
  274. ->setGatewayPaymentStatus($paymentStatus)
  275. ->setGatewayTransactionCode($_POST['txn_id']);
  276. while (!feof($fp)) {
  277. $result = trim(fgets($fp, 1024));
  278. if (strcmp($result, "VERIFIED") == 0) {
  279. if ($paymentStatus == "Completed") {
  280. $response = true;
  281. }
  282. }
  283. else if (strcmp($result, "INVALID") == 0) {
  284. $this->setGatewayPaymentStatus($result);
  285. }
  286. }
  287. fclose($fp);
  288. }
  289. }
  290. return $response;
  291. }
  292. }