|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PayPal Express Checkout Payment |
|
5
|
|
|
* @author Jeremy Shipman jeremy [at] burnbright.net |
|
6
|
|
|
* @author Nicolaas [at] sunnysideup.co.nz |
|
7
|
|
|
* |
|
8
|
|
|
* Developer documentation: |
|
9
|
|
|
* Integration guide: https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_ExpressCheckout_IntegrationGuide.pdf |
|
10
|
|
|
* API reference: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_reference |
|
11
|
|
|
* Uses the Name-Value Pair API protocol |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class PayPalExpressCheckoutPayment extends EcommercePayment |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
private static $debug = false; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
private static $db = array( |
|
|
|
|
|
|
22
|
|
|
'Token' => 'Varchar(30)', |
|
23
|
|
|
'PayerID' => 'Varchar(30)', |
|
24
|
|
|
'TransactionID' => 'Varchar(30)', |
|
25
|
|
|
'AuthorisationCode' => 'Text', |
|
26
|
|
|
'Debug' => 'HTMLText' |
|
27
|
|
|
); |
|
28
|
|
|
private static $logo = "ecommerce/images/paymentmethods/paypal.jpg"; |
|
|
|
|
|
|
29
|
|
|
private static $payment_methods = array(); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
//PayPal URLs |
|
32
|
|
|
private static $test_API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp"; |
|
|
|
|
|
|
33
|
|
|
private static $test_PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token="; |
|
|
|
|
|
|
34
|
|
|
private static $API_Endpoint = "https://api-3t.paypal.com/nvp"; |
|
|
|
|
|
|
35
|
|
|
private static $PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="; |
|
|
|
|
|
|
36
|
|
|
private static $privacy_link = "https://www.paypal.com/us/cgi-bin/webscr?cmd=p/gen/ua/policy_privacy-outside"; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
//config |
|
39
|
|
|
private static $test_mode = true; //on by default |
|
|
|
|
|
|
40
|
|
|
private static $API_UserName; |
|
|
|
|
|
|
41
|
|
|
private static $API_Password; |
|
|
|
|
|
|
42
|
|
|
private static $API_Signature; |
|
|
|
|
|
|
43
|
|
|
private static $sBNCode = null; // BN Code is only applicable for partners |
|
|
|
|
|
|
44
|
|
|
private static $version = '64'; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
//set custom settings |
|
47
|
|
|
private static $custom_settings = array( |
|
|
|
|
|
|
48
|
|
|
//design |
|
49
|
|
|
//'HDRIMG' => "http://www.mysite.com/images/logo.jpg", //max size = 750px wide by 90px high, and good to be on secure server |
|
|
|
|
|
|
50
|
|
|
//'HDRBORDERCOLOR' => 'CCCCCC', //header border |
|
|
|
|
|
|
51
|
|
|
//'HDRBACKCOLOR' => '00FFFF', //header background |
|
|
|
|
|
|
52
|
|
|
//'PAYFLOWCOLOR'=> 'AAAAAA' //payflow colour |
|
53
|
|
|
//'PAGESTYLE' => //page style set in merchant account settings |
|
54
|
|
|
'SOLUTIONTYPE' => 'Sole'//require paypal account, or not. Can be or 'Mark' (required) or 'Sole' (not required) |
|
55
|
|
|
//'BRANDNAME' => 'my site name'//override business name in checkout |
|
56
|
|
|
//'CUSTOMERSERVICENUMBER' => '0800 1234 5689'//number to call to resolve payment issues |
|
57
|
|
|
//'NOSHIPPING' => 1 //disable showing shipping details |
|
|
|
|
|
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
public function getCMSFields() |
|
61
|
|
|
{ |
|
62
|
|
|
$fields = parent::getCMSFields(); |
|
63
|
|
|
foreach (array_keys(self::$db) as $field) { |
|
64
|
|
|
$fields->removeFieldFromTab('Root.Main', $field); |
|
65
|
|
|
$fields->addFieldToTab('Root.Advanced', LiteralField::create($field.'_debug', '<h2>'.$field.'</h2><pre>'.$this->$field.'</pre>')); |
|
66
|
|
|
} |
|
67
|
|
|
return $fields; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getPaymentFormFields() |
|
71
|
|
|
{ |
|
72
|
|
|
$logo = '<img src="' . $this->Config()->get("logo") . '" alt="Credit card payments powered by PayPal"/>'; |
|
73
|
|
|
$privacyLink = '<a href="' . $this->Config()->get("privacy_link") . '" target="_blank" title="Read PayPal\'s privacy policy">' . $logo . '</a><br/>'; |
|
74
|
|
|
return new FieldList( |
|
75
|
|
|
new LiteralField('PayPalInfo', $privacyLink), |
|
76
|
|
|
new LiteralField( |
|
77
|
|
|
'PayPalPaymentsList', |
|
78
|
|
|
$this->renderWith("PaymentMethods") |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getPaymentFormRequirements() |
|
84
|
|
|
{ |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
//main processing function |
|
89
|
|
|
public function processPayment($data, $form) |
|
90
|
|
|
{ |
|
91
|
|
|
//sanity checks for credentials |
|
92
|
|
|
if (!$this->Config()->get("API_UserName") || !$this->Config()->get("API_Password") || !$this->Config()->get("API_Signature")) { |
|
93
|
|
|
user_error('You are attempting to make a payment without the necessary credentials set', E_USER_ERROR); |
|
94
|
|
|
} |
|
95
|
|
|
$data = $this->Order()->BillingAddress()->toMap(); |
|
|
|
|
|
|
96
|
|
|
$paymenturl = $this->getTokenURL($this->Amount->Amount, $this->Amount->Currency, $data); |
|
|
|
|
|
|
97
|
|
|
$this->Status = "Incomplete"; |
|
|
|
|
|
|
98
|
|
|
$this->write(); |
|
99
|
|
|
if ($paymenturl) { |
|
|
|
|
|
|
100
|
|
|
Controller::curr()->redirect($paymenturl); //redirect to payment gateway |
|
101
|
|
|
return EcommercePayment_Processing::create(); |
|
102
|
|
|
} |
|
103
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.COULDNOTBECONTACTED', "PayPal could not be contacted"); |
|
|
|
|
|
|
104
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
105
|
|
|
$this->write(); |
|
106
|
|
|
return EcommercePayment_Failure::create($this->Message); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* |
|
111
|
|
|
* depracated |
|
112
|
|
|
*/ |
|
113
|
|
|
public function PayPalForm() |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
user_error("This form is no longer used."); |
|
116
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
|
117
|
|
|
|
|
118
|
|
|
// 1) Main Information |
|
119
|
|
|
$fields = ''; |
|
120
|
|
|
$order = $this->Order(); |
|
|
|
|
|
|
121
|
|
|
$items = $order->Items(); |
|
122
|
|
|
$member = $order->Member(); |
|
123
|
|
|
|
|
124
|
|
|
// 2) Main Settings |
|
125
|
|
|
|
|
126
|
|
|
$url = $this->Config()->get("test_mode") ? $this->Config()->get("test_url") : $this->Config()->get("url"); |
|
127
|
|
|
$inputs['cmd'] = '_cart'; |
|
|
|
|
|
|
128
|
|
|
$inputs['upload'] = '1'; |
|
129
|
|
|
|
|
130
|
|
|
// 3) Items Informations |
|
131
|
|
|
|
|
132
|
|
|
$cpt = 0; |
|
133
|
|
|
foreach ($items as $item) { |
|
134
|
|
|
$inputs['item_name_' . ++$cpt] = $item->TableTitle(); |
|
135
|
|
|
// item_number is unnecessary |
|
136
|
|
|
$inputs['amount_' . $cpt] = $item->UnitPrice(); |
|
137
|
|
|
$inputs['quantity_' . $cpt] = $item->Quantity; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// 4) Payment Informations And Authorisation Code |
|
141
|
|
|
|
|
142
|
|
|
$inputs['business'] = $this->Config()->get("test_mode") ? $this->Config()->get("test_account_email") : $this->Config()->get("account_email"); |
|
143
|
|
|
$inputs['custom'] = $this->ID . '-' . $this->AuthorisationCode; |
|
|
|
|
|
|
144
|
|
|
// Add Here The Shipping And/Or Taxes |
|
145
|
|
|
$inputs['currency_code'] = $this->Currency; |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
// 5) Redirection Informations |
|
148
|
|
|
|
|
149
|
|
|
$inputs['cancel_return'] = Director::absoluteBaseURL() . PayPalExpressCheckoutPayment_Handler::cancel_link($inputs['custom']); |
|
|
|
|
|
|
150
|
|
|
$inputs['return'] = Director::absoluteBaseURL() . PayPalExpressCheckoutPayment_Handler::complete_link(); |
|
|
|
|
|
|
151
|
|
|
$inputs['rm'] = '2'; |
|
152
|
|
|
// Add Here The Notify URL |
|
153
|
|
|
|
|
154
|
|
|
// 6) PayPal Pages Style Optional Informations |
|
155
|
|
|
|
|
156
|
|
|
if (self:: $continue_button_text) { |
|
157
|
|
|
$inputs['cbt'] = $this->Config()->get("continue_button_text"); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($this->Config()->get("header_image_url")) { |
|
161
|
|
|
$inputs['cpp_header_image'] = urlencode($this->Config()->get("header_image_url")); |
|
162
|
|
|
} |
|
163
|
|
|
if ($this->Config()->get("header_back_color")) { |
|
164
|
|
|
$inputs['cpp_headerback_color'] = $this->Config()->get("header_back_color"); |
|
165
|
|
|
} |
|
166
|
|
|
if ($this->Config()->get("header_border_color")) { |
|
167
|
|
|
$inputs['cpp_headerborder_color'] = $this->Config()->get("header_border_color"); |
|
168
|
|
|
} |
|
169
|
|
|
if ($this->Config()->get("payflow_color")) { |
|
170
|
|
|
$inputs['cpp_payflow_color'] = $this->Config()->get("payflow_color"); |
|
171
|
|
|
} |
|
172
|
|
|
if ($this->Config()->get("back_color")) { |
|
173
|
|
|
$inputs['cs'] = $this->Config()->get("back_color"); |
|
174
|
|
|
} |
|
175
|
|
|
if ($this->Config()->get("image_url")) { |
|
176
|
|
|
$inputs['image_url'] = urlencode($this->Config()->get("image_url")); |
|
177
|
|
|
} |
|
178
|
|
|
if ($this->Config()->get("page_style")) { |
|
179
|
|
|
$inputs['page_style'] = $this->Config()->get("page_style"); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// 7) Prepopulating Customer Informations |
|
183
|
|
|
$billingAddress = $order->BillingAddress(); |
|
184
|
|
|
$inputs['first_name'] = $billingAddress->FirstName; |
|
185
|
|
|
$inputs['last_name'] = $billingAddress->Surname; |
|
186
|
|
|
$inputs['address1'] = $billingAddress->Address; |
|
187
|
|
|
$inputs['address2'] = $billingAddress->Address2; |
|
188
|
|
|
$inputs['city'] = $billingAddress->City; |
|
189
|
|
|
$inputs['zip'] = $billingAddress->PostalCode; |
|
190
|
|
|
$inputs['state'] = $billingAddress->Region()->Code; |
|
191
|
|
|
$inputs['country'] = $billingAddress->Country; |
|
192
|
|
|
$inputs['email'] = $member->Email; |
|
193
|
|
|
|
|
194
|
|
|
// 8) Form Creation |
|
195
|
|
|
if (is_array($inputs) && count($inputs)) { |
|
196
|
|
|
foreach ($inputs as $name => $value) { |
|
197
|
|
|
$ATT_value = Convert::raw2att($value); |
|
198
|
|
|
$fields .= "<input type=\"hidden\" name=\"$name\" value=\"$ATT_value\" />"; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return <<<HTML |
|
203
|
|
|
<form id="PaymentForm" method="post" action="$url">
|
|
204
|
|
|
$fields |
|
205
|
|
|
<input type="submit" value="Submit" /> |
|
206
|
|
|
</form> |
|
207
|
|
|
<script type="text/javascript"> |
|
208
|
|
|
jQuery(document).ready(function() { |
|
209
|
|
|
jQuery("input[type='submit']").hide(); |
|
210
|
|
|
jQuery('#PaymentForm').submit(); |
|
211
|
|
|
}); |
|
212
|
|
|
</script> |
|
213
|
|
|
HTML; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function populateDefaults() |
|
217
|
|
|
{ |
|
218
|
|
|
parent::populateDefaults(); |
|
219
|
|
|
$this->AuthorisationCode = md5(uniqid(rand(), true)); |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Requests a Token url, based on the provided Name-Value-Pair fields |
|
227
|
|
|
* See docs for more detail on these fields: |
|
228
|
|
|
* https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_SetExpressCheckout |
|
229
|
|
|
* |
|
230
|
|
|
* Note: some of these values will override the paypal merchant account settings. |
|
231
|
|
|
* Note: not all fields are listed here. |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getTokenURL($paymentAmount, $currencyCodeType, $extradata = array()) |
|
234
|
|
|
{ |
|
235
|
|
|
$data = array( |
|
236
|
|
|
//payment info |
|
237
|
|
|
'PAYMENTREQUEST_0_AMT' => $paymentAmount, |
|
238
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $currencyCodeType, //TODO: check to be sure all currency codes match the SS ones |
|
239
|
|
|
//TODO: include individual costs: shipping, shipping discount, insurance, handling, tax?? |
|
240
|
|
|
//'PAYMENTREQUEST_0_ITEMAMT' => //item(s) |
|
241
|
|
|
//'PAYMENTREQUEST_0_SHIPPINGAMT' //shipping |
|
242
|
|
|
//'PAYMENTREQUEST_0_SHIPDISCAMT' //shipping discount |
|
243
|
|
|
//'PAYMENTREQUEST_0_HANDLINGAMT' //handling |
|
244
|
|
|
//'PAYMENTREQUEST_0_TAXAMT' //tax |
|
245
|
|
|
//'PAYMENTREQUEST_0_INVNUM' => $this->PaidObjectID //invoice number |
|
|
|
|
|
|
246
|
|
|
//'PAYMENTREQUEST_0_TRANSACTIONID' //Transaction id |
|
247
|
|
|
//'PAYMENTREQUEST_0_DESC' => //description |
|
248
|
|
|
//'PAYMENTREQUEST_0_NOTETEXT' => //note to merchant |
|
249
|
|
|
//'PAYMENTREQUEST_0_PAYMENTACTION' => , //Sale, Order, or Authorization |
|
|
|
|
|
|
250
|
|
|
//'PAYMENTREQUEST_0_PAYMENTREQUESTID' |
|
251
|
|
|
//return urls |
|
252
|
|
|
'RETURNURL' => PayPalExpressCheckoutPayment_Handler::return_link(), |
|
253
|
|
|
'CANCELURL' => PayPalExpressCheckoutPayment_Handler::cancel_link(), |
|
254
|
|
|
//'PAYMENTREQUEST_0_NOTIFYURL' => //Instant payment notification |
|
255
|
|
|
//'CALLBACK' |
|
256
|
|
|
//'CALLBACKTIMEOUT' |
|
257
|
|
|
//shipping display |
|
258
|
|
|
//'REQCONFIRMSHIPPING' //require that paypal account address be confirmed |
|
259
|
|
|
'NOSHIPPING' => 1, //show shipping fields, or not 0 = show shipping, 1 = don't show shipping, 2 = use account address, if none passed |
|
260
|
|
|
//'ALLOWOVERRIDE' //display only the provided address, not the one stored in paypal |
|
261
|
|
|
//TODO: Probably overkill, but you can even include the prices,qty,weight,tax etc for individual sale items |
|
262
|
|
|
//other settings |
|
263
|
|
|
//'LOCALECODE' => //locale, or default to US |
|
264
|
|
|
'LANDINGPAGE' => 'Billing' //can be 'Billing' or 'Login' |
|
265
|
|
|
); |
|
266
|
|
|
|
|
267
|
|
|
if (!isset($extradata['Name'])) { |
|
268
|
|
|
$arr = array(); |
|
269
|
|
|
if (isset($extradata['FirstName'])) { |
|
270
|
|
|
$arr[] = $extradata['FirstName']; |
|
271
|
|
|
} |
|
272
|
|
|
if (isset($extradata['MiddleName'])) { |
|
273
|
|
|
$arr[] = $extradata['MiddleName']; |
|
274
|
|
|
} |
|
275
|
|
|
if (isset($extradata['Surname'])) { |
|
276
|
|
|
$arr[] = $extradata['Surname']; |
|
277
|
|
|
} |
|
278
|
|
|
$extradata['Name'] = implode(' ', $arr); |
|
279
|
|
|
} |
|
280
|
|
|
$extradata["OrderID"] = SiteConfig::current_site_config()->Title." ".$this->Order()->getTitle(); |
|
|
|
|
|
|
281
|
|
|
//add member & shipping fields, etc ...this will pre-populate the paypal login / create account form |
|
282
|
|
|
foreach (array( |
|
283
|
|
|
'Email' => 'EMAIL', |
|
284
|
|
|
'Name' => 'PAYMENTREQUEST_0_SHIPTONAME', |
|
285
|
|
|
'Address' => 'PAYMENTREQUEST_0_SHIPTOSTREET', |
|
286
|
|
|
'Address2' => 'PAYMENTREQUEST_0_SHIPTOSTREET2', |
|
287
|
|
|
'City' => 'PAYMENTREQUEST_0_SHIPTOCITY', |
|
288
|
|
|
'PostalCode' => 'PAYMENTREQUEST_0_SHIPTOZIP', |
|
289
|
|
|
'Region' => 'PAYMENTREQUEST_0_SHIPTOPHONENUM', |
|
290
|
|
|
'Phone' => 'PAYMENTREQUEST_0_SHIPTOPHONENUM', |
|
291
|
|
|
'Country' => 'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE', |
|
292
|
|
|
'OrderID' => 'PAYMENTREQUEST_0_DESC' |
|
293
|
|
|
) as $field => $val) { |
|
294
|
|
|
if (isset($extradata[$field])) { |
|
295
|
|
|
$data[$val] = $extradata[$field]; |
|
296
|
|
|
} elseif ($this->$field) { |
|
297
|
|
|
$data[$val] = $this->$field; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
//set design settings |
|
301
|
|
|
$data = array_merge($this->Config()->get("custom_settings"), $data); |
|
302
|
|
|
$response = $this->apiCall('SetExpressCheckout', $data); |
|
303
|
|
|
if (Config::inst()->get("PayPalExpressCheckoutPayment", "debug")) { |
|
304
|
|
|
$this->addDebugInfo("RESPONSE: ".print_r($response, 1)); |
|
305
|
|
|
$debugmessage = "PayPal Debug:" . |
|
306
|
|
|
"\nMode: $mode". |
|
|
|
|
|
|
307
|
|
|
"\nAPI url: ".$this->getApiEndpoint(). |
|
308
|
|
|
"\nRedirect url: ".$this->getPayPalURL($response['TOKEN']). |
|
309
|
|
|
"\nUsername: " .$this->Config()->get("API_UserName"). |
|
310
|
|
|
"\nPassword: " .$this->Config()->get("API_Password"). |
|
311
|
|
|
"\nSignature: ".$this->Config()->get("API_Signature"). |
|
312
|
|
|
"\nRequest Data: ".print_r($data, true). |
|
313
|
|
|
"\nResponse: ".print_r($response, true); |
|
314
|
|
|
$this->addDebugInfo("DEBUG MESSAGE: ".$debugmessage); |
|
315
|
|
|
} |
|
316
|
|
|
if (!isset($response['ACK']) || !(strtoupper($response['ACK']) == "SUCCESS" || strtoupper($response['ACK']) == "SUCCESSWITHWARNING")) { |
|
317
|
|
|
$mode = ($this->Config()->get("test_mode") === true) ? "test" : "live"; |
|
|
|
|
|
|
318
|
|
|
return null; |
|
319
|
|
|
} |
|
320
|
|
|
//get and save token for later |
|
321
|
|
|
$token = $response['TOKEN']; |
|
322
|
|
|
$this->Token = $token; |
|
|
|
|
|
|
323
|
|
|
$this->write(); |
|
324
|
|
|
return $this->getPayPalURL($token); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* see https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_DoExpressCheckoutPayment |
|
329
|
|
|
*/ |
|
330
|
|
|
public function confirmPayment() |
|
|
|
|
|
|
331
|
|
|
{ |
|
332
|
|
|
$data = array( |
|
333
|
|
|
'PAYERID' => $this->PayerID, |
|
|
|
|
|
|
334
|
|
|
'TOKEN' => $this->Token, |
|
|
|
|
|
|
335
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => "Sale", |
|
336
|
|
|
'PAYMENTREQUEST_0_AMT' => $this->Amount->Amount, |
|
|
|
|
|
|
337
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $this->Amount->Currency, |
|
|
|
|
|
|
338
|
|
|
'IPADDRESS' => urlencode($_SERVER['SERVER_NAME']) |
|
339
|
|
|
); |
|
340
|
|
|
$response = $this->apiCall('DoExpressCheckoutPayment', $data); |
|
341
|
|
|
if (!isset($response['ACK']) || !(strtoupper($response['ACK']) == "SUCCESS" || strtoupper($response['ACK']) == "SUCCESSWITHWARNING")) { |
|
342
|
|
|
return null; |
|
343
|
|
|
} |
|
344
|
|
|
if (isset($response["PAYMENTINFO_0_TRANSACTIONID"])) { |
|
345
|
|
|
//' Unique transaction ID of the payment. Note: If the PaymentAction of the request was Authorization or Order, this value is your AuthorizationID for use with the Authorization & Capture APIs. |
|
346
|
|
|
$this->TransactionID = $response["PAYMENTINFO_0_TRANSACTIONID"]; |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
//$transactionType = $response["PAYMENTINFO_0_TRANSACTIONTYPE"]; //' The type of transaction Possible values: l cart l express-checkout |
|
|
|
|
|
|
349
|
|
|
//$paymentType = $response["PAYMENTTYPE"]; //' Indicates whether the payment is instant or delayed. Possible values: l none l echeck l instant |
|
|
|
|
|
|
350
|
|
|
//$orderTime = $response["ORDERTIME"]; //' Time/date stamp of payment |
|
|
|
|
|
|
351
|
|
|
//TODO: should these be updated like this? |
|
352
|
|
|
//$this->Amount->Amount = $response["AMT"]; //' The final amount charged, including any shipping and taxes from your Merchant Profile. |
|
|
|
|
|
|
353
|
|
|
//$this->Amount->Currency= $response["CURRENCYCODE"]; //' A three-character currency code for one of the currencies listed in PayPay-Supported Transactional Currencies. Default: USD. |
|
|
|
|
|
|
354
|
|
|
//TODO: store this extra info locally? |
|
355
|
|
|
//$feeAmt = $response["FEEAMT"]; //' PayPal fee amount charged for the transaction |
|
|
|
|
|
|
356
|
|
|
//$settleAmt = $response["SETTLEAMT"]; //' Amount deposited in your PayPal account after a currency conversion. |
|
|
|
|
|
|
357
|
|
|
//$taxAmt = $response["TAXAMT"]; //' Tax charged on the transaction. |
|
|
|
|
|
|
358
|
|
|
//$exchangeRate = $response["EXCHANGERATE"]; //' Exchange rate if a currency conversion occurred. Relevant only if your are billing in their non-primary currency. If the customer chooses to pay with a currency other than the non-primary currency, the conversion occurs in the customer's account. |
|
|
|
|
|
|
359
|
|
|
if (isset($response["PAYMENTINFO_0_PAYMENTSTATUS"])) { |
|
360
|
|
|
switch (strtoupper($response["PAYMENTINFO_0_PAYMENTSTATUS"])) { |
|
361
|
|
|
case "PROCESSED": |
|
362
|
|
|
case "COMPLETED": |
|
363
|
|
|
$this->Status = 'Success'; |
|
|
|
|
|
|
364
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.SUCCESS', "The payment has been completed, and the funds have been successfully transferred"); |
|
|
|
|
|
|
365
|
|
|
break; |
|
366
|
|
|
case "EXPIRED": |
|
367
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.AUTHORISATION', "The authorization period for this payment has been reached"); |
|
|
|
|
|
|
368
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
369
|
|
|
break; |
|
370
|
|
|
case "DENIED": |
|
371
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.FAILURE', "Payment was denied"); |
|
|
|
|
|
|
372
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
373
|
|
|
break; |
|
374
|
|
|
case "REVERSED": |
|
375
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
376
|
|
|
break; |
|
377
|
|
|
case "VOIDED": |
|
378
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.VOIDED', "An authorization for this transaction has been voided."); |
|
|
|
|
|
|
379
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
380
|
|
|
break; |
|
381
|
|
|
case "FAILED": |
|
382
|
|
|
$this->Status = 'Failure'; |
|
|
|
|
|
|
383
|
|
|
break; |
|
384
|
|
|
case "CANCEL-REVERSAL": // A reversal has been canceled; for example, when you win a dispute and the funds for the reversal have been returned to you. |
|
385
|
|
|
break; |
|
386
|
|
|
case "IN-PROGRESS": |
|
387
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.INPROGRESS', "The transaction has not terminated");//, e.g. an authorization may be awaiting completion."; |
|
|
|
|
|
|
388
|
|
|
break; |
|
389
|
|
|
case "PARTIALLY-REFUNDED": |
|
390
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.PARTIALLYREFUNDED', "The payment has been partially refunded."); |
|
|
|
|
|
|
391
|
|
|
break; |
|
392
|
|
|
case "PENDING": |
|
393
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.PENDING', "The payment is pending."); |
|
|
|
|
|
|
394
|
|
|
if (isset($response["PAYMENTINFO_0_PENDINGREASON"])) { |
|
395
|
|
|
$this->Message .= " ".$this->getPendingReason($response["PAYMENTINFO_0_PENDINGREASON"]); |
|
|
|
|
|
|
396
|
|
|
} |
|
397
|
|
|
break; |
|
398
|
|
|
case "REFUNDED": |
|
399
|
|
|
$this->Message = _t('PayPalExpressCheckoutPayment.REFUNDED', "Payment refunded."); |
|
|
|
|
|
|
400
|
|
|
break; |
|
401
|
|
|
default: |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
//$reasonCode = $response["REASONCODE"]; |
|
|
|
|
|
|
405
|
|
|
$this->write(); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
protected function getPendingReason($reason) |
|
409
|
|
|
{ |
|
410
|
|
|
switch ($reason) { |
|
411
|
|
|
case "address": |
|
412
|
|
|
return _t('PayPalExpressCheckoutPayment.PENDING.ADDRESS', "A confirmed shipping address was not provided."); |
|
413
|
|
|
case "authorization": |
|
414
|
|
|
return _t('PayPalExpressCheckoutPayment.PENDING.AUTHORISATION', "Payment has been authorised, but not settled."); |
|
415
|
|
|
case "echeck": |
|
416
|
|
|
return _t('PayPalExpressCheckoutPayment.PENDING.ECHECK', "eCheck has not cleared."); |
|
417
|
|
|
case "intl": |
|
418
|
|
|
return _t('PayPalExpressCheckoutPayment.PENDING.INTERNATIONAL', "International: payment must be accepted or denied manually."); |
|
419
|
|
|
case "multicurrency": |
|
420
|
|
|
return _t('PayPalExpressCheckoutPayment.PENDING.MULTICURRENCY', "Multi-currency: payment must be accepted or denied manually."); |
|
421
|
|
|
case "order": |
|
422
|
|
|
case "paymentreview": |
|
423
|
|
|
case "unilateral": |
|
424
|
|
|
case "verify": |
|
425
|
|
|
case "other": |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Handles actual communication with API server. |
|
431
|
|
|
*/ |
|
432
|
|
|
protected function apiCall($method, $data = array()) |
|
433
|
|
|
{ |
|
434
|
|
|
$this->addDebugInfo('---------------------------------------'); |
|
435
|
|
|
$this->addDebugInfo('---------------------------------------'); |
|
436
|
|
|
$this->addDebugInfo('---------------------------------------'); |
|
437
|
|
|
$postfields = array( |
|
438
|
|
|
'METHOD' => $method, |
|
439
|
|
|
'VERSION' => $this->Config()->get("version"), |
|
440
|
|
|
'USER' => $this->Config()->get("API_UserName"), |
|
441
|
|
|
'PWD'=> $this->Config()->get("API_Password"), |
|
442
|
|
|
'SIGNATURE' => $this->Config()->get("API_Signature"), |
|
443
|
|
|
'BUTTONSOURCE' => $this->Config()->get("sBNCode") |
|
444
|
|
|
); |
|
445
|
|
|
if (Config::inst()->get("PayPalExpressCheckoutPayment", "debug")) { |
|
446
|
|
|
$this->addDebugInfo("STANDARD POSTING FIELDS .... //// : ".print_r($postfields, 1)); |
|
447
|
|
|
$this->addDebugInfo("ADDITIONAL POSTING FIELDS .... //// : ".print_r($data, 1)); |
|
448
|
|
|
$this->addDebugInfo("SENDING TO .... //// : ".print_r($this->getApiEndpoint(), 1)); |
|
449
|
|
|
} |
|
450
|
|
|
$postfields = array_merge($postfields, $data); |
|
451
|
|
|
//Make POST request to Paypal via RESTful service |
|
452
|
|
|
$rs = new RestfulService($this->getApiEndpoint(), 0); //REST connection that will expire immediately |
|
453
|
|
|
$rs->httpHeader('Accept: application/xml'); |
|
454
|
|
|
$rs->httpHeader('Content-Type: application/x-www-form-urlencoded'); |
|
455
|
|
|
$response = $rs->request('', 'POST', http_build_query($postfields)); |
|
456
|
|
|
if (Config::inst()->get("PayPalExpressCheckoutPayment", "debug")) { |
|
457
|
|
|
$this->addDebugInfo('RESPONSE .... //// : '.print_r($response, 1)); |
|
458
|
|
|
} |
|
459
|
|
|
return $this->deformatNVP($response->getBody()); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
protected function deformatNVP($nvpstr) |
|
463
|
|
|
{ |
|
464
|
|
|
$intial = 0; |
|
465
|
|
|
$nvpArray = array(); |
|
466
|
|
|
while (strlen($nvpstr)) { |
|
467
|
|
|
//postion of Key |
|
468
|
|
|
$keypos= strpos($nvpstr, '='); |
|
469
|
|
|
//position of value |
|
470
|
|
|
$valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&'): strlen($nvpstr); |
|
471
|
|
|
/*getting the Key and Value values and storing in a Associative Array*/ |
|
472
|
|
|
$keyval=substr($nvpstr, $intial, $keypos); |
|
473
|
|
|
$valval=substr($nvpstr, $keypos+1, $valuepos-$keypos-1); |
|
474
|
|
|
//decoding the respose |
|
475
|
|
|
$nvpArray[urldecode($keyval)] =urldecode($valval); |
|
476
|
|
|
$nvpstr=substr($nvpstr, $valuepos+1, strlen($nvpstr)); |
|
477
|
|
|
} |
|
478
|
|
|
return $nvpArray; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
protected function getApiEndpoint() |
|
|
|
|
|
|
482
|
|
|
{ |
|
483
|
|
|
return ($this->Config()->get("test_mode") === true) ? $this->Config()->get("test_API_Endpoint") : $this->Config()->get("API_Endpoint"); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
protected function getPayPalURL($token) |
|
487
|
|
|
{ |
|
488
|
|
|
$url = ($this->Config()->get("test_mode") === true) ? $this->Config()->get("test_PAYPAL_URL") : $this->Config()->get("PAYPAL_URL"); |
|
489
|
|
|
return $url.$token.'&useraction=commit'; //useraction=commit ensures the payment is confirmed on PayPal, and not on a merchant confirm page. |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
|
|
493
|
|
|
protected function addDebugInfo($msg) |
|
494
|
|
|
{ |
|
495
|
|
|
$this->Debug .= "---------//------------\n\n".$msg; |
|
|
|
|
|
|
496
|
|
|
$this->write(); |
|
497
|
|
|
} |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Handler for responses from the PayPal site |
|
502
|
|
|
*/ |
|
503
|
|
|
class PayPalExpressCheckoutPayment_Handler extends Controller |
|
|
|
|
|
|
504
|
|
|
{ |
|
505
|
|
|
private static $url_segment = 'paypalexpresscheckoutpayment_handler'; |
|
|
|
|
|
|
506
|
|
|
|
|
507
|
|
|
protected $payment = null; //only need to get this once |
|
508
|
|
|
|
|
509
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
510
|
|
|
'confirm', |
|
511
|
|
|
'cancel' |
|
512
|
|
|
); |
|
513
|
|
|
|
|
514
|
|
|
public function Link($action = null) |
|
515
|
|
|
{ |
|
516
|
|
|
return Controller::join_links( |
|
517
|
|
|
Director::baseURL(), |
|
518
|
|
|
$this->Config()->get("url_segment"), |
|
519
|
|
|
$action |
|
520
|
|
|
); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
public function payment() |
|
|
|
|
|
|
524
|
|
|
{ |
|
525
|
|
|
if ($this->payment) { |
|
526
|
|
|
return $this->payment; |
|
527
|
|
|
} elseif ($token = Controller::getRequest()->getVar('token')) { |
|
528
|
|
|
$payment = PayPalExpressCheckoutPayment::get() |
|
529
|
|
|
->filter( |
|
530
|
|
|
array( |
|
531
|
|
|
"Token" => $token, |
|
532
|
|
|
"Status" => "Incomplete" |
|
533
|
|
|
) |
|
534
|
|
|
) |
|
535
|
|
|
->first(); |
|
536
|
|
|
$this->payment = $payment; |
|
537
|
|
|
$this->payment->init(); |
|
538
|
|
|
return $this->payment; |
|
539
|
|
|
} |
|
540
|
|
|
return null; |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
public function confirm($request) |
|
|
|
|
|
|
544
|
|
|
{ |
|
545
|
|
|
//TODO: pretend the user confirmed, and skip straight to results. (check that this is allowed) |
|
546
|
|
|
//TODO: get updated shipping details from paypal?? |
|
547
|
|
|
if ($payment = $this->payment()) { |
|
548
|
|
|
if ($pid = Controller::getRequest()->getVar('PayerID')) { |
|
549
|
|
|
$payment->PayerID = $pid; |
|
550
|
|
|
$payment->write(); |
|
551
|
|
|
$payment->confirmPayment(); |
|
552
|
|
|
} |
|
553
|
|
|
} else { |
|
|
|
|
|
|
554
|
|
|
//something went wrong? ..perhaps trying to pay for a payment that has already been processed |
|
555
|
|
|
} |
|
556
|
|
|
$this->doRedirect(); |
|
557
|
|
|
return; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
public function cancel($request) |
|
|
|
|
|
|
561
|
|
|
{ |
|
562
|
|
|
if ($payment = $this->payment()) { |
|
563
|
|
|
//TODO: do API call to gather further information |
|
564
|
|
|
$payment->Status = "Failure"; |
|
565
|
|
|
$payment->Message = _t('PayPalExpressCheckoutPayment.USERCANCELLED', "User cancelled"); |
|
566
|
|
|
$payment->write(); |
|
567
|
|
|
} |
|
568
|
|
|
$this->doRedirect(); |
|
569
|
|
|
return; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
protected function doRedirect() |
|
573
|
|
|
{ |
|
574
|
|
|
$payment = $this->payment(); |
|
575
|
|
|
if ($payment && $obj = $payment->PaidObject()) { |
|
576
|
|
|
$this->redirect($obj->Link()); |
|
577
|
|
|
return; |
|
578
|
|
|
} |
|
579
|
|
|
$this->redirect(Director::absoluteURL('home', true)); //TODO: make this customisable in Payment_Controllers |
|
580
|
|
|
return; |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
public static function return_link() |
|
584
|
|
|
{ |
|
585
|
|
|
return Director::absoluteURL(Config::inst()->get("PayPalExpressCheckoutPayment_Handler", "url_segment"), true)."/confirm/"; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
public static function cancel_link() |
|
589
|
|
|
{ |
|
590
|
|
|
return Director::absoluteURL(Config::inst()->get("PayPalExpressCheckoutPayment_Handler", "url_segment"), true)."/cancel/"; |
|
591
|
|
|
} |
|
592
|
|
|
} |
|
593
|
|
|
|