PayPalSandbox.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ mv8+4GZyaXK9rXNy6v7/aUJqMS5QvAJJ6z0rheUNJNQ=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.2
  11. */
  12. /**
  13. * paypal sandbox payment gateway model class
  14. */
  15. namespace Ppb\Model\PaymentGateway;
  16. use Cube\Controller\Request\AbstractRequest;
  17. class PayPalSandbox extends AbstractPaymentGateway
  18. {
  19. /**
  20. * payment gateway name
  21. */
  22. const NAME = 'PayPalSandbox';
  23. /**
  24. * required settings
  25. */
  26. const BUSINESS = 'sandbox_business';
  27. /**
  28. * form post url (sandbox)
  29. */
  30. const POST_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
  31. /**
  32. * paypal description
  33. */
  34. protected $_description = 'Click to pay through PayPal (sandbox mode).';
  35. public function __construct($userId = null)
  36. {
  37. parent::__construct(self::NAME, $userId);
  38. }
  39. /**
  40. *
  41. * check if the gateway is enabled
  42. *
  43. * @return bool
  44. */
  45. public function enabled()
  46. {
  47. if (!empty($this->_data[self::BUSINESS])) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. *
  54. * get paypal setup form elements
  55. *
  56. * @return array
  57. */
  58. public function getElements()
  59. {
  60. $translate = $this->getTranslate();
  61. return array(
  62. array(
  63. 'form_id' => 'PayPalSandbox',
  64. 'id' => self::BUSINESS,
  65. 'element' => 'text',
  66. 'label' => $this->_('PayPal Sandbox Email Address'),
  67. 'description' => $translate->_('Enter your PayPal Sandbox registered email address<br>'
  68. . 'PayPal IPN URL: <br>') . $this->getIpnUrl(),
  69. 'attributes' => array(
  70. 'class' => 'form-control input-medium',
  71. ),
  72. ),
  73. );
  74. }
  75. public function formElements()
  76. {
  77. return array(
  78. array(
  79. 'id' => 'cmd',
  80. 'value' => '_xclick',
  81. 'element' => 'hidden',
  82. ),
  83. array(
  84. 'id' => 'bn',
  85. 'value' => 'wa_dw_2.0.4',
  86. 'element' => 'hidden',
  87. ),
  88. array(
  89. 'id' => 'business',
  90. 'value' => $this->_data[self::BUSINESS],
  91. 'element' => 'hidden',
  92. ),
  93. array(
  94. 'id' => 'receiver_email',
  95. 'value' => $this->_data[self::BUSINESS],
  96. 'element' => 'hidden',
  97. ),
  98. array(
  99. 'id' => 'item_name',
  100. 'value' => $this->getName(),
  101. 'element' => 'hidden',
  102. ),
  103. array(
  104. 'id' => 'amount',
  105. 'value' => $this->getAmount(),
  106. 'element' => 'hidden',
  107. ),
  108. array(
  109. 'id' => 'currency_code',
  110. 'value' => $this->getCurrency(),
  111. 'element' => 'hidden',
  112. ),
  113. array(
  114. 'id' => 'custom',
  115. 'value' => $this->getTransactionId(),
  116. 'element' => 'hidden',
  117. ),
  118. array(
  119. 'id' => 'notify_url',
  120. 'value' => $this->getIpnUrl(),
  121. 'element' => 'hidden',
  122. ),
  123. array(
  124. 'id' => 'return',
  125. 'value' => $this->getSuccessUrl(),
  126. 'element' => 'hidden',
  127. ),
  128. array(
  129. 'id' => 'cancel_return',
  130. 'value' => $this->getFailureUrl(),
  131. 'element' => 'hidden',
  132. ),
  133. array(
  134. 'id' => 'undefined_quantity',
  135. 'value' => '0',
  136. 'element' => 'hidden',
  137. ),
  138. array(
  139. 'id' => 'no_shipping',
  140. 'value' => '1',
  141. 'element' => 'hidden',
  142. ),
  143. array(
  144. 'id' => 'no_note',
  145. 'value' => '1',
  146. 'element' => 'hidden',
  147. ),
  148. );
  149. }
  150. /**
  151. *
  152. * get gateway post url
  153. *
  154. * @return string
  155. */
  156. public function getPostUrl()
  157. {
  158. return self::POST_URL;
  159. }
  160. /**
  161. *
  162. * process ipn
  163. *
  164. * @param \Cube\Controller\Request\AbstractRequest $request
  165. *
  166. * @return bool return true if ipn returns a valid transaction
  167. */
  168. public function processIpn(AbstractRequest $request)
  169. {
  170. $errno = null;
  171. $errstr = null;
  172. $response = false;
  173. if ($request->isPost()) {
  174. $fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
  175. if (!$fp) {
  176. $this->setGatewayPaymentStatus($errstr . ' (' . $errno . ')');
  177. }
  178. else {
  179. $content = 'cmd=_notify-validate';
  180. foreach ($request->getParams() as $key => $value) {
  181. $content .= '&' . $key . '=' . urlencode(stripslashes($value));
  182. }
  183. $header = "POST /cgi-bin/webscr HTTP/1.1\r\n"
  184. . "Content-Type: application/x-www-form-urlencoded\r\n"
  185. . "Host: www.paypal.com\r\n"
  186. . "Connection: close\r\n"
  187. . "Content-Length: " . strlen($content) . "\r\n\r\n";
  188. fputs($fp, $header . $content);
  189. $paymentStatus = $_POST['payment_status'];
  190. $this->setTransactionId($_POST['custom'])
  191. ->setAmount($_POST['mc_gross'])
  192. ->setCurrency($_POST['mc_currency'])
  193. ->setGatewayPaymentStatus($paymentStatus)
  194. ->setGatewayTransactionCode($_POST['txn_id']);
  195. while (!feof($fp)) {
  196. $result = trim(fgets($fp, 1024));
  197. if (strcmp($result, "VERIFIED") == 0) {
  198. if ($paymentStatus == "Completed") {
  199. $response = true;
  200. }
  201. }
  202. else if (strcmp($result, "INVALID") == 0) {
  203. $this->setGatewayPaymentStatus($result);
  204. }
  205. }
  206. fclose($fp);
  207. }
  208. }
  209. return $response;
  210. }
  211. }