| 1 | <?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class Payment_Paypalpro_Driver implements Payment_Driver |
|---|
| 13 | { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | private $required_fields = array |
|---|
| 24 | ( |
|---|
| 25 | |
|---|
| 26 | 'ENDPOINT' => FALSE, |
|---|
| 27 | 'USER' => FALSE, |
|---|
| 28 | 'PWD' => FALSE, |
|---|
| 29 | 'SIGNATURE' => FALSE, |
|---|
| 30 | 'VERSION' => FALSE, |
|---|
| 31 | |
|---|
| 32 | 'METHOD' => FALSE, |
|---|
| 33 | |
|---|
| 34 | 'PAYMENTACTION' => FALSE, |
|---|
| 35 | |
|---|
| 36 | 'CURRENCYCODE' => FALSE, |
|---|
| 37 | 'AMT' => FALSE, |
|---|
| 38 | |
|---|
| 39 | 'IPADDRESS' => FALSE, |
|---|
| 40 | |
|---|
| 41 | 'FIRSTNAME' => FALSE, |
|---|
| 42 | 'LASTNAME' => FALSE, |
|---|
| 43 | |
|---|
| 44 | 'CREDITCARDTYPE' => FALSE, |
|---|
| 45 | 'ACCT' => FALSE, |
|---|
| 46 | 'EXPDATE' => FALSE, |
|---|
| 47 | 'CVV2' => FALSE |
|---|
| 48 | |
|---|
| 49 | ); |
|---|
| 50 | |
|---|
| 51 | private $fields = array |
|---|
| 52 | ( |
|---|
| 53 | |
|---|
| 54 | 'ENDPOINT' => '', |
|---|
| 55 | 'USER' => '', |
|---|
| 56 | 'PWD' => '', |
|---|
| 57 | 'SIGNATURE' => '', |
|---|
| 58 | 'VERSION' => '', |
|---|
| 59 | |
|---|
| 60 | 'METHOD' => '', |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | 'PAYMENTACTION' => '', |
|---|
| 71 | |
|---|
| 72 | 'CURRENCYCODE' => '', |
|---|
| 73 | 'AMT' => 0, |
|---|
| 74 | |
|---|
| 75 | 'IPADDRESS' => '', |
|---|
| 76 | |
|---|
| 77 | 'FIRSTNAME' => '', |
|---|
| 78 | 'LASTNAME' => '', |
|---|
| 79 | |
|---|
| 80 | 'CREDITCARDTYPE' => '', |
|---|
| 81 | 'ACCT' => '', |
|---|
| 82 | 'EXPDATE' => '', |
|---|
| 83 | 'CVV2' => '', |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | 'ITEMAMT' => '', |
|---|
| 88 | 'SHIPPINGAMT' => '', |
|---|
| 89 | 'HANDLINGAMT' => '', |
|---|
| 90 | 'TAXAMT' => '', |
|---|
| 91 | |
|---|
| 92 | 'STREET' => '', |
|---|
| 93 | 'STREET2' => '', |
|---|
| 94 | 'CITY' => '', |
|---|
| 95 | 'STATE' => '', |
|---|
| 96 | 'ZIP' => '', |
|---|
| 97 | 'COUNTRYCODE' => '', |
|---|
| 98 | |
|---|
| 99 | 'SHIPTONAME' => '', |
|---|
| 100 | 'SHIPTOSTREET' => '', |
|---|
| 101 | 'SHIPTOSTREET2' => '', |
|---|
| 102 | 'SHIPTOCITY' => '', |
|---|
| 103 | 'SHIPTOSTATE' => '', |
|---|
| 104 | 'SHIPTOZIP' => '', |
|---|
| 105 | 'SHIPTOCOUNTRYCODE' => '', |
|---|
| 106 | |
|---|
| 107 | 'INVNUM' => '' |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | ); |
|---|
| 112 | |
|---|
| 113 | private $test_mode = TRUE; |
|---|
| 114 | |
|---|
| 115 | private $nvp_response_array = array(); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | public function __construct($config) |
|---|
| 124 | { |
|---|
| 125 | $this->test_mode = $config['test_mode']; |
|---|
| 126 | |
|---|
| 127 | if($this->test_mode) |
|---|
| 128 | { |
|---|
| 129 | $this->fields['USER'] = $config['SANDBOX_USER']; |
|---|
| 130 | $this->fields['PWD'] = $config['SANDBOX_PWD']; |
|---|
| 131 | $this->fields['SIGNATURE'] = $config['SANDBOX_SIGNATURE']; |
|---|
| 132 | $this->fields['ENDPOINT'] = $config['SANDBOX_ENDPOINT']; |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | $this->fields['USER'] = $config['USER']; |
|---|
| 137 | $this->fields['PWD'] = $config['PWD']; |
|---|
| 138 | $this->fields['SIGNATURE'] = $config['SIGNATURE']; |
|---|
| 139 | $this->fields['ENDPOINT'] = $config['ENDPOINT']; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | $this->fields['VERSION'] = $config['VERSION']; |
|---|
| 143 | $this->fields['CURRENCYCODE'] = $config['CURRENCYCODE']; |
|---|
| 144 | |
|---|
| 145 | $this->required_fields['USER'] = !empty($config['USER']); |
|---|
| 146 | $this->required_fields['PWD'] = !empty($config['PWD']); |
|---|
| 147 | $this->required_fields['SIGNATURE'] = !empty($config['SIGNATURE']); |
|---|
| 148 | $this->required_fields['ENDPOINT'] = !empty($config['ENDPOINT']); |
|---|
| 149 | $this->required_fields['VERSION'] = !empty($config['VERSION']); |
|---|
| 150 | $this->required_fields['CURRENCYCODE'] = !empty($config['CURRENCYCODE']); |
|---|
| 151 | |
|---|
| 152 | $this->curl_config = $config['curl_config']; |
|---|
| 153 | |
|---|
| 154 | Log::add('debug', 'Paypalpro Payment Driver Initialized'); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | public function set_fields($fields) |
|---|
| 162 | { |
|---|
| 163 | foreach ((array) $fields as $key => $value) |
|---|
| 164 | { |
|---|
| 165 | $this->fields[$key] = $value; |
|---|
| 166 | |
|---|
| 167 | if (array_key_exists($key, $this->required_fields) and !empty($value)) |
|---|
| 168 | { |
|---|
| 169 | $this->required_fields[$key] = TRUE; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | public function process() |
|---|
| 180 | { |
|---|
| 181 | |
|---|
| 182 | if (in_array(FALSE, $this->required_fields)) |
|---|
| 183 | { |
|---|
| 184 | $fields = array(); |
|---|
| 185 | |
|---|
| 186 | foreach ($this->required_fields as $key => $field) |
|---|
| 187 | { |
|---|
| 188 | if (!$field) $fields[] = $key; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | throw new Kohana_Exception('payment.required', implode(', ', $fields)); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | $ch = curl_init($this->fields['ENDPOINT']); |
|---|
| 197 | |
|---|
| 198 | foreach($this->fields as $key => $val) |
|---|
| 199 | { |
|---|
| 200 | |
|---|
| 201 | if($val==='' OR $key=='ENDPOINT') unset($this->fields[$key]); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | $nvp_qstr = http_build_query($this->fields); |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | curl_setopt_array($ch, $this->curl_config); |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp_qstr); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | $response = curl_exec($ch); |
|---|
| 215 | |
|---|
| 216 | if (curl_errno($ch)) |
|---|
| 217 | { |
|---|
| 218 | $curl_error_no = curl_errno($ch); |
|---|
| 219 | $curl_error_msg = curl_error($ch); |
|---|
| 220 | throw new Kohana_Exception('curl.error:'.$curl_error_no.' - '.$curl_error_msg); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | curl_close ($ch); |
|---|
| 224 | |
|---|
| 225 | if (!$response) |
|---|
| 226 | throw new Kohana_Exception('payment.gateway_connection_error'); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | $this->nvp_response_array = array(); |
|---|
| 230 | |
|---|
| 231 | parse_str(urldecode($response),$this->nvp_response_array); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | return ($this->nvp_response_array['ACK']=='Success')? True : $response; |
|---|
| 235 | |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | } |
|---|