This repository has been archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathpaypal-mobile-js-helper.js
230 lines (211 loc) · 10.2 KB
/
paypal-mobile-js-helper.js
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
//
// Copyright (c) 2014 PayPal. All rights reserved.
//
/**
* The PayPalItem class defines an optional itemization for a payment.
* @see https://developer.paypal.com/docs/api/#item-object for more details.
* @param {String} name: Name of the item. 127 characters max
* @param {Number} quantity: Number of units. 10 characters max.
* @param {String} price: Unit price for this item 10 characters max.
* May be negative for "coupon" etc
* @param {String} currency: ISO standard currency code.
* @param {String} sku: The stock keeping unit for this item. 50 characters max (optional)
*/
function PayPalItem(name, quantity, price, currency, sku) {
this.name = String(name);
this.quantity = Number(quantity);
this.price = String(price);
this.currency = String(currency);
this.sku = sku;
}
/**
* The PayPalPaymentDetails class defines optional amount details.
* @param {String} subtotal: Sub-total (amount) of items being paid for. 10 characters max with support for 2 decimal places.
* @param {String} shipping: Amount charged for shipping. 10 characters max with support for 2 decimal places.
* @param {String} tax: Amount charged for tax. 10 characters max with support for 2 decimal places.
* @see https://developer.paypal.com/webapps/developer/docs/api/#details-object for more details.
*/
function PayPalPaymentDetails(subtotal, shipping, tax) {
this.subtotal = String(subtotal);
this.shipping = shipping;
this.tax = tax;
}
/**
* Convenience constructor. Returns a PayPalPayment with the specified amount, currency code, and short description.
* @param {String} amount: The amount of the payment.
* @param {String} currencyCode: The ISO 4217 currency for the payment.
* @param {String} shortDescription: A short descripton of the payment.
* @param {String} intent: "Sale" for an immediate payment.
* "Auth" for payment authorization only, to be captured separately at a later time.
* "Order" for taking an order, with authorization and capture to be done separately at a later time.
* @param {PayPalPaymentDetails} details: PayPalPaymentDetails object (optional)
*/
function PayPalPayment(amount, currency, shortDescription, intent, details) {
this.amount = String(amount);
this.currency = String(currency);
this.shortDescription = String(shortDescription);
this.intent = String(intent);
this.details = details;
this.bnCode = "PhoneGap_SP";
}
/**
* Optional invoice number, for your tracking purposes. (up to 256 characters)
* @param {String} invoiceNumber: The invoice number for the payment.
*/
PayPalPayment.prototype.invoiceNumber = function(invoiceNumber) {
this.invoiceNumber = invoiceNumber;
};
/**
* Optional text, for your tracking purposes. (up to 256 characters)
* @param {String} custom: The custom text for the payment.
*/
PayPalPayment.prototype.custom = function(custom) {
this.custom = custom;
};
/**
* Optional text which will appear on the customer's credit card statement. (up to 22 characters)
* @param {String} softDescriptor: credit card text for payment
*/
PayPalPayment.prototype.softDescriptor = function(softDescriptor) {
this.softDescriptor = softDescriptor;
};
/**
* Optional Build Notation code ("BN code"), obtained from [email protected],
* for your tracking purposes.
* @param {String} bnCode: bnCode for payment
*/
PayPalPayment.prototype.bnCode = function(bnCode) {
this.bnCode = bnCode;
};
/**
* Optional array of PayPalItem objects. @see PayPalItem
* @note If you provide one or more items, be sure that the various prices correctly
* sum to the payment `amount` or to `paymentDetails.subtotal`.
* @param {String} bnCode: bnCode for payment
*/
PayPalPayment.prototype.items = function(items) {
this.items = items;
};
/**
* Optional payee email, if your app is paying a third-party merchant.
* @param {String} payeeEmail: The payee's email. It must be a valid PayPal email address.
*/
PayPalPayment.prototype.payeeEmail = function(payeeEmail) {
this.payeeEmail = payeeEmail;
};
/**
* Optional customer shipping address, if your app wishes to provide this to the SDK.
* @note make sure to set `payPalShippingAddressOption` in PayPalConfiguration to 1 or 3.
* @param {Object} shippingAddress: PayPalShippingAddress object
*/
PayPalPayment.prototype.shippingAddress = function(shippingAddress) {
this.shippingAddress = shippingAddress;
};
/**
* You use a PayPalConfiguration object to configure many aspects of how the SDK behaves.
* see defaults for options available
*/
function PayPalConfiguration(options) {
var defaults = {
/// Will be overridden by email used in most recent PayPal login.
defaultUserEmail: null,
/// Will be overridden by phone country code used in most recent PayPal login
defaultUserPhoneCountryCode: null,
/// Will be overridden by phone number used in most recent PayPal login.
/// @note If you set defaultUserPhoneNumber, be sure to also set defaultUserPhoneCountryCode.
defaultUserPhoneNumber: null,
/// Your company name, as it should be displayed to the user
/// when requesting consent via a PayPalFuturePaymentViewController.
merchantName: null,
/// URL of your company's privacy policy, which will be offered to the user
/// when requesting consent via a PayPalFuturePaymentViewController.
merchantPrivacyPolicyURL: null,
/// URL of your company's user agreement, which will be offered to the user
/// when requesting consent via a PayPalFuturePaymentViewController.
merchantUserAgreementURL: null,
/// If set to NO, the SDK will only support paying with PayPal, not with credit cards.
/// This applies only to single payments (via PayPalPaymentViewController).
/// Future payments (via PayPalFuturePaymentViewController) always use PayPal.
/// Defaults to YES
acceptCreditCards: true,
/// For single payments, options for the shipping address.
/// - 0 - PayPalShippingAddressOptionNone: no shipping address applies.
/// - 1 - PayPalShippingAddressOptionProvided: shipping address will be provided by your app,
/// in the shippingAddress property of PayPalPayment.
/// - 2 - PayPalShippingAddressOptionPayPal: user will choose from shipping addresses on file
/// for their PayPal account.
/// - 3 - PayPalShippingAddressOptionBoth: user will choose from the shipping address provided by your app,
/// in the shippingAddress property of PayPalPayment, plus the shipping addresses on file for the user's PayPal account.
/// Defaults to 0 (PayPalShippingAddressOptionNone).
payPalShippingAddressOption: 0,
/// If set to YES, then if the user pays via their PayPal account,
/// the SDK will remember the user's PayPal username or phone number;
/// if the user pays via their credit card, then the SDK will remember
/// the PayPal Vault token representing the user's credit card.
///
/// If set to NO, then any previously-remembered username, phone number, or
/// credit card token will be erased, and subsequent payment information will
/// not be remembered.
///
/// Defaults to YES.
rememberUser: true,
/// If not set, or if set to nil, defaults to the device's current language setting.
///
/// Can be specified as a language code ("en", "fr", "zh-Hans", etc.) or as a locale ("en_AU", "fr_FR", "zh-Hant", etc.).
/// If the library does not contain localized strings for a specified locale, then will fall back to the language. E.g., "es_CO" -> "es".
/// If the library does not contain localized strings for a specified language, then will fall back to American English.
///
/// If you specify only a language code, and that code matches the device's currently preferred language,
/// then the library will attempt to use the device's current region as well.
/// E.g., specifying "en" on a device set to "English" and "United Kingdom" will result in "en_GB".
///
/// These localizations are currently included:
/// da,de,en,en_AU,en_GB,es,es_MX,fr,he,it,ja,ko,nb,nl,pl,pt,pt_BR,ru,sv,tr,zh-Hans,zh-Hant,zh-Hant_TW.
languageOrLocale: null,
/// Normally, the SDK blurs the screen when the app is backgrounded,
/// to obscure credit card or PayPal account details in the iOS-saved screenshot.
/// If your app already does its own blurring upon backgrounding, you might choose to disable this.
/// Defaults to NO.
disableBlurWhenBackgrounding: false,
/// If you will present the SDK's view controller within a popover, then set this property to YES.
/// Defaults to NO. (iOS only)
presentingInPopover: false,
/// Sandbox credentials can be difficult to type on a mobile device. Setting this flag to YES will
/// cause the sandboxUserPassword and sandboxUserPin to always be pre-populated into login fields.
///
/// This setting will have no effect if the operation mode is production.
forceDefaultsInSandbox: false,
/// Password to use for sandbox if 'forceDefaultsInSandbox' is set.
sandboxUserPassword: null,
/// PIN to use for sandbox if 'forceDefaultsInSandbox' is set.
sandboxUserPin: null
};
if (!options || typeof options !== "object") {
return defaults;
}
for (var i in options) {
if (defaults.hasOwnProperty(i)) {
defaults[i] = options[i];
}
}
return defaults;
}
/**
* See the documentation of the individual properties for more detail.
* @param {String} recipientName: Name of the recipient at this address. 50 characters max.
* @param {String} line1: Line 1 of the address (e.g., Number, street, etc). 100 characters max.
* @param {String} Line 2 of the address (e.g., Suite, apt #, etc). 100 characters max. Optional.
* @param {String} city: City name. 50 characters max.
* @param {String} state: 2-letter code for US states, and the equivalent for other countries. 100 characters max. Required in certain countries.
* @param {String} postalCode: ZIP code or equivalent is usually required for countries that have them. 20 characters max. Required in certain countries.
* @param {String} countryCode: 2-letter country code. 2 characters max.
*/
function PayPalShippingAddress(recipientName, line1, line2, city, state, postalCode, countryCode) {
this.recipientName = recipientName;
this.line1 = line1;
this.line2 = line2;
this.city = city;
this.state = state;
this.postalCode = postalCode;
this.countryCode = countryCode;
}