/kohana_core/modules/payment/libraries/drivers/Payment/Paypalpro.php

https://gitlab.com/vince.omega/mcb-nov-build · PHP · 229 lines · 129 code · 56 blank · 44 comment · 8 complexity · 1a81ae377aaf509c6fa9644be8993c19 MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Paypay (website payments pro) Payment Driver
  4. *
  5. *
  6. * @package Payment
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2008 Kohana Team
  9. * @license http://kohanaphp.com/license.html
  10. */
  11. class Payment_Paypalpro_Driver implements Payment_Driver
  12. {
  13. // Fields required to do a transaction
  14. /**
  15. * these are the required fields for the API method DoDirectPayment
  16. * other API methods and the associated fields required are listed here: http://tinyurl.com/2cffgr
  17. *
  18. * *note - paypalpro API field names are not case sensitive
  19. */
  20. private $required_fields = array
  21. (
  22. 'ENDPOINT' => FALSE,
  23. 'USER' => FALSE,
  24. 'PWD' => FALSE,
  25. 'SIGNATURE' => FALSE,
  26. 'VERSION' => FALSE,
  27. 'METHOD' => FALSE,
  28. 'PAYMENTACTION' => FALSE,
  29. 'CURRENCYCODE' => FALSE, // default is USD - only required if other currency needed
  30. 'AMT' => FALSE, // payment amount
  31. 'IPADDRESS' => FALSE,
  32. 'FIRSTNAME' => FALSE,
  33. 'LASTNAME' => FALSE,
  34. 'CREDITCARDTYPE' => FALSE,
  35. 'ACCT' => FALSE, // card number
  36. 'EXPDATE' => FALSE,
  37. 'CVV2' => FALSE
  38. );
  39. private $fields = array
  40. (
  41. 'ENDPOINT' => '',
  42. 'USER' => '',
  43. 'PWD' => '',
  44. 'SIGNATURE' => '',
  45. 'VERSION' => '',
  46. 'METHOD' => '',
  47. /* some possible values for METHOD :
  48. 'DoDirectPayment',
  49. 'RefundTransaction',
  50. 'DoAuthorization',
  51. 'DoReauthorization',
  52. 'DoCapture',
  53. 'DoVoid'
  54. */
  55. 'PAYMENTACTION' => '',
  56. 'CURRENCYCODE' => '',
  57. 'AMT' => 0, // payment amount
  58. 'IPADDRESS' => '',
  59. 'FIRSTNAME' => '',
  60. 'LASTNAME' => '',
  61. 'CREDITCARDTYPE' => '',
  62. 'ACCT' => '', // card number
  63. 'EXPDATE' => '', // Format: MMYYYY
  64. 'CVV2' => '', // security code
  65. // -- OPTIONAL FIELDS --
  66. 'STREET' => '',
  67. 'STREET2' => '',
  68. 'CITY' => '',
  69. 'STATE' => '',
  70. 'ZIP' => '',
  71. 'COUNTRYCODE' => '',
  72. 'SHIPTONAME' => '',
  73. 'SHIPTOSTREET' => '',
  74. 'SHIPTOSTREET2' => '',
  75. 'SHIPTOCITY' => '',
  76. 'SHIPTOSTATE' => '',
  77. 'SHIPTOZIP' => '',
  78. 'SHIPTOCOUNTRYCODE' => '',
  79. 'INVNUM' => '' // your internal order id / transaction id
  80. // other optional fields listed here:
  81. // https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2145100
  82. );
  83. private $test_mode = TRUE;
  84. /**
  85. * Sets the config for the class.
  86. *
  87. * @param : array - config passed from the payment library constructor
  88. */
  89. public function __construct($config)
  90. {
  91. $this->test_mode = $config['test_mode'];
  92. if ($this->test_mode)
  93. {
  94. $this->fields['USER'] = $config['SANDBOX_USER'];
  95. $this->fields['PWD'] = $config['SANDBOX_PWD'];
  96. $this->fields['SIGNATURE'] = $config['SANDBOX_SIGNATURE'];
  97. $this->fields['ENDPOINT'] = $config['SANDBOX_ENDPOINT'];
  98. }
  99. else
  100. {
  101. $this->fields['USER'] = $config['USER'];
  102. $this->fields['PWD'] = $config['PWD'];
  103. $this->fields['SIGNATURE'] = $config['SIGNATURE'];
  104. $this->fields['ENDPOINT'] = $config['ENDPOINT'];
  105. }
  106. $this->fields['VERSION'] = $config['VERSION'];
  107. $this->fields['CURRENCYCODE'] = $config['CURRENCYCODE'];
  108. $this->required_fields['USER'] = !empty($config['USER']);
  109. $this->required_fields['PWD'] = !empty($config['PWD']);
  110. $this->required_fields['SIGNATURE'] = !empty($config['SIGNATURE']);
  111. $this->required_fields['ENDPOINT'] = !empty($config['ENDPOINT']);
  112. $this->required_fields['VERSION'] = !empty($config['VERSION']);
  113. $this->required_fields['CURRENCYCODE'] = !empty($config['CURRENCYCODE']);
  114. $this->curl_config = $config['curl_config'];
  115. Kohana::log('debug', 'Paypalpro Payment Driver Initialized');
  116. }
  117. /**
  118. *@desc set fields for nvp string
  119. */
  120. public function set_fields($fields)
  121. {
  122. foreach ((array) $fields as $key => $value)
  123. {
  124. $this->fields[$key] = $value;
  125. if (array_key_exists($key, $this->required_fields) and !empty($value))
  126. {
  127. $this->required_fields[$key] = TRUE;
  128. }
  129. }
  130. }
  131. /**
  132. *@desc process PayPal Website Payments Pro transaction
  133. */
  134. public function process()
  135. {
  136. // Check for required fields
  137. if (in_array(FALSE, $this->required_fields))
  138. {
  139. $fields = array();
  140. foreach ($this->required_fields as $key => $field)
  141. {
  142. if (!$field) $fields[] = $key;
  143. }
  144. throw new Kohana_Exception('payment.required', implode(', ', $fields));
  145. }
  146. // Instantiate curl and pass the API post url
  147. $ch = curl_init($this->fields['ENDPOINT']);
  148. foreach ($this->fields as $key => $val)
  149. {
  150. // don't include unset optional fields in the name-value pair request string
  151. if ($val==='' OR $key=='ENDPOINT') unset($this->fields[$key]);
  152. }
  153. $nvp_qstr = http_build_query($this->fields);
  154. // Set custom curl options
  155. curl_setopt_array($ch, $this->curl_config);
  156. // Set the curl POST fields
  157. curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp_qstr);
  158. // Execute post and get results
  159. $response = curl_exec($ch);
  160. if (curl_errno($ch))
  161. {
  162. $curl_error_no = curl_errno($ch);
  163. $curl_error_msg = curl_error($ch);
  164. throw new Kohana_Exception('curl.error:'.$curl_error_no.' - '.$curl_error_msg);
  165. }
  166. curl_close ($ch);
  167. if (!$response)
  168. throw new Kohana_Exception('payment.gateway_connection_error');
  169. $nvp_res_array = array();
  170. parse_str(urldecode($response),$nvp_res_array);
  171. return ($nvp_res_array['ACK'] == TRUE);
  172. }
  173. } // End Payment_Paypalpro_Driver Class