This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
paypal-purchase.class.php
334 lines (277 loc) · 13.5 KB
/
paypal-purchase.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* A PayPal Purchase object for the Digital Goods with Express Checkout API.
*
* @package PayPal
* @subpackage Purchase
*
* @license GPLv3 see license.txt
* @copyright 2011 Leonard's Ego Pty. Ltd.
*/
require_once( 'paypal-digital-goods.class.php' );
class PayPal_Purchase extends PayPal_Digital_Goods {
/**
* Details of the items in this particular purchase.
*/
private $purchase;
/**
* Creates a PayPal Digital Goods Object configured according to the parameters in the args associative array.
*
* Available $args parameters:
* - purchase_details, array, details of the purchase.
* - description, string, (Optional) Description of items the buyer is purchasing.
* Transaction Totals:
* - amount, double, required. Total cost of the transaction to the buyer, including tax.
* - tax, double, default 0.00. The sum of tax for all items in this order.
* - invoice_number, string, (Optional) Your own invoice or tracking number. Can be up to 127 single-byte alphanumeric characters.
* Item details:
* - items array, An array of arrays for each item in this transaction.
* - item_name, string, Item name.
* - item_description, string, (Optional) Item description.
* - item_amount, string, (Optional) Cost of this individual item.
* - item_quantity, string, default 1. Number of this specific item.
* - item_tax, string, (Optional) Sales tax of this individual item.
* - item_number, string, (Optional) Your own invoice or tracking number. Can be up to 127 single-byte alphanumeric characters.
* Miscellaneous
* - custom (Optional) A free-form field for your own use.
*
* @todo Allow purchase details to include more than one item.
*
* @param args, named parameters to customise the subscription and checkout process. See description for available parameters.
*/
public function __construct( $purchase_details = array() ){
$purchase_defaults = array(
'name' => 'Digital Good',
'description' => '',
// Price
'amount' => '5.00',
'tax_amount' => '0.00',
'invoice_number' => '',
'number' => '',
'items' => array(),
'custom' => '',
);
$purchase_details = array_merge( $purchase_defaults, $purchase_details );
// Make it super simple to create a single item transaction
if( empty( $purchase_details['items'] ) )
$purchase_details['items'] = array( array(
'item_name' => $purchase_details['name'],
'item_description' => $purchase_details['description'],
'item_amount' => $purchase_details['amount'],
'item_tax' => $purchase_details['tax_amount'],
'item_quantity' => 1,
'item_number' => $purchase_details['number']
) );
$this->purchase = (object)$purchase_details;
parent::__construct();
}
/**
* Makes a payment by calling the PayPal DoExpressCheckoutPayment API operation.
*
* After an express checkout transaction has been created in request_checkout_token, this function can
* be called to complete the transaction.
*
* This function returned the response from PayPal, which includes a profile ID (PROFILEID). You should
* save this profile ID to aid with changing and
*
* @return array(
* [TOKEN] => EC-XXXX
* [SUCCESSPAGEREDIRECTREQUESTED] => false
* [TIMESTAMP] => YYYY-MM-DDTHH:MM:SSZ
* [CORRELATIONID] => XXXX
* [ACK] => Success
* [VERSION] => 76.0
* [BUILD] => 2271164
* [INSURANCEOPTIONSELECTED] => false
* [SHIPPINGOPTIONISDEFAULT] => false
* [PAYMENTINFO_0_TRANSACTIONID] => XXXX
* [PAYMENTINFO_0_TRANSACTIONTYPE] => cart
* [PAYMENTINFO_0_PAYMENTTYPE] => instant
* [PAYMENTINFO_0_ORDERTIME] => YYYY-MM-DDTHH:MM:SSZ
* [PAYMENTINFO_0_AMT] => XX.00
* [PAYMENTINFO_0_FEEAMT] => 0.XX
* [PAYMENTINFO_0_TAXAMT] => 0.00
* [PAYMENTINFO_0_CURRENCYCODE] => USD
* [PAYMENTINFO_0_PAYMENTSTATUS] => Completed | None | Denied | Expired | Failed | In-Progress | Partially-Refunded | Pending | Refunded | Reversed | Processed | Voided
* [PAYMENTINFO_0_PENDINGREASON] => None
* [PAYMENTINFO_0_REASONCODE] => None
* [PAYMENTINFO_0_PROTECTIONELIGIBILITY] => Ineligible
* [PAYMENTINFO_0_PROTECTIONELIGIBILITYTYPE] => None
* [PAYMENTINFO_0_SECUREMERCHANTACCOUNTID] => XXXX
* [PAYMENTINFO_0_ERRORCODE] => 0
* [PAYMENTINFO_0_ACK] => Success
* )
*/
public function process_payment(){
return $this->call_paypal( 'DoExpressCheckoutPayment' );
}
/**
* A wrapper for the process_payment function to implement the unified Digital Goods API.
*/
public function process(){
return $this->process_payment();
}
/**
* Returns information about a purchase transaction by calling the PayPal GetTransactionDetails API method.
*
* @param $profile_id, string. The profile ID of the subscription for which the details should be looked up.
* @return array (
* [RECEIVEREMAIL] => [email protected]
* [RECEIVERID] => XXXX
* [EMAIL] => [email protected]
* [PAYERID] => XXXX
* [PAYERSTATUS] => verified
* [COUNTRYCODE] => US
* [ADDRESSOWNER] => PayPal
* [ADDRESSSTATUS] => None
* [SALESTAX] => 0.00
* [SUBJECT] => Example Digital Good Purchase
* [TIMESTAMP] => YYYY-MM-DDTHH:MM:SSZ
* [CORRELATIONID] => XXXX
* [ACK] => Success
* [VERSION] => 76.0
* [BUILD] => 2230381
* [FIRSTNAME] => Test
* [LASTNAME] => User
* [TRANSACTIONID] => XXXX
* [TRANSACTIONTYPE] => cart
* [PAYMENTTYPE] => instant
* [ORDERTIME] => YYYY-MM-DDTHH:MM:SSZ
* [AMT] => XX.00
* [FEEAMT] => 0.XX
* [TAXAMT] => 0.00
* [SHIPPINGAMT] => 0.00
* [HANDLINGAMT] => 0.00
* [CURRENCYCODE] => USD
* [PAYMENTSTATUS] => Completed
* [PENDINGREASON] => None
* [REASONCODE] => None
* [PROTECTIONELIGIBILITY] => Ineligible
* [PROTECTIONELIGIBILITYTYPE] => None
* [L_NAME0] => Digital Good Example
* [L_QTY0] => 1
* [L_SHIPPINGAMT0] => 0.00
* [L_HANDLINGAMT0] => 0.00
* [L_CURRENCYCODE0] => USD
* [L_AMT0] => XX.00
* )
*/
public function get_transaction_details( $transaction_id ){
return $this->call_paypal( 'GetTransactionDetails', $transaction_id );
}
/**
* A wrapper for the get_transaction_details function implementing the unified API.
*
* Accepts either a get_transaction_details ID or a $response array as returned
* from the SetExpressCheckout call.
*/
public function get_details( $transaction ){
if ( is_array( $transaction ) && isset( $transaction['PAYMENTINFO_0_TRANSACTIONID'] ) )
$transaction = $transaction['PAYMENTINFO_0_TRANSACTIONID'];
return $this->get_transaction_details( $transaction );
}
/**
* Overloads the base class's get_payment_details_url to map the subscription details
* to the PayPal NVP format for posting to PayPal.
*
* The function first calls the Parent's get_payment_details_url, which takes care of
* common NVP fields, like API credentials. It then appends subscription related fields.
*
* Note: The SetExpressCheckout action URL is partially built in the base class as it contains
* NVP fields common to both Purchases & Subscriptions.
*
* @param $action, string. The PayPal NVP API action to create the URL for. One of SetExpressCheckout, DoExpressCheckoutPayment or GetTransactionDetails.
* @param $transaction_id, (optional) string. A PayPal Transaction ID, required for the GetTransactionDetails operation.
* @return string A URI which can be use in the @see call_paypal() method to perform the appropriate API operation.
*/
protected function get_payment_details_url( $action, $transaction_id = '' ){
// Setup the Payment Details
$api_request = parent::get_payment_details_url( $action, $transaction_id );
// Parameters to Request Recurring Payment Token
if( 'SetExpressCheckout' == $action ) {
$api_request .= '&PAYMENTREQUEST_0_CURRENCYCODE=' . urlencode( $this->currency ) // A 3-character currency code (default is USD).
. '&PAYMENTREQUEST_0_PAYMENTACTION=Sale' // From PayPal: When implementing digital goods, this field is required and must be set to Sale.
// Payment details
. '&PAYMENTREQUEST_0_AMT=' . $this->purchase->amount // (Required) Total cost of the transaction to the buyer. If tax charges are known, include them in this value. If not, this value should be the current sub-total of the order. If the transaction includes one or more one-time purchases, this field must be equal to the sum of the purchases.
. '&PAYMENTREQUEST_0_ITEMAMT=' . ( $this->purchase->amount - $this->purchase->tax_amount ) // (Required) Sum of cost of all items in this order.
. '&PAYMENTREQUEST_0_TAXAMT=' . $this->purchase->tax_amount // (Optional) Sum of tax for all items in this order
. '&PAYMENTREQUEST_0_DESC=' . urlencode( $this->purchase->description ); // (Optional) Description of items the buyer is purchasing.
// Maybe add an Invoice number
if( ! empty( $this->purchase->invoice_number ) )
$api_request .= '&PAYMENTREQUEST_0_INVNUM=' . $this->purchase->invoice_number; // (Optional) Your own invoice or tracking number.
// Maybe add an IPN URL
if( ! empty( $this->notify_url ) )
$api_request .= '&PAYMENTREQUEST_0_NOTIFYURL=' . urlencode( $this->notify_url );
// Maybe add a custom field
if( ! empty( $this->purchase->custom ) )
$api_request .= '&PAYMENTREQUEST_0_CUSTOM=' . urlencode( $this->purchase->custom );
// Item details
$item_count = 0;
foreach( $this->purchase->items as $item ) {
$api_request .= '&L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_count.'=Digital'
. '&L_PAYMENTREQUEST_0_NAME'.$item_count.'=' . urlencode( $item['item_name'] )
. '&L_PAYMENTREQUEST_0_AMT'.$item_count.'=' . $item['item_amount']
. '&L_PAYMENTREQUEST_0_QTY'.$item_count.'=' . $item['item_quantity'];
if( ! empty( $item['item_description'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_DESC'.$item_count.'=' . urlencode( $item['item_description'] );
if( ! empty( $item['item_tax'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_TAXAMT'.$item_count.'=' . $item['item_tax'];
if( ! empty( $item['item_number'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_NUMBER'.$item_count.'=' . $item['item_number'];
$item_count++;
}
} elseif ( 'DoExpressCheckoutPayment' == $action ) {
$api_request .= '&METHOD=DoExpressCheckoutPayment'
. '&TOKEN=' . $this->token
. '&PAYERID=' . $_GET['PayerID']
// Payment details
. '&PAYMENTREQUEST_0_CURRENCYCODE=' . urlencode( $this->currency )
. '&PAYMENTREQUEST_0_PAYMENTACTION=Sale' // From PayPal: When implementing digital goods, this field is required and must be set to Sale.
// Payment details
. '&PAYMENTREQUEST_0_AMT=' . $this->purchase->amount // (Required) Total cost of the transaction to the buyer. If tax charges are known, include them in this value. If not, this value should be the current sub-total of the order. If the transaction includes one or more one-time purchases, this field must be equal to the sum of the purchases.
. '&PAYMENTREQUEST_0_ITEMAMT=' . ( $this->purchase->amount - $this->purchase->tax_amount ) // (Required) Sum of cost of all items in this order.
. '&PAYMENTREQUEST_0_TAXAMT=' . $this->purchase->tax_amount // (Optional) Sum of tax for all items in this order
. '&PAYMENTREQUEST_0_DESC=' . urlencode( $this->purchase->description ); // (Optional) Description of items the buyer is purchasing.
// Maybe add an Invoice number
if( ! empty( $this->purchase->invoice_number ) )
$api_request .= '&PAYMENTREQUEST_0_INVNUM=' . $this->purchase->invoice_number; // (Optional) Your own invoice or tracking number.
// Maybe add an IPN URL
if( ! empty( $this->notify_url ) )
$api_request .= '&PAYMENTREQUEST_0_NOTIFYURL=' . urlencode( $this->notify_url );
// Maybe add a custom field
if( ! empty( $this->purchase->custom ) )
$api_request .= '&PAYMENTREQUEST_0_CUSTOM=' . urlencode( $this->purchase->custom );
// Item details
$item_count = 0;
foreach( $this->purchase->items as $item ) {
$api_request .= '&L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_count.'=Digital'
. '&L_PAYMENTREQUEST_0_NAME'.$item_count.'=' . urlencode( $item['item_name'] )
. '&L_PAYMENTREQUEST_0_AMT'.$item_count.'=' . $item['item_amount']
. '&L_PAYMENTREQUEST_0_QTY'.$item_count.'=' . $item['item_quantity'];
if( ! empty( $item['item_description'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_DESC'.$item_count.'=' . urlencode( $item['item_description'] );
if( ! empty( $item['item_tax'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_TAXAMT'.$item_count.'=' . $item['item_tax'];
if( ! empty( $item['item_number'] ) )
$api_request .= '&L_PAYMENTREQUEST_0_NUMBER'.$item_count.'=' . $item['item_number'];
$item_count++;
}
} elseif ( 'GetTransactionDetails' == $action ) {
$api_request .= '&METHOD=GetTransactionDetails'
. '&TRANSACTIONID=' . urlencode( $transaction_id );
}
return $api_request;
}
/**
* Returns a string representing the price for the purchase, including currency code. For example "$10".
*/
public function get_purchase_price() {
return $this->get_currency_symbol() . $this->purchase->amount;
}
/**
* Get the description for this subscription
*/
public function get_description(){
return $this->purchase->description;
}
}