Ticket #502: Paypalpro.2.php

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

Tidied Paypalpro driver with the bug in the process method fixed

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2
3/**
4 * Paypay (website payments pro) Payment Driver
5 *
6 *
7 * @package    Payment
8 * @author     Kohana Team
9 * @copyright  (c) 2007-2008 Kohana Team
10 * @license    http://kohanaphp.com/license.html
11 */
12class Payment_Paypalpro_Driver implements Payment_Driver
13{
14    // Fields required to do a transaction
15
16    /**
17    * these are the required fields for the API method DoDirectPayment
18    * other API methods and the associated fields required are listed here: http://tinyurl.com/2cffgr
19    *
20    * *note - paypalpro API field names are not case sensitive
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, // default is USD - only required if other currency needed
37        'AMT'            => FALSE, // payment amount
38
39        'IPADDRESS'      => FALSE,
40
41        'FIRSTNAME'      => FALSE,
42        'LASTNAME'       => FALSE,
43
44        'CREDITCARDTYPE' => FALSE,
45        'ACCT'           => FALSE, // card number
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        /* some possible values for METHOD :
62           'DoDirectPayment',
63           'RefundTransaction',
64           'DoAuthorization',
65           'DoReauthorization',
66           'DoCapture',
67           'DoVoid'
68        */
69
70        'PAYMENTACTION'  => '',
71
72        'CURRENCYCODE'   => '',
73        'AMT'            => 0// payment amount
74
75        'IPADDRESS'      => '',
76
77        'FIRSTNAME'      => '',
78        'LASTNAME'       => '',
79
80        'CREDITCARDTYPE' => '',
81        'ACCT'           => '', // card number
82        'EXPDATE'        => '', // Format: MMYYYY
83        'CVV2'           => '', // security code
84
85        // -- OPTIONAL FIELDS --
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'         => '' // 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    private $nvp_response_array = array();
116
117    /**
118     * Sets the config for the class.
119     *
120     * @param : array  - config passed from the payment library constructor
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    *@desc set fields for nvp string
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    *@desc process PayPal Website Payments Pro transaction
177    */
178
179    public function process()
180    {
181        // Check for required fields
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        // Instantiate curl and pass the API post url
196        $ch = curl_init($this->fields['ENDPOINT']);
197
198        foreach($this->fields as $key => $val)
199        {
200            // don't include unset optional fields in the name-value pair request string
201            if($val==='' OR $key=='ENDPOINT') unset($this->fields[$key]);
202        }
203
204
205        $nvp_qstr = http_build_query($this->fields);
206
207        // Set custom curl options
208        curl_setopt_array($ch, $this->curl_config);
209
210        // Set the curl POST fields
211        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp_qstr);
212
213        // Execute post and get results
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} // End Payment_Paypalpro_Driver Class