forked from azmenak/react-stripe-checkout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripeCheckout.jsx
259 lines (217 loc) · 8.42 KB
/
StripeCheckout.jsx
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
'use strict';
var React = require('react');
var ReactScriptLoaderMixin = require('react-script-loader').ReactScriptLoaderMixin;
var ReactStripeCheckout = React.createClass({
mixins: [ReactScriptLoaderMixin],
getDefaultProps: function() {
return {
className: 'StripeCheckout',
label: 'Pay With Card',
locale: 'auto'
};
},
propTypes: {
// If included, will render the default blue button with label text.
// (Requires including stripe-checkout.css or adding the .styl file
// to your pipeline)
label: React.PropTypes.string,
// Show a loading indicator
showLoadingDialog: React.PropTypes.func,
// Hide the loading indicator
hideLoadingDialog: React.PropTypes.func,
// Run this method when the scrupt fails to load. Will run if the internet
// connection is offline when attemting to load the script.
onScriptError: React.PropTypes.func,
// =====================================================
// Required by stripe
// see Stripe docs for more info:
// https://stripe.com/docs/checkout#integration-custom
// =====================================================
// Your publishable key (test or live).
// can't use "key" as a prop in react, so have to change the keyname
stripeKey: React.PropTypes.string.isRequired,
// The callback to invoke when the Checkout process is complete.
// function(token)
// token is the token object created.
// token.id can be used to create a charge or customer.
// token.email contains the email address entered by the user.
token: React.PropTypes.func.isRequired,
// ==========================
// Highly Recommended Options
// ==========================
// Name of the company or website.
name: React.PropTypes.string,
// A description of the product or service being purchased.
description: React.PropTypes.string,
// A relative URL pointing to a square image of your brand or product. The
// recommended minimum size is 128x128px. The recommended image types are
// .gif, .jpeg, and .png.
image: React.PropTypes.string,
// The amount (in cents) that's shown to the user. Note that you will still
// have to explicitly include it when you create a charge using the API.
amount: React.PropTypes.number,
// Specify auto to display Checkout in the user's preferred language, if
// available. English will be used by default.
//
// https://support.stripe.com/questions/what-languages-does-stripe-checkout-support
// for more info.
locale: React.PropTypes.oneOf([
'auto', // (Default) Automatically chosen by checkout
'zh', //Chinese
'nl', //Dutch
'en', //English
'fr', //French
'de', //German
'it', //Italian
'jp', //Japanease
'es' //Spanish
]),
// ==============
// Optional Props
// ==============
// The currency of the amount (3-letter ISO code). The default is USD.
currency: React.PropTypes.oneOf([
'AED','AFN','ALL','AMD','ANG','AOA','ARS','AUD','AWG','AZN','BAM','BBD',
'BDT','BGN','BIF','BMD','BND','BOB','BRL','BSD','BWP','BZD','CAD','CDF',
'CHF','CLP','CNY','COP','CRC','CVE','CZK','DJF','DKK','DOP','DZD','EEK',
'EGP','ETB','EUR','FJD','FKP','GBP','GEL','GIP','GMD','GNF','GTQ','GYD',
'HKD','HNL','HRK','HTG','HUF','IDR','ILS','INR','ISK','JMD','JPY','KES',
'KGS','KHR','KMF','KRW','KYD','KZT','LAK','LBP','LKR','LRD','LSL','LTL',
'LVL','MAD','MDL','MGA','MKD','MNT','MOP','MRO','MUR','MVR','MWK','MXN',
'MYR','MZN','NAD','NGN','NIO','NOK','NPR','NZD','PAB','PEN','PGK','PHP',
'PKR','PLN','PYG','QAR','RON','RSD','RUB','RWF','SAR','SBD','SCR','SEK',
'SGD','SHP','SLL','SOS','SRD','STD','SVC','SZL','THB','TJS','TOP','TRY',
'TTD','TWD','TZS','UAH','UGX','USD','UYU','UZS','VND','VUV','WST','XAF',
'XCD','XOF','XPF','YER','ZAR','ZMW'
]),
// The label of the payment button in the Checkout form (e.g. “Subscribe”,
// “Pay {{amount}}”, etc.). If you include {{amount}}, it will be replaced
// by the provided amount. Otherwise, the amount will be appended to the
// end of your label.
panelLabel: React.PropTypes.string,
// Specify whether Checkout should validate the billing ZIP code (true or
// false)
zipCode: React.PropTypes.bool,
// Specify whether Checkout should collect the user's billing address
// (true or false). The default is false.
billingAddress: React.PropTypes.bool,
// Specify whether Checkout should collect the user's shipping address
// (true or false). The default is false.
shippingAddress: React.PropTypes.bool,
// Specify whether Checkout should validate the billing ZIP code (true or
// false). The default is false.
email: React.PropTypes.string,
// Specify whether to include the option to "Remember Me" for future
// purchases (true or false). The default is true.
allowRememberMe: React.PropTypes.bool,
// Specify whether to accept Bitcoin in Checkout. The default is false.
bitcoin: React.PropTypes.bool,
// Specify whether to accept Alipay ('auto', true, or false). The default
// is false.
alipay: React.PropTypes.oneOf(['auto', true, false]),
// Specify if you need reusable access to the customer's Alipay account
// (true or false). The default is false.
alipayReusable: React.PropTypes.bool,
// function() The callback to invoke when Checkout is opened (not supported
// in IE6 and IE7).
opened: React.PropTypes.func,
// function() The callback to invoke when Checkout is closed (not supported
// in IE6 and IE7).
closed: React.PropTypes.func
},
getInitialState: function() {
return {
scriptLoading: true,
scriptLoadError: false
};
},
// Used by scriptLoader mixin
getScriptURL: function() {
return 'https://checkout.stripe.com/checkout.js';
},
statics: {
stripeHandler: null,
scriptDidError: false
},
hasPendingClick: false,
onScriptLoaded: function() {
this.setState({scriptLoading: false});
// Initialize the Stripe handler on the first onScriptLoaded call.
// This handler is shared by all StripeButtons on the page.
if (!ReactStripeCheckout.stripeHandler) {
this.updateStripeHandler();
}
},
updateStripeHandler: function () {
ReactStripeCheckout.stripeHandler = StripeCheckout.configure(this.getConfig());
if (this.hasPendingClick) {
this.showStripeDialog();
}
},
componentDidUpdate: function () {
if (!this.state.scriptLoading)
this.updateStripeHandler();
},
showLoadingDialog: function() {
this.props.showLoadingDialog &&
this.props.showLoadingDialog.apply(this, arguments);
},
hideLoadingDialog: function() {
this.props.hideLoadingDialog &&
this.props.hideLoadingDialog.apply(this, arguments);
},
getConfig: function getConfig() {
var config = {};
config.key = this.props.stripeKey;
var options = [
'token', 'image', 'name', 'description', 'amount', 'locale',
'currency', 'panelLabel', 'zipCode', 'shippingAddress',
'billingAddress', 'email', 'allowRememberMe', 'bitcoin',
'alipay', 'alipayReusable', 'opened', 'closed'
];
for (var i = 0; i < options.length; i++) {
var key = options[i];
if (key in this.props) {
config[key] = this.props[key];
}
}
return config;
},
showStripeDialog: function() {
this.hideLoadingDialog();
ReactStripeCheckout.stripeHandler.open(this.getConfig());
},
onScriptError: function() {
this.hideLoadingDialog();
ReactStripeCheckout.scriptDidError = true;
this.props.onScriptError &&
this.props.onScriptError.apply(this);
},
onClick: function() {
if (ReactStripeCheckout.scriptDidError) {
console.log('failed to load script');
} else if (ReactStripeCheckout.stripeHandler) {
this.showStripeDialog();
} else {
this.showLoadingDialog();
this.hasPendingClick = true;
}
},
renderStripeButton: function() {
return (
<button className="stripe-checkout-button" onClick={this.onClick}>
<span className="inner-text">{this.props.label}</span>
</button>
);
},
render: function () {
return (
!this.props.children ? this.renderStripeButton() : (
<span {...this.props} onClick={this.onClick}>
{this.props.children}
</span>
)
);
}
});
module.exports = ReactStripeCheckout;