/application/libraries/payments/paypal.php

https://github.com/calvinfroedge/CodeIgniter-Payments-Library · PHP · 283 lines · 177 code · 43 blank · 63 comment · 5 complexity · 1e833065ea3b56b954974b1357965cef MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class PayPal
  3. {
  4. function __construct()
  5. {
  6. $this->ci =& get_instance();
  7. $this->ci->load->config('payments');
  8. $this->ci->load->config('payments/paypal');
  9. $this->endpoint = $this->ci->config->item('paypal_api_endpoint');
  10. $this->settings = array(
  11. 'USER' => $this->ci->config->item('paypal_api_username'),
  12. 'PWD' => $this->ci->config->item('paypal_api_password'),
  13. 'VERSION' => $this->ci->config->item('paypal_api_version'),
  14. 'SIGNATURE' => $this->ci->config->item('paypal_api_signature'),
  15. );
  16. }
  17. /**
  18. * Get profile info for a particular profile id
  19. *
  20. * @param array
  21. * @return object
  22. */
  23. public function get_recurring_profile_info($profile_id)
  24. {
  25. $function_settings = array(
  26. 'DESC' => $this->ci->config->item('paypal_api_service_description'),
  27. 'METHOD' => 'GetRecurringPaymentsProfileDetails'
  28. );
  29. $data = array(
  30. 'ProfileID' => $profile_id
  31. );
  32. $return_data = $this->handle_query(array_merge($this->settings, $function_settings), $data, $this->endpoint);
  33. $return_array = array(
  34. 'profile_id' => $return_data->response['PROFILEID'],
  35. 'status' => $return_data->response['STATUS'],
  36. 'next_billing_date' => $return_data->response['NEXTBILLINGDATE'],
  37. 'amount' => $return_data->response['AMT'],
  38. 'billing_period' => $return_data->response['BILLINGPERIOD'],
  39. 'billing_frequency' => $return_data->response['BILLINGFREQUENCY'],
  40. 'failedpayments' => $return_data->response['FAILEDPAYMENTCOUNT'],
  41. 'billing_method' => $this->ci->config->item('payment-system_paypal'),
  42. 'billing_type' => $this->ci->config->item('recurring_payment-type')
  43. );
  44. return (object) $return_array;
  45. }
  46. /**
  47. * Create a new recurring payment
  48. *
  49. * @param array
  50. * @return object
  51. */
  52. public function make_recurring_payment($billing_data, $trial = FALSE)
  53. {
  54. $billing_keys = array(
  55. 'CREDITCARDTYPE',
  56. 'ACCT',
  57. 'EXPDATE',
  58. 'FIRSTNAME',
  59. 'LASTNAME',
  60. 'PROFILESTARTDATE',
  61. 'BILLINGPERIOD',
  62. 'BILLINGFREQUENCY',
  63. 'AMT',
  64. 'MAXFAILEDPAYMENTS'
  65. );
  66. if($trial)
  67. {
  68. $billing_keys = array(
  69. 'CREDITCARDTYPE',
  70. 'ACCT',
  71. 'EXPDATE',
  72. 'FIRSTNAME',
  73. 'LASTNAME',
  74. 'PROFILESTARTDATE',
  75. 'BILLINGPERIOD',
  76. 'BILLINGFREQUENCY',
  77. 'AMT',
  78. 'MAXFAILEDPAYMENTS',
  79. 'TRIALBILLINGPERIOD',
  80. 'TRIALBILLINGFREQUENCY',
  81. 'TRIALAMT',
  82. 'TRIALTOTALBILLINGCYCLES'
  83. );
  84. }
  85. $function_settings = array(
  86. 'DESC' => $this->ci->config->item('paypal_api_service_description'),
  87. 'METHOD' => 'CreateRecurringPaymentsProfile'
  88. );
  89. return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($billing_keys, $billing_data), $this->endpoint);
  90. }
  91. /**
  92. * Update an existing payments subscription
  93. *
  94. * @param array
  95. * @return object
  96. */
  97. public function update_billing_info($billing_data)
  98. {
  99. $billing_keys = array(
  100. 'PROFILEID',
  101. 'CREDITCARDTYPE',
  102. 'ACCT',
  103. 'EXPDATE',
  104. 'FIRSTNAME',
  105. 'LASTNAME',
  106. 'PROFILESTARTDATE',
  107. 'BILLINGPERIOD',
  108. 'BILLINGFREQUENCY',
  109. 'AMT'
  110. );
  111. $function_settings = array(
  112. 'DESC' => $this->ci->config->item('paypal_api_service_description'),
  113. 'METHOD' => 'UpdateRecurringPaymentsProfile'
  114. );
  115. return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($billing_keys, $billing_data), $this->endpoint);
  116. }
  117. /**
  118. * Cancel a recurring subscription
  119. *
  120. * @param array
  121. * @return object
  122. */
  123. public function cancel_subscription($profile_id)
  124. {
  125. $request_params = array(
  126. 'PROFILEID',
  127. 'ACTION'
  128. );
  129. $request_values = array(
  130. $profile_id,
  131. 'Cancel'
  132. );
  133. $function_settings = array(
  134. 'METHOD' => 'ManageRecurringPaymentsProfileStatus'
  135. );
  136. return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($request_params, $request_values), $this->endpoint);
  137. }
  138. /**
  139. * Suspend a subscription
  140. *
  141. * @param string
  142. * @return object
  143. */
  144. public function suspend_subscription($profile_id)
  145. {
  146. $request_params = array(
  147. 'PROFILEID',
  148. 'ACTION'
  149. );
  150. $request_values = array(
  151. $profile_id,
  152. 'Suspend'
  153. );
  154. $function_settings = array(
  155. 'METHOD' => 'ManageRecurringPaymentsProfileStatus'
  156. );
  157. return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($request_params, $request_values), $this->endpoint);
  158. }
  159. /**
  160. * Activate a subscription
  161. *
  162. * @param int
  163. * @return object
  164. */
  165. public function activate_subscription($profile_id)
  166. {
  167. $request_params = array(
  168. 'PROFILEID',
  169. 'ACTION'
  170. );
  171. $request_values = array(
  172. $profile_id,
  173. 'Reactivate'
  174. );
  175. $function_settings = array(
  176. 'METHOD' => 'ManageRecurringPaymentsProfileStatus'
  177. );
  178. return $this->handle_query(array_merge($this->settings, $function_settings), array_combine($request_params, $request_values), $this->endpoint);
  179. }
  180. /**
  181. * Build the query for the response and call the request function
  182. *
  183. * @param array
  184. * @param array
  185. * @param string
  186. * @return array
  187. */
  188. private function handle_query($settings, $data, $endpoint)
  189. {
  190. $request = http_build_query(array_merge($settings, $data));
  191. return $this->parse_response($this->make_request($endpoint.$request));
  192. }
  193. /**
  194. * Make a new request to PayPal API
  195. *
  196. * @param array
  197. * @return array
  198. */
  199. private function make_request($request)
  200. {
  201. // create a new cURL resource
  202. $curl = curl_init();
  203. // set URL
  204. curl_setopt($curl, CURLOPT_URL, $request);
  205. // set to return the data as a string
  206. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  207. curl_setopt($curl, CURLOPT_HEADER, 0);
  208. // Run the query and get a response
  209. $response = curl_exec($curl);
  210. // close cURL resource, and free up system resources
  211. curl_close($curl);
  212. // Return the response
  213. return $response;
  214. }
  215. /**
  216. * Parse the response from the server
  217. *
  218. * @param array
  219. * @return object
  220. */
  221. private function parse_response($response)
  222. {
  223. $results = explode('&',urldecode($response));
  224. foreach($results as $result)
  225. {
  226. list($key, $value) = explode('=', $result);
  227. $response_array[$key]=$value;
  228. }
  229. $return_object = array();
  230. //Set the response status
  231. ($response_array['ACK'] == 'Success' ? $success = TRUE : $failure = TRUE );
  232. if(isset($failure)){
  233. $return_object[] = array('status'=>'failure') + array('response'=>$response_array['L_LONGMESSAGE0']);
  234. }
  235. if(isset($success)){
  236. $return_object[] = array('status'=>'success') + array('response'=>$response_array);
  237. }
  238. return (object) $return_object[0];
  239. }
  240. }