-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaymentStripeIntents.module
310 lines (249 loc) · 12.1 KB
/
PaymentStripeIntents.module
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
<?php
/*
* @author: BenByford
*/
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'init.php');
class PaymentStripeIntents extends PaymentModule {
public static function getModuleInfo() {
return array(
'title' => 'PaymentStripeIntents',
'version' => 001,
'summary' => 'Stripe Intents implementation that allows you to take payments from your site given Price Description, and Stripe account',
'href' => 'https://github.com/benbyford/PaymentStripeIntents',
'singular' => true,
'requires' => 'PaymentModule',
);
}
protected $name = "PaymentStripeIntents";
protected $submitButton = "Submit Payment";
public function init() {
$this->currency = $this->defaultCurrency;
}
public function getTitle() {
return $this->_("Credit Card (StripeIntents)");
}
public function setSubmitButton($text) {
$this->submitButton = $text;
}
protected function createIntent($amount, $currency, $description = ""){
\Stripe\Stripe::setApiKey($this->secretKey);
$description = $description != "" ? $description : "Charge from " . $this->seller;
$capturePayment = "automatic";
if((int)$this->captureCheckbox){
$capturePayment = "manual";
}
$email = $this->customer->email != null ? $this->customer->email : null;
$response = \Stripe\PaymentIntent::create([
"amount" => $amount,
"currency" => $currency,
"payment_method_types" => ["card"],
"capture_method" => $capturePayment,
"description" => $description,
"receipt_email" => $email
// "customer" => $customecurrencyrID
]);
return $response;
}
public function paymentCheckout($description = "", $custom_amount = 0, $custom_currency = "", $custom_returnUrl = "")
{
// set defaults
$amount = $this->getTotalAmount();
$currency = $this->currency;
$ajax = 0;
$imageUrl = $this->config->urls->siteModules . $this->name . '/img/OutlineDark/powered_by_stripe.svg';
// set return address
$returnUrl = $this->page->httpUrl; // default to current page address if none set in request or admin settings
$customerName = $this->customer->givenName;
if($this->customer->givenName && $this->customer->familyName){ $customerName = $this->customer->givenName . " " . $this->customer->familyName; }
// overwrite defaults if set
if( $custom_currency != "" ) $currency = $custom_currency;
$currency = strtolower($currency); // must be lower case for Stripe paymentRequest
if( $custom_amount != 0 ) $amount = $custom_amount;
if( (int)$this->ajax ){ $ajax = (int)$this->ajax; }
if( $this->returnUrl != "" ){ $returnUrl = $this->returnUrl; }
if( $custom_returnUrl != "" ){ $returnUrl = $custom_returnUrl; }
if( $this->processUrl != "" ){ $returnUrl = $this->processUrl; }
if( $this->imageUrl != "") { $imageUrl = $this->imageUrl; }
// get Stripe amount country code
$country = $this->countryCode;
//
// setup payment Intent
//
$intent = $this->createIntent($amount, $currency, $description);
// throw error if no stripe intent id present
if(!$intent->id) throw new WireException("No Stripe Intent ID present");
// save Intent in session
$this->saveIntentData($intent->id);
$formIntent = '<div id="stripe-intents-form">
<form action="'. $returnUrl .'" method="post" data-ajax="'. $ajax .'" data-amount="'. $amount .'" data-description="'. $description .'" data-currency="'. $currency .'" data-ajax="'. $ajax .'" data-country="'. $country .'" id="stripe-payment-form">
<!-- Stripe elements form -->
<div class="row">
<div class="field">
<label for="cardholder-name">Cardholder Name</label>
<input id="cardholder-name" name="name" type="text" required value="'. $customerName .'">
</div>
</div>
<div class="row">
<div class="field">
<label for="cardholder-name">Cardholder Email</label>
<input id="cardholder-email" name="email" type="text" required value="'. $this->customer->email .'">
</div>
</div>
<div class="card-details">
<label for="card-element">Credit or debit card</label>
<div id="card-element"></div>
<button id="card-button" data-public="'. $this->publicKey .'" data-secret="' . $intent->client_secret .'">'.$this->submitButton.'</button>
</div>
</form>
<!-- Stripe cross browser payments button -->
<div id="cross-browser-button">
<p class="align_center">or</p>
<div id="payment-request-button"></div>
</div>
<div id="card-errors" role="alert"></div>
<div class="stripe-logo">
<a href="https://stripe.com" target="_blank" rel="nofollow"><img src="'.$imageUrl.'"></a>
</div>
</div>';
// return Stripe form and intent id as array
return array( $formIntent, $intent->id );
}
public function paymentCompleted(){
$this->deleteIntentData();
}
//
// save intent info in the session
//
protected function saveIntentData($intentID = ""){
$this->session->set("stripe-intent-amount",$this->getTotalAmount());
$this->session->set("stripe-intent-currency",$this->currency);
$this->session->set("stripe-intent-intent-id", $intentID);
}
protected function deleteIntentData($intentID = ""){
$this->session->set("stripe-intent-amount","");
$this->session->set("stripe-intent-currency","");
$this->session->set("stripe-intent-intent-id","");
}
//
// intent utilities
//
public function cancelIntent($intentID){
$intent = $this->retrieveIntent($intentId);
$intent->cancel();
}
protected function retrieveIntent($intentID){
return \Stripe\PaymentIntent::retrieve($intentID);
}
public function capturePaymentFromIntent($intentId){
\Stripe\Stripe::setApiKey($this->secretKey);
$intent = $this->retrieveIntent($intentId);
$intent->capture();
return $intent;
}
//
// CSS and Scripts for form styling and display
//
public function getScripts($v = 0){
if($v == 0){
$v = "";
}else{
$v = "?" . $v;
}
$scripts = "<script defer>". $this->script_styling ."</script>";
$scripts .= "<script defer src='" . $this->config->urls->siteModules . $this->name . "/scripts/stripejs-init.js" . $v . "'></script>";
return $scripts;
}
public function getCSS(){
$css = "<style>". file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '/css/form-style.css', true) . "</style>";
return $css;
}
// render Stripe form
public function render() {
return "nothing here yet";
}
//
// Module admin fields
//
public static function getModuleConfigInputfields(array $data) {
$inputfields = new InputfieldWrapper();
// $field = wire('modules')->get('InputfieldText');
// $field->name = 'seller';
// $field->label = __("Seller name");
// $field->notes = __("Company or seller of the goods - this is shown on stripe payment form");
// if(isset($data['seller'])) $field->value = $data['seller'];
// $field->columnWidth = 50;
// $inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'secretKey';
$field->label = __("Secret key");
$field->notes = __("Your secret key: remember to change this to your live secret key in production. See your keys [here](https://dashboard.stripe.com/account)");
$field->columnWidth = 50;
if(isset($data['secretKey'])) $field->value = $data['secretKey'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'publicKey';
$field->label = __("Public key");
$field->notes = __("Your public key: remember to change this to your live public key in production. See your keys [here](https://dashboard.stripe.com/account)");
$field->columnWidth = 50;
if(isset($data['publicKey'])) $field->value = $data['publicKey'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'defaultCurrency';
$field->label = __("Default currency");
$field->notes = __("Use this currency by default (always possible to overwrite when using this module from API) Find currency codes listed [here](https://www.iso.org/iso-4217-currency-codes.html)");
$field->columnWidth = 50;
if(isset($data['defaultCurrency'])) $field->value = $data['defaultCurrency'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'countryCode';
$field->label = __("Stripe account country code");
$field->notes = __("E.g. AE, AT, AU, BE, BR, CA, CH, DE, DK, EE, ES, FI, FR, GB, GR, HK, IE, IN, IT, JP, LT, LU, LV, MX, MY, NL, NO, NZ, PH, PL, PT, RO, SE, SG, SI, SK, US");
$field->columnWidth = 50;
// default value to US and have in admin to show this is the case
if(isset($data['countryCode'])) $field->value = $data['countryCode'] ? $data['countryCode'] : "US";
$inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'returnUrl';
$field->label = __("Return Url");
$field->notes = __("Url the form will POST to on successful payment or card capture e.g. /cart/my-thank-you-page . Defaults to the same page.");
$field->columnWidth = 50;
if(isset($data['returnUrl'])) $field->value = $data['returnUrl'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldText');
$field->name = 'imageUrl';
$field->label = __("Url to logo");
$field->notes = __("Company logo or other image to show in Stripe payment form. Default: Stripe white outline logo");
$field->columnWidth = 50;
if(isset($data['imageUrl'])) $field->value = $data['imageUrl'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldCheckbox');
$field->name = 'ajax';
$field->label = __("Send form via ajax");
$field->notes = __("On complete transaction send AJAX request to form url with transaction data.");
$field->columnWidth = 50;
if(isset($data['ajax'])) {
$field->value = $data['ajax'];
$field->attr('checked', $data['ajax'] === 1 ? 'checked' : '' );
}
$inputfields->add($field);
$field = wire('modules')->get('InputfieldCheckbox');
$field->name = 'captureCheckbox';
$field->label = __("Capture payment or defer - check to defer payment capture");
$field->notes = __("If unchecked (default) capture payment (payment taken immediately), if checked then defer payment (will have to be captured on Stripe account or via API)");
$field->columnWidth = 50;
if(isset($data['captureCheckbox'])) {
$field->value = $data['captureCheckbox'];
$field->attr('checked', $data['captureCheckbox'] === 1 ? 'checked' : '' );
}
$inputfields->add($field);
$field = wire('modules')->get('InputfieldTextarea');
$field->name = 'script_styling';
$field->label = __("Form styling in JS");
$field->notes = __("Style your form using StripeJS Elements.
e.g. `var style = { base: { color: '#32325d' } }`
More examples can be found [here](https://stripe.com/docs/stripe-js/reference#element-options)");
if(isset($data['script_styling'])) $field->value = $data['script_styling'];
$inputfields->add($field);
return $inputfields;
}
}