Ticket #520: Paypalpro.php

File Paypalpro.php, 5.7 kB (added by atomless, 5 months ago)

Payment driver for paypal website payments pro

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        'STREET'         => '',
88        'STREET2'        => '',
89        'CITY'           => '',
90        'STATE'          => '',
91        'ZIP'            => '',
92        'COUNTRYCODE'    => '',
93
94        'SHIPTONAME'        => '',
95        'SHIPTOSTREET'      => '',
96        'SHIPTOSTREET2'     => '',
97        'SHIPTOCITY'        => '',
98        'SHIPTOSTATE'       => '',
99        'SHIPTOZIP'         => '',
100        'SHIPTOCOUNTRYCODE' => '',
101
102        'INVNUM'         => '' // your internal order id / transaction id
103
104        // other optional fields listed here:
105        // https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2145100
106    );
107
108    private $test_mode = TRUE;
109
110    /**
111     * Sets the config for the class.
112     *
113     * @param : array  - config passed from the payment library constructor
114     */
115
116    public function __construct($config)
117    {
118        $this->test_mode = $config['test_mode'];
119
120        if($this->test_mode)
121        {
122            $this->fields['USER']         = $config['SANDBOX_USER'];
123            $this->fields['PWD']          = $config['SANDBOX_PWD'];
124            $this->fields['SIGNATURE']    = $config['SANDBOX_SIGNATURE'];
125            $this->fields['ENDPOINT']     = $config['SANDBOX_ENDPOINT'];
126        }
127        else
128        {
129            $this->fields['USER']         = $config['USER'];
130            $this->fields['PWD']          = $config['PWD'];
131            $this->fields['SIGNATURE']    = $config['SIGNATURE'];
132            $this->fields['ENDPOINT']     = $config['ENDPOINT'];
133        }
134
135        $this->fields['VERSION']      = $config['VERSION'];
136        $this->fields['CURRENCYCODE'] = $config['CURRENCYCODE'];
137
138        $this->required_fields['USER']         = !empty($config['USER']);
139        $this->required_fields['PWD']          = !empty($config['PWD']);
140        $this->required_fields['SIGNATURE']    = !empty($config['SIGNATURE']);
141        $this->required_fields['ENDPOINT']     = !empty($config['ENDPOINT']);
142        $this->required_fields['VERSION']      = !empty($config['VERSION']);
143        $this->required_fields['CURRENCYCODE'] = !empty($config['CURRENCYCODE']);
144
145        $this->curl_config = $config['curl_config'];
146
147        Log::add('debug', 'Paypalpro Payment Driver Initialized');
148    }
149
150    /**
151    *@desc set fields for nvp string
152    */
153
154    public function set_fields($fields)
155    {
156        foreach ((array) $fields as $key => $value)
157        {
158            $this->fields[$key] = $value;
159
160            if (array_key_exists($key, $this->required_fields) and !empty($value))
161            {
162                $this->required_fields[$key] = TRUE;
163            }
164
165        }
166    }
167
168    /**
169    *@desc process PayPal Website Payments Pro transaction
170    */
171
172    public function process()
173    {
174        // Check for required fields
175        if (in_array(FALSE, $this->required_fields))
176        {
177            $fields = array();
178
179            foreach ($this->required_fields as $key => $field)
180            {
181                if (!$field) $fields[] = $key;
182            }
183
184            throw new Kohana_Exception('payment.required', implode(', ', $fields));
185        }
186
187
188        // Instantiate curl and pass the API post url
189        $ch = curl_init($this->fields['ENDPOINT']);
190
191        foreach($this->fields as $key => $val)
192        {
193            // don't include unset optional fields in the name-value pair request string
194            if($val==='' OR $key=='ENDPOINT') unset($this->fields[$key]);
195        }
196
197
198        $nvp_qstr = http_build_query($this->fields);
199
200        // Set custom curl options
201        curl_setopt_array($ch, $this->curl_config);
202
203        // Set the curl POST fields
204        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp_qstr);
205
206        // Execute post and get results
207        $response = curl_exec($ch);
208
209        if (curl_errno($ch))
210        {
211            $curl_error_no  = curl_errno($ch);
212            $curl_error_msg = curl_error($ch);
213            throw new Kohana_Exception('curl.error:'.$curl_error_no.' - '.$curl_error_msg);
214        }
215
216        curl_close ($ch);
217
218        if (!$response)
219            throw new Kohana_Exception('payment.gateway_connection_error');
220
221
222        $nvp_res_array = array();
223
224        parse_str(urldecode($response),$nvp_res_array);
225
226        return ($nvp_res_array['ACK'] == TRUE);
227
228    }
229} // End Payment_Paypalpro_Driver Class