-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommerce_stripe.module
executable file
·1108 lines (986 loc) · 43 KB
/
commerce_stripe.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* This module provides Stripe (http://stripe.com/) payment gateway integration
* to Commerce. Commerce Stripe offers a PCI-compliant way to process payments
* straight from you Commerce shop.
*/
define('STRIPE_DEFAULT_INTEGRATION', 'stripejs');
/**
* Implements hook_menu().
*/
function commerce_stripe_menu() {
$items = array();
// Add a menu item to stripe payment transactions that can be refunded.
$items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/commerce-stripe-refund'] = array(
'title' => 'Refund',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_stripe_refund_form', 3, 5),
'access callback' => 'commerce_stripe_return_access',
'access arguments' => array(3, 5),
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 1,
'file' => 'includes/commerce_stripe.admin.inc',
);
return $items;
}
/**
* Implements hook_libraries_info().
*/
function commerce_stripe_libraries_info() {
return array(
'stripe-php' => array(
'name' => 'Stripe API Client Library for PHP',
'vendor url' => 'https://stripe.com/',
'download url' => 'https://github.com/stripe/stripe-php',
'dependencies' => array(),
'version arguments' => array(
'file' => 'VERSION',
'pattern' => '/(\d+\.\d+\.\d+)/',
),
'files' => array(
'php' => array(
'lib/Stripe.php',
),
),
),
);
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_stripe_commerce_payment_method_info() {
$payment_methods = array();
// The payment method info depends on the stripe integration method (stripe.js
// or Stripe Checkout) the administrator has selected. We need to load
// the rules action to determine which integration method is selected.
$stripe_integration = STRIPE_DEFAULT_INTEGRATION;
$cardonfile = FALSE;
$payment_method_rule = rules_config_load('commerce_payment_commerce_stripe');
if ($payment_method_rule && $payment_method_rule->active) {
foreach ($payment_method_rule->actions() as $action) {
// Skip any actions that are not simple rules actions. (i.e. loops)
if (!($action instanceof RulesAction)) {
continue;
}
if (!empty($action->settings['payment_method']['method_id']) && $action->settings['payment_method']['method_id'] == 'commerce_stripe') {
// Default to Stripe.js if no integration_type has been chosen.
if (!empty($action->settings['payment_method']['settings']['integration_type'])) {
$stripe_integration = $action->settings['payment_method']['settings']['integration_type'];
}
$cardonfile = !empty($action->settings['payment_method']['settings']['cardonfile']) ? TRUE : FALSE;
break;
}
}
}
$payment_methods['commerce_stripe'] = array(
'title' => t('Stripe'),
'short_title' => t('Stripe'),
'display_title' => t('Credit card'),
'description' => t('Stripe payment gateway'),
'active' => FALSE,
'terminal' => FALSE,
'offsite' => FALSE,
);
// Set the cardonfile settings. We check that the administrator has enabled
// cardonfile functionality for commerce_stripe; if not, we do not add the
// cardonfile callbacks that would otherwise be called.
if ($cardonfile) {
// Allow charging and deleting saved cards for any Stripe integration method.
$payment_methods['commerce_stripe']['cardonfile'] = array(
'charge callback' => 'commerce_stripe_cardonfile_charge',
'delete callback' => 'commerce_stripe_cardonfile_delete',
);
// Allow creating and updating cards outside of the checkout process only
// when the Stripe integration method is stripejs.
if ($stripe_integration == 'stripejs') {
$payment_methods['commerce_stripe']['cardonfile'] += array(
'create form callback' => 'commerce_stripe_cardonfile_create_form',
'create callback' => 'commerce_stripe_cardonfile_create',
'update callback' => 'commerce_stripe_cardonfile_update',
);
}
}
return $payment_methods;
}
/**
* Access callback for processing returns.
*/
function commerce_stripe_return_access($order, $transaction) {
// Don't allow refunds on non-stripe transactions.
if ($transaction->payment_method != 'commerce_stripe') {
return FALSE;
}
// Don't allow refunds on fully refunded transactions.
if (!empty($transaction->data['stripe']['amount_refunded'])) {
if ($transaction->data['stripe']['amount_refunded'] >= $transaction->amount) {
return FALSE;
}
}
// Only allow refunds on original charge transactions.
if (!empty($transaction->data['stripe']['stripe_refund'])) {
return FALSE;
}
return commerce_payment_transaction_access('update', $transaction);
}
/**
* Payment method settings form.
*
* @param $settings
* Default settings provided from rules
*
* @return array
* Settings form array
*/
function commerce_stripe_settings_form($settings) {
$form = array();
$form['stripe_currency'] = array(
'#type' => 'select',
'#title' => t('Currency'),
'#options' => array(
'CAD' => t('CAD'),
'EUR' => t('EUR'),
'GBP' => t('GBP'),
'USD' => t('USD'),
'AUD' => t('AUD'),
'CHF' => t('CHF'),
),
'#description' => t('Select the currency that you are using.'),
'#default_value' => !empty($settings['stripe_currency']) ? $settings['stripe_currency'] : 'USD',
);
$form['secret_key'] = array(
'#type' => 'textfield',
'#title' => t('Secret Key'),
'#description' => t('Secret API Key. Get your key from https://stripe.com/'),
'#default_value' => !empty($settings['secret_key']) ? $settings['secret_key'] : '',
'#required' => TRUE,
);
$form['public_key'] = array(
'#type' => 'textfield',
'#title' => t('Publishable Key'),
'#description' => t('Publishable API Key. Get your key from https://stripe.com/'),
'#default_value' => !empty($settings['public_key']) ? $settings['public_key'] : '',
'#required' => TRUE,
);
$form['display_title'] = array(
'#type' => 'textfield',
'#title' => t('Payment method display title'),
'#description' => t('Payment method display title'),
'#default_value' => !empty($settings['display_title']) ? $settings['display_title'] : t('Stripe'),
);
$form['receipt_email'] = array(
'#type' => 'checkbox',
'#title' => t('Email receipts'),
'#description' => t('When selected, customers will receive email receipts from Stripe.'),
'#default_value' => isset($settings['receipt_email']) ? $settings['receipt_email'] : 0,
);
$form['integration_type'] = array(
'#type' => 'select',
'#title' => t('Integration type'),
'#description' => t('Choose Stripe integration method: Stripe.js makes it easy to collect credit card (and other similarly sensitive) details without having the information touch your server. Checkout is an embeddable iframe for desktop, tablet, and mobile devices.'),
'#options' => array('stripejs' => t('stripe.js'), 'checkout' => t('checkout')),
'#default_value' => !empty($settings['integration_type']) ? $settings['integration_type'] : STRIPE_DEFAULT_INTEGRATION,
);
// Stripe Checkout specific settings.
// @see: https://stripe.com/docs/checkout#integration-custom
$form['checkout_settings'] = array(
'#type' => 'fieldset',
'#title' => t('These settings are specific to "checkout" integration type.'),
'#states' => array(
'visible' => array(
':input[name$="[integration_type]"]' => array('value' => 'checkout'),
),
),
);
// Highly recommended checkout options:
// Name
$form['checkout_settings']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('The name of your company or website.'),
'#default_value' => isset($settings['checkout_settings']['name']) ? $settings['checkout_settings']['name'] : variable_get('site_name', ''),
);
// Description
$form['checkout_settings']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('A description of the product or service being purchased.'),
'#default_value' => isset($settings['checkout_settings']['description']) ? $settings['checkout_settings']['description'] : '',
);
// Image
$form['checkout_settings']['image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => FALSE,
'#upload_location' => 'public://commerce_stripe/checkout_images/',
'#default_value' => isset($settings['checkout_settings']['image']) ? $settings['checkout_settings']['image']['fid'] : '',
'#element_validate' => array('commerce_stripe_settings_form_image_validate'),
);
// Optional checkout options:
// panelLabel
$form['checkout_settings']['panelLabel'] = array(
'#type' => 'textfield',
'#title' => t('Payment button label'),
'#description' => t('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.'),
'#default_value' => isset($settings['checkout_settings']['panelLabel']) ? $settings['checkout_settings']['panelLabel'] : "Pay {{amount}}",
);
// zipCode
$form['checkout_settings']['zipCode'] = array(
'#type' => 'checkbox',
'#title' => t('ZIP code verification'),
'#description' => t('Specify whether Checkout should validate the billing ZIP code.'),
'#default_value' => isset($settings['checkout_settings']['zipCode']) ? $settings['checkout_settings']['zipCode'] : 0,
);
// allowRememberMe
$form['checkout_settings']['allowRememberMe'] = array(
'#type' => 'checkbox',
'#title' => t('Show "Remember Me" option'),
'#description' => t('Specify whether Checkout should allow the user to store their credit card for faster checkout.'),
'#default_value' => isset($settings['checkout_settings']['allowRememberMe']) ? $settings['checkout_settings']['allowRememberMe'] : 0,
);
// bitcoin
$form['checkout_settings']['bitcoin'] = array(
'#type' => 'checkbox',
'#title' => t('Accept Bitcoin'),
'#description' => t('When checked, Stripe Checkout will accept Bitcoin as payment.'). l(t('Must be enabled in your Stripe account.'), 'https://dashboard.stripe.com/account/bitcoin/enable'),
'#default_value' => isset($settings['checkout_settings']['bitcoin']) ? $settings['checkout_settings']['bitcoin'] : 0,
);
// billingAddress (Undocumented option)
$form['checkout_settings']['billingAddress'] = array(
'#type' => 'checkbox',
'#title' => t('Billing address'),
'#description' => t('Specify whether to enable billing address collection in Checkout.'),
'#default_value' => isset($settings['checkout_settings']['billingAddress']) ? $settings['checkout_settings']['billingAddress'] : 0,
);
// shippingAddress (Undocumented option)
$form['checkout_settings']['shippingAddress'] = array(
'#type' => 'checkbox',
'#title' => t('Shipping address'),
'#description' => t('Specify whether to enable shipping address collection in Checkout.'),
'#default_value' => isset($settings['checkout_settings']['shippingAddress']) ? $settings['checkout_settings']['shippingAddress'] : 0,
);
if (module_exists('commerce_cardonfile')) {
$form['cardonfile'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Card on File functionality.'),
'#default_value' => isset($settings['cardonfile']) ? $settings['cardonfile'] : 0,
);
}
else {
$form['cardonfile'] = array(
'#type' => 'markup',
'#markup' => t('To enable Card on File funcitionality download and install the Card on File module.'),
);
}
return $form;
}
/**
* Custom validation callback for image field of stripe settings form.
* Uploaded image file is temporary by default, it'll be removed during the file garbage
* collection process. Change its status to FILE_STATUS_PERMANENT.
*
* @param array $element
* @param array $form_state
* @param array $form
*/
function commerce_stripe_settings_form_image_validate($element, &$form_state, $form) {
if (isset($element['fid']['#value'])) {
$file = file_load($element['fid']['#value']);
if (is_object($file) && $file->status !== FILE_STATUS_PERMANENT) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}
}
function _commerce_stripe_credit_card_form() {
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
$credit_card_fields = array(
'owner' => '',
'number' => '',
'exp_month' => '',
'exp_year' => '',
'code' => '',
);
$form = commerce_payment_credit_card_form($credit_card_fields);
// Add a css class so that we can easily identify Stripe related input fields
// Do not require the fields
//
// Remove "name" attributes from Stripe related input elements to
// prevent card data to be sent to Drupal server
// (see https://stripe.com/docs/tutorials/forms)
foreach (array_keys($credit_card_fields) as $key) {
$credit_card_field = &$form['credit_card'][$key];
$credit_card_field['#attributes']['class'][] = 'stripe';
$credit_card_field['#required'] = FALSE;
$credit_card_field['#post_render'][] = '_commerce_stripe_credit_card_field_remove_name';
}
return $form;
}
/**
* Payment method callback: checkout form.
*/
function commerce_stripe_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
global $user;
$integration_type = !empty($payment_method['settings']['integration_type']) ? $payment_method['settings']['integration_type'] : STRIPE_DEFAULT_INTEGRATION;
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$field = field_info_field('commerce_customer_address');
$instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', 'billing');
$available_countries = NULL;
if (isset($form_state['input']['country'])) {
$available_countries = array($form_state['input']['country'] => NULL);
}
// Attempt to load the billing address from the order data.
$billing_address = addressfield_default_values($field, $instance, array($available_countries));
if (!empty($order->commerce_customer_billing)) {
if (!empty($order_wrapper->commerce_customer_billing->commerce_customer_address)) {
$billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
}
}
// Pass the billing address values to javacript so they can be included in
// the token request to Stripe.
$address = array(
'address_line1' => !empty($billing_address['thoroughfare']) ? $billing_address['thoroughfare'] : '',
'address_line2' => !empty($billing_address['premise']) ? $billing_address['premise'] : '',
'address_city' => !empty($billing_address['locality']) ? $billing_address['locality'] : '',
'address_state' => !empty($billing_address['administrative_area']) ? $billing_address['administrative_area'] : '',
'address_zip' => !empty($billing_address['postal_code']) ? $billing_address['postal_code'] : '',
'address_country' => !empty($billing_address['country']) ? $billing_address['country'] : '',
);
drupal_add_js(array('commerce_stripe_address' => $address), array('type' => 'setting'));
// Differentiate form elements based on stripe integration type.
if ($integration_type == 'stripejs') {
$form = _commerce_stripe_credit_card_form();
// Include the stripe.js from stripe.com.
drupal_add_js('https://js.stripe.com/v2/', 'external');
}
elseif ($integration_type == 'checkout') {
$form = array();
// Add pay button.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_total = $order_wrapper->commerce_order_total->value();
// Add Checkout settings.
$checkout_settings = $payment_method['settings']['checkout_settings'];
// Convert JS settings to Booleans before adding them to the page.
$checkout_settings = array_map(function($value) {
if ($value === 0 OR $value === 1) {
$value = (bool) $value;
}
return $value;
}, $checkout_settings);
// Get the image file if one is set.
if (isset($checkout_settings['image']['fid'])) {
$image = file_load($checkout_settings['image']['fid']);
if (is_object($image)) {
$checkout_settings['image'] = file_create_url($image->uri);
}
else {
// Empty image setting will cause a broken image to be displayed in checkout
// iframe.
unset($checkout_settings['image']);
}
}
$checkout_settings += array(
'currency' => $payment_method['settings']['stripe_currency'],
'email' => $order->mail,
'amount' => $order_total['amount'],
);
drupal_add_js(array('stripe' => array('checkout' => $checkout_settings)), 'setting');
// Add external checkout.js library.
drupal_add_js('https://checkout.stripe.com/checkout.js', 'external');
}
// Add stripe token field. This field is a container for token received from
// Stripe API.
$form['stripe_token'] = array(
'#type' => 'hidden',
'#attributes' => array('id' => 'stripe_token'),
'#default_value' => !empty($pane_values['stripe_token']) ? $pane_values['stripe_token'] : '',
);
// Set our key to settings array.
drupal_add_js(
array(
'stripe' => array(
'publicKey' => $payment_method['settings']['public_key'],
'integration_type' => $integration_type,
),
),
'setting'
);
// Load commerce_stripe.js.
$form['#attached']['js'] = array(
drupal_get_path('module', 'commerce_stripe') . '/commerce_stripe.js',
);
// To display validation errors.
$form['errors'] = array(
'#type' => 'markup',
'#markup' => '<div class="payment-errors"></div>',
);
return $form;
}
function _commerce_stripe_credit_card_field_remove_name($content, $element) {
$name_pattern = '/\sname\s*=\s*[\'"]?' . preg_quote($element['#name']) . '[\'"]?/';
return preg_replace($name_pattern, '', $content);
}
/**
* Payment method callback: checkout form submission.
*/
function commerce_stripe_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
// If instructed to do so, try using the specified card on file.
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) &&
!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
$card_data = commerce_cardonfile_load($pane_values['cardonfile']);
if (empty($card_data) || $card_data->status == 0) {
drupal_set_message(t('The requested card on file is no longer valid.'), 'error');
return FALSE;
}
return commerce_stripe_cardonfile_charge($payment_method, $card_data, $order, $charge);
}
// The card is new. Either charge and forget, or charge and save.
if (!commerce_stripe_load_library()) {
drupal_set_message(t('Error making the payment. Please contact shop admin to proceed.'), 'error');
return FALSE;
}
// Begin assembling charge parameters.
Stripe::setApiKey($payment_method['settings']['secret_key']);
// Build a default description and offer modules the possibility to alter it.
$description = t('Order Number: @order_number', array('@order_number' => $order->order_number));
$currency_code = $payment_method['settings']['stripe_currency'];
if(isset($charge['currency_code'])){
$currency_code = $charge['currency_code'];
}
$c = array(
'amount' => $charge['amount'],
'currency' => $currency_code,
'card' => $pane_values['stripe_token'],
'description' => $description,
);
// Specify that we want to send a receipt email if we are configured to do so.
if (!empty($payment_method['settings']['receipt_email'])) {
$c['receipt_email'] = $order->mail;
}
// The metadata could be added via the alter below but for compatibility
// reasons it may stay.
commerce_stripe_add_metadata($c, $order);
// Let modules alter the charge object to add attributes.
drupal_alter('commerce_stripe_order_charge', $c, $order);
// To later store the card with all required fields, carry out necessary steps before making the charge request.
if (module_exists('commerce_cardonfile') && !empty($payment_method['settings']['cardonfile']) &&
!empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
$card = _commerce_stripe_create_card($pane_values['stripe_token'], $order->uid, $payment_method);
// If the card is not declined or otherwise is error-free, we can save it.
if ($card && !empty($card->id)) {
$stripe_card_id = $card->id;
$stripe_customer_id = $card->customer;
$c['card'] = $stripe_card_id;
$c['customer'] = $stripe_customer_id;
$save_card = TRUE;
}
}
$transaction = commerce_payment_transaction_new('commerce_stripe', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $currency_code;
/*
* save the transaction as pending. this will cause an exception to be thrown
* if the transaction cannot be saved. this prevents the scenario where it
* can go all the way through the try/catch below with success in stripe but
* failing to ever save the transaction. saving the transaction here also acts as
* an early catch to prevent the stripe charge from going through if the Drupal
* side will be unable to save it for some reason.
*/
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
if (!_commerce_stripe_commerce_payment_transaction_save($transaction)) {
return FALSE;
}
try {
// Stripe does not appreciate $0 transfers.
if ($charge['amount'] > 0) {
$response = Stripe_Charge::create($c);
$transaction->remote_id = $response->id;
$transaction->payload[REQUEST_TIME] = $response->__toJSON();
$transaction->message = t('Payment completed successfully.');
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
_commerce_stripe_commerce_payment_transaction_save($transaction);
if (property_exists($response, 'card')) {
$card_response = $response->card;
}
else {
$card_response = $response->source;
}
}
// If the total is $0 we just put the card on file.
else {
$card_response = $card;
}
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card. Please enter your information again or try a different card.'), 'error');
drupal_set_message(check_plain($e->getMessage()), 'error');
watchdog('commerce_stripe', 'Following error received when processing card @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
$transaction->remote_id = $e->getHttpStatus();
$transaction->payload[REQUEST_TIME] = $e->jsonBody;
$transaction->message = t('Card processing error: @stripe_error', array('@stripe_error' => $e->getMessage()));
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
_commerce_stripe_commerce_payment_transaction_save($transaction);
return FALSE;
}
// If so instructed by the customer, save the card.
if (!empty($save_card)) {
_commerce_stripe_save_cardonfile($card_response, $order->uid, $payment_method, $pane_values['cardonfile_instance_default']);
}
}
/**
* attempt to save the transaction and set messages if unsuccessful
*/
function _commerce_stripe_commerce_payment_transaction_save($transaction) {
if (!commerce_payment_transaction_save($transaction)) {
drupal_set_message(t('Our site is currently unable to process your card. Please contact the site administrator to complete your transaction'), 'error');
watchdog('commerce_stripe', 'commerce_payment_transaction_save returned false in saving a stripe transaction for order_id @order_id.', array('@order_id' => $transaction->order_id), WATCHDOG_ERROR);
return FALSE;
}
else {
return TRUE;
}
}
function _commerce_stripe_create_card($stripe_token, $uid, $payment_method) {
if (!commerce_stripe_load_library()) {
return FALSE;
}
Stripe::setApiKey($payment_method['settings']['secret_key']);
// If there is no existing customer id, use the Stripe form token to create one.
$stripe_customer_id = commerce_stripe_customer_id($uid, $payment_method['instance_id']);
if (!$stripe_customer_id) {
$account = user_load($uid);
try {
$customer = Stripe_Customer::create(array(
'card' => $stripe_token,
'email' => $account->mail,
));
if (property_exists($customer, 'cards')) {
foreach ($customer->cards->data as $card) {
if ($card->id == $customer->default_card) {
return $card;
}
}
}
else {
foreach ($customer->sources->data as $card) {
if ($card->object == 'card' && $card->id == $customer->default_source) {
return $card;
}
}
}
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card: %error. Please enter your information again or try a different card.', array('%error' => $e->getMessage())), 'error');
watchdog('commerce_stripe', 'Following error received when creating Stripe customer: @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
return FALSE;
}
}
// If the customer id already existed, use the Stripe form token to create the new card.
else {
try {
$customer = Stripe_Customer::retrieve($stripe_customer_id);
if (property_exists($customer, 'cards')) {
$card = $customer->cards->create(array('card' => $stripe_token));
}
else {
$card = $customer->sources->create(array('card' => $stripe_token));
}
return $card;
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card: %error. Please enter your information again or try a different card.', array('%error' => $e->getMessage())), 'error');
watchdog('commerce_stripe', 'Following error received when adding a card to customer: @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
return FALSE;
}
}
}
function _commerce_stripe_save_cardonfile($card, $uid, $payment_method, $set_default) {
// Store the Stripe customer and card ids in the remote id field of {commerce_cardonfile} table
$remote_id = (string) $card->customer . '|' . (string) $card->id;
// Populate and save the card
$card_data = commerce_cardonfile_new();
$card_data->uid = $uid;
$card_data->payment_method = $payment_method['method_id'];
$card_data->instance_id = $payment_method['instance_id'];
$card_data->remote_id = $remote_id;
$card_data->card_type = $card->type;
$card_data->card_name = $card->name;
$card_data->card_number = $card->last4;
$card_data->card_exp_month = $card->exp_month;
$card_data->card_exp_year = $card->exp_year;
$card_data->status = 1;
$card_data->instance_default = 0;
if (property_exists($card, 'type')) {
$card_data->card_type = $card->type;
}
else {
$card_data->card_type = $card->brand;
}
commerce_cardonfile_save($card_data);
if ($set_default) {
commerce_cardonfile_set_default_card($card_data->card_id);
}
watchdog('commerce_stripe', 'Stripe Customer Profile @profile_id created and saved to user @uid.', array('@profile_id' => (string) $card->customer, '@uid' => $uid));
}
/**
* Implements hook_commerce_payment_method_info_alter().
*
* Displays a warning if Stripe private and public keys are not set and the
* user has permission to administer payment methods.
*/
function commerce_stripe_commerce_payment_method_info_alter(&$payment_methods) {
if (isset($payment_methods['commerce_stripe'])) {
// Just return if they don't have permission to see these errors.
if (!user_access('administer payment methods')) {
return;
}
$found_errors = FALSE;
$settings = _commerce_stripe_load_settings();
// If secret_key or public_key is not set.
if (empty($settings['secret_key']) || empty($settings['public_key'])) {
$found_errors = TRUE;
drupal_set_message(t('Stripe secret and public key are required in order to use Stripe payment method. See README.txt for instructions.'), 'warning');
}
// If integration_type is not set.
if (empty($settings['integration_type'])) {
$found_errors = TRUE;
drupal_set_message(t('The Stripe payment method "Integration type" is not set. Stripe.js will be used by default.'), 'warning');
}
// If they need to configure anything, be nice and give them the link.
if ($found_errors) {
$link = l(t('configured here'), 'admin/commerce/config/payment-methods');
drupal_set_message(t('Settings required for the Stripe payment method can be !link.', array('!link' => $link)), 'warning');
}
}
}
function _commerce_stripe_load_settings($name = NULL) {
static $settings = array();
if (!empty($settings)) {
return $settings;
}
if (commerce_payment_method_load('commerce_stripe') && rules_config_load('commerce_payment_commerce_stripe')) {
$commerce_stripe_payment_method = commerce_payment_method_instance_load('commerce_stripe|commerce_payment_commerce_stripe');
}
if (isset($name) && rules_config_load('commerce_payment_commerce_stripe')) {
$commerce_stripe_payment_method = commerce_payment_method_instance_load('commerce_stripe|commerce_payment_commerce_stripe');
}
if (isset($commerce_stripe_payment_method)) {
$settings = $commerce_stripe_payment_method['settings'];
}
return $settings;
}
function _commerce_stripe_load_setting($name, $default_value = NULL) {
$settings = _commerce_stripe_load_settings($name);
return isset($settings[$name]) ? $settings[$name] : $default_value;
}
/**
* Card on file callback: background charge payment
* TODO: implement proper return codes per commerce payment
*/
function commerce_stripe_cardonfile_charge($payment_method, $card_data, $order, $charge = NULL) {
if (!commerce_stripe_load_library()) {
return FALSE;
}
// Fetch the customer id and card id from $card_data->remote_id
list($customer_id, $card_id) = explode('|', $card_data->remote_id);
$currency_code = $payment_method['settings']['stripe_currency'];
if(isset($charge['currency_code'])){
$currency_code = $charge['currency_code'];
}
// Assemble charge parameters.
Stripe::setApiKey($payment_method['settings']['secret_key']);
// Build a default description and offer modules the possibility to alter it.
$description = t('Order Number: @order_number', array('@order_number' => $order->order_number));
$c = array(
'amount' => $charge['amount'],
'currency' => $currency_code,
'customer' => $customer_id,
'card' => $card_id,
'description' => $description,
);
commerce_stripe_add_metadata($c, $order);
// Let modules alter the charge object to add attributes.
drupal_alter('commerce_stripe_order_charge', $c, $order);
$transaction = commerce_payment_transaction_new('commerce_stripe', $order->order_id);
$transaction->instance_id = $payment_method['instance_id'];
$transaction->amount = $charge['amount'];
$transaction->currency_code = $currency_code;
// save as pending to ensure the drupal side of things is working before we attempt the stripe api call
$transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
if (!_commerce_stripe_commerce_payment_transaction_save($transaction)) {
return FALSE;
}
try {
$lock = __FUNCTION__ .'_'. $order->order_id;
if (lock_acquire($lock) && ($charge['amount'] > 0)) {
$response = Stripe_Charge::create($c);
$transaction->remote_id = $response->id;
$transaction->payload[REQUEST_TIME] = $response->__toJSON();
$transaction->message = t('Payment completed successfully.');
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
_commerce_stripe_commerce_payment_transaction_save($transaction);
lock_release($lock);
return TRUE;
}
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card. Please enter your information again or try a different card.'), 'error');
drupal_set_message(check_plain($e->getMessage()), 'error');
watchdog('commerce_stripe', 'Following error received when processing card @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
$transaction->remote_id = $e->getHttpStatus();
$transaction->payload[REQUEST_TIME] = $e->jsonBody;
$transaction->message = t('Card processing error: @stripe_error', array('@stripe_error' => $e->getMessage()));
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
_commerce_stripe_commerce_payment_transaction_save($transaction);
lock_release($lock);
return FALSE;
}
}
/**
* Card on file callback: create form
*/
function commerce_stripe_cardonfile_create_form($form, &$form_state, $op, $card_data) {
// Pass along information to the validate and submit handlers.
$form_state['card_data'] = $card_data;
$form_state['op'] = $op;
// Add stripe token field. This field is a container for token received from
// Stripe API's response handler.
$form['stripe_token'] = array(
'#type' => 'hidden',
'#attributes' => array('id' => 'stripe_token'),
'#default_value' => !empty($form_state['values']['stripe_token']) ? $form_state['values']['stripe_token'] : '',
);
// Set our key to settings array.
drupal_add_js(array('stripe' => array('publicKey' => _commerce_stripe_load_setting('public_key'))), 'setting');
// Include the stripe.js from stripe.com.
drupal_add_js('https://js.stripe.com/v2/', 'external');
// Load commerce_stripe.js.
$form['#attached']['js'] = array(
drupal_get_path('module', 'commerce_stripe') . '/commerce_stripe.js',
);
$form['errors'] = array(
'#markup' => '<div id="card-errors"></div>'
);
$form += _commerce_stripe_credit_card_form();
$payment_method = commerce_payment_method_instance_load($card_data->instance_id);
$form['credit_card']['cardonfile_instance_default'] = array(
'#type' => 'checkbox',
'#title' => t('Use as default card for payments with %method', array('%method' => $payment_method['display_title'])),
'#default_value' => FALSE,
);
// Create a billing profile object and add the address form.
$profile = commerce_customer_profile_new('billing', $form_state['card']->uid);
$form_state['commerce_customer_profile'] = $profile;
$form['commerce_customer_profile'] = array();
field_attach_form('commerce_customer_profile', $profile, $form['commerce_customer_profile'], $form_state);
$form['commerce_customer_profile']['#weight'] = -1;
// Add a validation callback so that we can call field_attach functions.
$form['#validate'][] = 'commerce_stripe_cardonfile_create_validate';
commerce_stripe_set_addressfield_class_names($form['address']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add card')
);
return $form;
}
/**
* Validation callback for card on file creation.
*/
function commerce_stripe_cardonfile_create_validate($form, &$form_state) {
$profile = $form_state['commerce_customer_profile'];
field_attach_form_validate('commerce_customer_profile', $profile, $form['commerce_customer_profile'], $form_state);
}
function commerce_stripe_cardonfile_create_form_submit($form, &$form_state) {
$card_data = $form_state['card_data'];
$payment_method = commerce_payment_method_instance_load($card_data->instance_id);
commerce_stripe_cardonfile_create($form, $form_state, $payment_method, $card_data);
$form_state['redirect'] = 'user/' . $card_data->uid . '/cards';
}
/**
* Card on file callback: create
*/
function commerce_stripe_cardonfile_create($form, &$form_state, $payment_method, $card_data) {
$card = _commerce_stripe_create_card($form_state['values']['stripe_token'], $card_data->uid, $payment_method);
if (!$card) {
return;
}
_commerce_stripe_save_cardonfile($card, $card_data->uid, $payment_method, $form_state['values']['credit_card']['cardonfile_instance_default']);
}
/**
* Card on file callback: updates the associated customer payment profile.
*/
function commerce_stripe_cardonfile_update($form, &$form_state, $payment_method, $card_data) {
if (!commerce_stripe_load_library()) {
return FALSE;
}
// Fetch the customer id and card id from $card_data->remote_id
list($customer_id, $card_id) = explode('|', $card_data->remote_id);
Stripe::setApiKey($payment_method['settings']['secret_key']);
try {
$customer = Stripe_Customer::retrieve($customer_id);
if (property_exists($customer, 'cards')) {
$card = $customer->cards->retrieve($card_id);
}
else {
$card = $customer->sources->retrieve($card_id);
}
$card->exp_month = $form_state['values']['credit_card']['exp_month'];
$card->exp_year = $form_state['values']['credit_card']['exp_year'];
$card->save();
return TRUE;
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card: %error. Please enter your information again or try a different card.', array('%error' => $e->getMessage())), 'error');
watchdog('commerce_stripe', 'Following error received when updating card @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
return FALSE;
}
}
/**
* Card on file callback: deletes the associated customer payment profile.
*/
function commerce_stripe_cardonfile_delete($form, &$form_state, $payment_method, $card_data) {
if (!commerce_stripe_load_library()) {
return FALSE;
}
// Fetch the customer id and card id from $card_data->remote_id
list($customer_id, $card_id) = explode('|', $card_data->remote_id);
Stripe::setApiKey($payment_method['settings']['secret_key']);
try {
$customer = Stripe_Customer::retrieve($customer_id);
if (property_exists($customer, 'card')) {
$customer->cards->retrieve($card_id)->delete();
}
else {
$customer->sources->retrieve($card_id)->delete();
}
return TRUE;
}
catch (Exception $e) {
drupal_set_message(t('We received the following error processing your card: %error. Please enter your information again or try a different card.', array('%error' => $e->getMessage())), 'error');
watchdog('commerce_stripe', 'Following error received when deleting card @stripe_error.', array('@stripe_error' => $e->getMessage()), WATCHDOG_NOTICE);
return FALSE;
}
}
/**
* Brings the stripe php client library into scope
*/
function commerce_stripe_load_library() {
$library = libraries_load('stripe-php');
if (!$library || empty($library['loaded'])) {
watchdog('commerce_stripe', 'Failure to load Stripe API PHP Client Library.', array(), WATCHDOG_CRITICAL);
return FALSE;
}
else {
$minimum_version = '1.17.1';
$maximum_version = '1.18.0';
$message = "Commerce Stripe is currently tested with stripe-php library versions @minimum_version through @maximum_version. You are using version @installed_version, and you should @upgrade_or_downgrade.";
//check that it's not lower than the minimum required version
if (version_compare($library['version'], $minimum_version, '<')) {
$variables = array('@minimum_version' => $minimum_version, '@maximum_version' => $maximum_version, '@installed_version' => $library['version'], '@upgrade_or_downgrade' => 'upgrade');
watchdog('commerce_stripe', $message, $variables, WATCHDOG_WARNING);
return FALSE;