Ticket #502: Paypalpro.php

File Paypalpro.php, 6.0 kB (added by atomless, 9 months ago)

Kohana Payment driver for Paypal Website Payments Pro

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2
3// -- save this in your 'application/libraries/drivers/Payment' directory
4
5/**
6 * Paypay (website payments pro) Payment Driver
7 *
8 *
9 * @package    Payment
10 * @author     James Tindall
11 * @copyright  (c) 2007-2008 Kohana Team
12 * @license    http://kohanaphp.com/license.html
13 */
14class Payment_Paypalpro_Driver implements Payment_Driver
15{
16    // Fields required to do a transaction
17
18    /**
19    * these are the required fields for the API method DoDirectPayment
20    * other API methods and the associated fields required are listed here: http://tinyurl.com/2cffgr
21    *
22    * *note - paypalpro API field names are not case sensitive
23    */
24
25    private $required_fields = array
26    (
27
28        'ENDPOINT'       => FALSE,
29        'USER'           => FALSE,
30        'PWD'            => FALSE,
31        'SIGNATURE'      => FALSE,
32        'VERSION'        => FALSE,
33
34        'METHOD'         => FALSE,
35
36        'PAYMENTACTION'  => FALSE,
37
38        'CURRENCYCODE'   => FALSE, // default is USD - only required if other currency needed
39        'AMT'            => FALSE, // payment amount
40
41        'IPADDRESS'      => FALSE,
42
43        'FIRSTNAME'      => FALSE,
44        'LASTNAME'       => FALSE,
45
46        'CREDITCARDTYPE' => FALSE,
47        'ACCT'           => FALSE, // card number
48        'EXPDATE'        => FALSE,
49        'CVV2'           => FALSE
50
51    );
52
53
54    private $fields = array
55    (
56
57        'ENDPOINT'       => '',
58        'USER'           => '',
59        'PWD'            => '',
60        'SIGNATURE'      => '',
61        'VERSION'        => '',
62
63        'METHOD'         => '',
64        /* some possible values for METHOD :
65           'DoDirectPayment',
66           'RefundTransaction',
67           'DoAuthorization',
68           'DoReauthorization',
69           'DoCapture',
70           'DoVoid'
71        */
72
73        'PAYMENTACTION'  => '',
74
75        'CURRENCYCODE'   => '',
76        'AMT'            => 0// payment amount
77
78        'IPADDRESS'      => '',
79
80        'FIRSTNAME'      => '',
81        'LASTNAME'       => '',
82
83        'CREDITCARDTYPE' => '',
84        'ACCT'           => '', // card number
85        'EXPDATE'        => '', // Format: MMYYYY
86        'CVV2'           => '', // security code
87
88        // -- OPTIONAL FIELDS --
89
90        'STREET'         => '',
91        'STREET2'        => '',
92        'CITY'           => '',
93        'STATE'          => '',
94        'ZIP'            => '',
95        'COUNTRYCODE'    => '',
96
97
98        'SHIPTONAME'        => '',
99        'SHIPTOSTREET'      => '',
100        'SHIPTOSTREET2'     => '',
101        'SHIPTOCITY'        => '',
102        'SHIPTOSTATE'       => '',
103        'SHIPTOZIP'         => '',
104        'SHIPTOCOUNTRYCODE' => '',
105
106
107        'INVNUM'         => '' // your internal order id / transaction id
108
109        // other optional fields listed here:
110        // https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2145100
111    );
112
113    private $test_mode = TRUE;
114   
115
116    /**
117     * Sets the config for the class.
118     *
119     * @param : array  - config passed from the payment library constructor
120     */
121     
122    public function __construct($config)
123    {
124   
125        $this->test_mode = $config['test_mode'];
126
127        if($this->test_mode){
128       
129            $this->fields['USER']         = $config['SANDBOX_USER'];
130           
131            $this->fields['PWD']          = $config['SANDBOX_PWD'];
132           
133            $this->fields['SIGNATURE']    = $config['SANDBOX_SIGNATURE'];
134           
135            $this->fields['ENDPOINT']     = $config['SANDBOX_ENDPOINT'];
136           
137        }else{
138       
139            $this->fields['USER']         = $config['USER'];
140           
141            $this->fields['PWD']          = $config['PWD'];
142           
143            $this->fields['SIGNATURE']    = $config['SIGNATURE'];
144           
145            $this->fields['ENDPOINT']     = $config['ENDPOINT'];
146        }
147       
148
149        $this->fields['VERSION']      = $config['VERSION'];
150       
151        $this->fields['CURRENCYCODE'] = $config['CURRENCYCODE'];
152
153        $this->required_fields['USER']         = !empty($config['USER']);
154       
155        $this->required_fields['PWD']          = !empty($config['PWD']);
156       
157        $this->required_fields['SIGNATURE']    = !empty($config['SIGNATURE']);
158       
159        $this->required_fields['ENDPOINT']     = !empty($config['ENDPOINT']);
160       
161        $this->required_fields['VERSION']      = !empty($config['VERSION']);
162       
163        $this->required_fields['CURRENCYCODE'] = !empty($config['CURRENCYCODE']);
164
165        $this->curl_config = $config['curl_config'];
166       
167
168        Log::add('debug', 'Paypalpro Payment Driver Initialized');
169       
170    }
171   
172    /**
173    *@desc set fields for nvp string
174    */
175
176    public function set_fields($fields)
177    {
178        foreach ((array) $fields as $key => $value)
179        {
180            $this->fields[$key] = $value;
181           
182            if (array_key_exists($key, $this->required_fields) and !empty($value)) {
183           
184                $this->required_fields[$key] = TRUE;
185               
186            }
187       
188        }
189    }
190   
191   
192    /**
193    *@desc process PayPal Website Payments Pro transaction
194    */
195
196    public function process()
197    {
198
199        // Check for required fields
200        if (in_array(FALSE, $this->required_fields))
201        {
202            $fields = array();
203           
204            foreach ($this->required_fields as $key => $field)
205            {
206                if (!$field) $fields[] = $key;
207            }
208           
209            throw new Kohana_Exception('payment.required', implode(', ', $fields));
210        }
211
212
213        // Instantiate curl and pass the API post url
214        $ch = curl_init($this->fields['ENDPOINT']);
215
216        foreach($this->fields as $key => $val){
217       
218            // don't include unset optional fields in the name-value pair request string
219            if($val==='' OR $key=='ENDPOINT') unset($this->fields[$key]);
220           
221        }
222
223
224        $nvp_qstr = http_build_query($this->fields);
225        //echo kOHANA::debug(explode('&',$nvp_qstr));exit;
226
227        // Set custom curl options
228        curl_setopt_array($ch, $this->curl_config);
229
230        // Set the curl POST fields
231        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp_qstr);
232
233        // Execute post and get results
234        $response = curl_exec($ch);
235
236        if (curl_errno($ch)) {
237
238            $curl_error_no  = curl_errno($ch);
239           
240            $curl_error_msg = curl_error($ch);
241           
242            throw new Kohana_Exception('curl.error:'.$curl_error_no.' - '.$curl_error_msg);
243
244        }
245
246        curl_close ($ch);
247
248        if (!$response)
249            throw new Kohana_Exception('payment.gateway_connection_error');
250
251
252        $nvp_res_array = array();
253
254        parse_str(urldecode($response),$nvp_res_array);
255
256        return $nvp_res_array;
257
258    }
259
260
261} // End Payment_Paypalpro_Driver Class