/mis/modules/payment/libraries/drivers/Payment/Moneybookers.php

https://github.com/craig1709/mis · PHP · 196 lines · 149 code · 26 blank · 21 comment · 29 complexity · 5ebb2b0f31ba681e5ad94e29f3563ba5 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * MoneyBookers Payment Driver
  4. *
  5. * $Id: MoneyBookers.php
  6. *
  7. * @package Payment
  8. * @author Josh Domagala
  9. * @copyright (c) 2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Payment_Moneybookers_Driver implements Payment_Driver
  13. {
  14. // Fields required to do a transaction
  15. private $required_fields = array
  16. (
  17. 'payment_url' => FALSE,
  18. 'script' => FALSE,
  19. 'currency' => FALSE,
  20. 'pay_to_email' => FALSE,
  21. 'test_pay_to_email' => FALSE,
  22. 'password' => FALSE,
  23. 'test_password' => FALSE,
  24. 'postdata' => FALSE,
  25. 'action' => FALSE
  26. );
  27. private $fields = array
  28. (
  29. 'payment_url' => '',
  30. 'script' => '',
  31. 'currency' => '',
  32. 'pay_to_email' => '',
  33. 'test_pay_to_email' => '',
  34. 'password' => '',
  35. 'test_password' => '',
  36. 'postdata' => '',
  37. 'action' => '',
  38. 'trn_id' => '',
  39. 'mb_trn_id' => '',
  40. 'rec_payment_id' => '',
  41. 'sid' => ''
  42. );
  43. private $test_mode = TRUE;
  44. /**
  45. * Sets the config for the class.
  46. *
  47. * @param array config passed from the library
  48. */
  49. public function __construct($config)
  50. {
  51. $this->test_mode = $config['test_mode'];
  52. $this->fields['payment_url'] = $config['payment_url'];
  53. $this->fields['currency'] = $config['currency'];
  54. if($this->test_mode)
  55. {
  56. $this->fields['test_pay_to_email'] = $config['test_pay_to_email'];
  57. $this->fields['test_password'] = $config['test_password'];
  58. }
  59. else
  60. {
  61. $this->fields['pay_to_email'] = $config['pay_to_email'];
  62. $this->fields['password'] = $config['password'];
  63. }
  64. $this->curl_config = $config['curl_config'];
  65. Kohana::Log('debug', 'MoneyBookers Payment Driver Initialized');
  66. }
  67. public function set_fields($fields)
  68. {
  69. foreach ((array) $fields as $key => $value)
  70. {
  71. $this->fields[$key] = $value;
  72. if ( array_key_exists( $key, $this->required_fields ) and !empty( $value ) )
  73. {
  74. $this->required_fields[$key] = TRUE;
  75. }
  76. }
  77. }
  78. public function process()
  79. {
  80. $post_url = ($this->test_mode)
  81. ? $this->fields['payment_url'].$this->fields['script'] // Test mode URL
  82. : $this->fields['payment_url'].$this->fields['script']; // Live URL
  83. $pay_to_email = ($this->test_mode)
  84. ? $this->fields['test_pay_to_email'] // Test mode email
  85. : $this->fields['pay_to_email']; // Live email
  86. $password = ($this->test_mode)
  87. ? $this->fields['test_password'] // Test mode password
  88. : $this->fields['password']; // Live password
  89. $this->fields['postdata']['action'] = $this->fields['action'];
  90. $config = Kohana::config('payment.MoneyBookers');
  91. if($this->fields['script'] == $config['refund_script'] && $this->fields['action'] == "prepare")
  92. {
  93. $this->fields['postdata']['email'] = $pay_to_email;
  94. $this->fields['postdata']['password'] = md5($password);
  95. $this->fields['postdata']['trn_id'] = $this->fields['trn_id'];
  96. }
  97. elseif($this->fields['action'] == "prepare")
  98. {
  99. $this->fields['postdata']['email'] = $pay_to_email;
  100. $this->fields['postdata']['password'] = md5($password);
  101. $this->fields['postdata']['amount'] = $this->fields['amount'];
  102. $this->fields['postdata']['currency'] = $this->fields['currency'];
  103. $this->fields['postdata']['frn_trn_id'] = $this->fields['trn_id'];
  104. $this->fields['postdata']['rec_payment_id'] = $this->fields['rec_payment_id'];
  105. }
  106. elseif($this->fields['action'] == "refund")
  107. {
  108. $this->fields['postdata']['sid'] = $this->fields['sid'];
  109. }
  110. elseif($this->fields['action'] == "request")
  111. {
  112. $this->fields['postdata']['sid'] = $this->fields['sid'];
  113. }
  114. elseif($this->fields['action'] == "status_od")
  115. {
  116. $this->fields['postdata']['email'] = $pay_to_email;
  117. $this->fields['postdata']['password'] = md5($password);
  118. $this->fields['postdata']['trn_id'] = $this->fields['trn_id'];
  119. }
  120. elseif($this->fields['action'] == "status_trn")
  121. {
  122. $this->fields['postdata']['email'] = $pay_to_email;
  123. $this->fields['postdata']['password'] = md5($password);
  124. $this->fields['postdata']['mb_trn_id'] = $this->fields['trn_id'];
  125. }
  126. $ch = curl_init($post_url);
  127. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  128. // the following disallows redirects
  129. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  130. // next we return into a variable
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  132. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  133. curl_setopt($ch, CURLOPT_POST, 1);
  134. // Set the curl POST fields
  135. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields['postdata']);
  136. // Execute post and get results
  137. $response = curl_exec($ch);
  138. // Get error messages.
  139. $info = curl_getinfo($ch);
  140. curl_close ($ch);
  141. if(!$response)
  142. {
  143. return false;
  144. }
  145. if($this->fields['action'] != "status_od" && $this->fields['action'] != "status_trn")
  146. {
  147. $xml = simplexml_load_string($response);
  148. }
  149. if($this->fields['action'] == "prepare")
  150. {
  151. return ($xml->{'sid'} != "") ? $xml : false;
  152. }
  153. if($this->fields['action'] == "request")
  154. {
  155. return ($xml->{'transaction'}->{'status_msg'} == "processed") ? $xml : false;
  156. }
  157. if($this->fields['action'] == "status_od")
  158. {
  159. return (strstr($response, "Status: 0")) ? true : false;
  160. }
  161. if($this->fields['action'] == "status_trn")
  162. {
  163. return (strstr($response, "OK")) ? $response : false;
  164. }
  165. if($this->fields['action'] == "refund")
  166. {
  167. return ($xml->{'status'} == 2) ? $xml : false;
  168. }
  169. }
  170. } // End Payment_MoneyBookers_Driver Class