|
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
|
|
|
'payment_url' => false, |
|
17
|
|
|
'script' => false, |
|
18
|
|
|
'currency' => false, |
|
19
|
|
|
'pay_to_email' => false, |
|
20
|
|
|
'test_pay_to_email' => false, |
|
21
|
|
|
'password' => false, |
|
22
|
|
|
'test_password' => false, |
|
23
|
|
|
'postdata' => false, |
|
24
|
|
|
'action' => false |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
private $fields = array( |
|
28
|
|
|
'payment_url' => '', |
|
29
|
|
|
'script' => '', |
|
30
|
|
|
'currency' => '', |
|
31
|
|
|
'pay_to_email' => '', |
|
32
|
|
|
'test_pay_to_email' => '', |
|
33
|
|
|
'password' => '', |
|
34
|
|
|
'test_password' => '', |
|
35
|
|
|
'postdata' => '', |
|
36
|
|
|
'action' => '', |
|
37
|
|
|
'trn_id' => '', |
|
38
|
|
|
'mb_trn_id' => '', |
|
39
|
|
|
'rec_payment_id' => '', |
|
40
|
|
|
'sid' => '' |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
private $test_mode = true; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the config for the class. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array config passed from the library |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($config) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->test_mode = $config['test_mode']; |
|
53
|
|
|
|
|
54
|
|
|
$this->fields['payment_url'] = $config['payment_url']; |
|
55
|
|
|
$this->fields['currency'] = $config['currency']; |
|
56
|
|
|
|
|
57
|
|
|
if ($this->test_mode) { |
|
58
|
|
|
$this->fields['test_pay_to_email'] = $config['test_pay_to_email']; |
|
59
|
|
|
$this->fields['test_password'] = $config['test_password']; |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->fields['pay_to_email'] = $config['pay_to_email']; |
|
62
|
|
|
$this->fields['password'] = $config['password']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->curl_config = $config['curl_config']; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
Kohana::Log('debug', 'MoneyBookers Payment Driver Initialized'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
public function set_fields($fields) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
foreach ((array) $fields as $key => $value) { |
|
73
|
|
|
$this->fields[$key] = $value; |
|
74
|
|
|
if (array_key_exists($key, $this->required_fields) and !empty($value)) { |
|
75
|
|
|
$this->required_fields[$key] = true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function process() |
|
81
|
|
|
{ |
|
82
|
|
|
$post_url = ($this->test_mode) |
|
83
|
|
|
? $this->fields['payment_url'].$this->fields['script'] // Test mode URL |
|
84
|
|
|
: $this->fields['payment_url'].$this->fields['script']; // Live URL |
|
85
|
|
|
|
|
86
|
|
|
$pay_to_email = ($this->test_mode) |
|
87
|
|
|
? $this->fields['test_pay_to_email'] // Test mode email |
|
88
|
|
|
: $this->fields['pay_to_email']; // Live email |
|
89
|
|
|
|
|
90
|
|
|
$password = ($this->test_mode) |
|
91
|
|
|
? $this->fields['test_password'] // Test mode password |
|
92
|
|
|
: $this->fields['password']; // Live password |
|
93
|
|
|
|
|
94
|
|
|
$this->fields['postdata']['action'] = $this->fields['action']; |
|
95
|
|
|
|
|
96
|
|
|
$config = Kohana::config('payment.MoneyBookers'); |
|
97
|
|
|
|
|
98
|
|
|
if ($this->fields['script'] == $config['refund_script'] && $this->fields['action'] == "prepare") { |
|
99
|
|
|
$this->fields['postdata']['email'] = $pay_to_email; |
|
100
|
|
|
$this->fields['postdata']['password'] = md5($password); |
|
101
|
|
|
$this->fields['postdata']['trn_id'] = $this->fields['trn_id']; |
|
102
|
|
|
} elseif ($this->fields['action'] == "prepare") { |
|
103
|
|
|
$this->fields['postdata']['email'] = $pay_to_email; |
|
104
|
|
|
$this->fields['postdata']['password'] = md5($password); |
|
105
|
|
|
$this->fields['postdata']['amount'] = $this->fields['amount']; |
|
106
|
|
|
$this->fields['postdata']['currency'] = $this->fields['currency']; |
|
107
|
|
|
$this->fields['postdata']['frn_trn_id'] = $this->fields['trn_id']; |
|
108
|
|
|
$this->fields['postdata']['rec_payment_id'] = $this->fields['rec_payment_id']; |
|
109
|
|
|
} elseif ($this->fields['action'] == "refund") { |
|
110
|
|
|
$this->fields['postdata']['sid'] = $this->fields['sid']; |
|
111
|
|
|
} elseif ($this->fields['action'] == "request") { |
|
112
|
|
|
$this->fields['postdata']['sid'] = $this->fields['sid']; |
|
113
|
|
|
} elseif ($this->fields['action'] == "status_od") { |
|
114
|
|
|
$this->fields['postdata']['email'] = $pay_to_email; |
|
115
|
|
|
$this->fields['postdata']['password'] = md5($password); |
|
116
|
|
|
$this->fields['postdata']['trn_id'] = $this->fields['trn_id']; |
|
117
|
|
|
} elseif ($this->fields['action'] == "status_trn") { |
|
118
|
|
|
$this->fields['postdata']['email'] = $pay_to_email; |
|
119
|
|
|
$this->fields['postdata']['password'] = md5($password); |
|
120
|
|
|
$this->fields['postdata']['mb_trn_id'] = $this->fields['trn_id']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$ch = curl_init($post_url); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, true); |
|
126
|
|
|
// the following disallows redirects |
|
127
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
|
128
|
|
|
// next we return into a variable |
|
129
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
130
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
|
131
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
132
|
|
|
|
|
133
|
|
|
// Set the curl POST fields |
|
134
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields['postdata']); |
|
135
|
|
|
|
|
136
|
|
|
// Execute post and get results |
|
137
|
|
|
$response = curl_exec($ch); |
|
138
|
|
|
// Get error messages. |
|
139
|
|
|
$info = curl_getinfo($ch); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
curl_close($ch); |
|
142
|
|
|
|
|
143
|
|
|
if (!$response) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($this->fields['action'] != "status_od" && $this->fields['action'] != "status_trn") { |
|
148
|
|
|
$xml = simplexml_load_string($response); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($this->fields['action'] == "prepare") { |
|
152
|
|
|
return ($xml->{'sid'} != "") ? $xml : false; |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($this->fields['action'] == "request") { |
|
156
|
|
|
return ($xml->{'transaction'}->{'status_msg'} == "processed") ? $xml : false; |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ($this->fields['action'] == "status_od") { |
|
160
|
|
|
return (strstr($response, "Status: 0")) ? true : false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ($this->fields['action'] == "status_trn") { |
|
164
|
|
|
return (strstr($response, "OK")) ? $response : false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($this->fields['action'] == "refund") { |
|
168
|
|
|
return ($xml->{'status'} == 2) ? $xml : false; |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} // End Payment_MoneyBookers_Driver Class |
|
172
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: