-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_cardonfile.module
2526 lines (2185 loc) · 77.6 KB
/
commerce_cardonfile.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
* Supports card on file functionality for credit card payment methods by
* associating card data reference IDs from payment gateways with user accounts.
*/
// -----------------------------------------------------------------------
// Constants
// Card on file process status codes
define('COMMERCE_COF_PROCESS_CODE_INSUFFICIENT_DATA', 'insufficient');
define('COMMERCE_COF_PROCESS_CODE_CARD_NA', 'card_na');
define('COMMERCE_COF_PROCESS_CODE_CARD_EXPIRED', 'card_expired');
define('COMMERCE_COF_PROCESS_CODE_CARD_NOT_CHARGEABLE', 'card_not_chargeable');
define('COMMERCE_COF_PROCESS_CODE_METHOD_EMPTY', 'no_method');
define('COMMERCE_COF_PROCESS_CODE_METHOD_NOT_CAPABLE', 'method_not_capable');
define('COMMERCE_COF_PROCESS_CODE_METHOD_FAILURE', 'method_failure');
define('COMMERCE_COF_PROCESS_CODE_METHOD_SUCCESS', 'method_success');
// -----------------------------------------------------------------------
// Includes
require_once dirname(__FILE__) . '/commerce_cardonfile.field.inc';
/**
* Implements hook_menu().
*/
function commerce_cardonfile_menu() {
$items = array();
$items['admin/commerce/config/cardonfile'] = array(
'title' => 'Card on file settings',
'description' => 'Configure your card on file settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_cardonfile_settings_form'),
'access arguments' => array('configure cardonfile'),
'file' => 'includes/commerce_cardonfile.admin.inc',
);
$items['user/%user/stored-payment-methods/%commerce_cardonfile_data'] = array(
'title' => 'Credit card',
'page callback' => 'commerce_cardonfile_redirect_to_user',
'page arguments' => array(1),
'access callback' => 'commerce_cardonfile_data_access',
'access arguments' => array('view', 3),
);
$items['user/%user/stored-payment-methods/%commerce_cardonfile_data/update'] = array(
'title' => 'Update',
'description' => 'Update a stored payment method.',
'page callback' => 'commerce_cardonfile_update_page',
'page arguments' => array(3, 1),
'access callback' => 'commerce_cardonfile_data_access',
'access arguments' => array('edit', 3, 1),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 0,
'file' => 'includes/commerce_cardonfile.pages.inc',
);
$items['user/%user/stored-payment-methods/%commerce_cardonfile_data/delete'] = array(
'title' => 'Delete',
'description' => 'Delete a stored payment method.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_cardonfile_delete_form', 3),
'access callback' => 'commerce_cardonfile_data_access',
'access arguments' => array('delete', 3, 1),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 5,
'file' => 'includes/commerce_cardonfile.pages.inc',
);
$items['user/%user/stored-payment-methods/%commerce_cardonfile_data/%/toggle-default'] = array(
'title' => 'Set default',
'title callback' => 'commerce_cardonfile_data_toggle_default_card_callback_title',
'title arguments' => array(3),
'description' => 'Toggle the default flag for a stored payment method.',
'page callback' => 'commerce_cardonfile_data_toggle_default_card_callback',
'page arguments' => array(3, 1, 4),
'access callback' => 'commerce_cardonfile_data_toggle_default_card_callback_access',
'access arguments' => array(3, 1, 4),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 20,
);
// create paths for each payment method
$create_implements = commerce_cardonfile_payment_method_implements('create callback');
foreach ($create_implements as $method_id => $method_function) {
$payment_method_instances = _commerce_cardonfile_payment_method_instances($method_id, TRUE);
if (empty($payment_method_instances)) {
continue;
}
foreach ($payment_method_instances as $instance_id => $payment_method) {
$new_card_data = commerce_cardonfile_data_new(array(
'instance_id' => $instance_id,
'payment_method' => $payment_method['method_id'],
));
$items['user/%user/stored-payment-methods/add/' . drupal_hash_base64($instance_id)] = array(
'title' => 'Add a !name',
'title arguments' => array('!name' => $payment_method['display_title']),
'page callback' => 'commerce_cardonfile_create_page',
'page arguments' => array($new_card_data, 1),
'access callback' => 'commerce_cardonfile_data_access',
'access arguments' => array('create', $new_card_data, 1),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/commerce_cardonfile.pages.inc',
);
}
}
return $items;
}
/**
* Redirects to the user's stored payment methods page.
*/
function commerce_cardonfile_redirect_to_user($account) {
drupal_goto('user/' . $account->uid . '/stored-payment-methods');
}
/**
* Implements hook_permission().
*/
function commerce_cardonfile_permission() {
return array(
'configure cardonfile' => array(
'title' => t('Configure Card on File'),
'description' => t('Update the Card on File configuration in the Store back end.'),
'restrict access' => TRUE,
),
'administer card data' => array(
'title' => t('Administer card data'),
'description' => t("Access and update any user's stored card data."),
'restrict access' => TRUE,
),
'view any card data' => array(
'title' => t('View any card data'),
'restrict access' => TRUE,
),
'view own card data' => array(
'title' => t('View own card data'),
),
'create any card data' => array(
'title' => t('Create any card data'),
'restrict access' => TRUE,
),
'create own card data' => array(
'title' => t('Create own card data'),
),
'edit any card data' => array(
'title' => t('Edit any card data'),
'restrict access' => TRUE,
),
'edit own card data' => array(
'title' => t('Edit own card data'),
),
'delete any card data' => array(
'title' => t('Delete any card data'),
'restrict access' => TRUE,
),
'delete own card data' => array(
'title' => t('Delete own card data'),
),
);
}
/**
* Implements hook_hook_info().
*/
function commerce_cardonfile_hook_info() {
$base_info = array(
'group' => 'commerce',
);
$hooks = array(
'commerce_cardonfile_data_insert' => $base_info,
'commerce_cardonfile_data_update' => $base_info,
'commerce_cardonfile_data_disable' => $base_info,
'commerce_cardonfile_data_delete' => $base_info,
'commerce_cardonfile_data_can_delete' => $base_info,
'commerce_cardonfile_order_chargeable_cards' => $base_info,
'commerce_cardonfile_checkout_pane_form_alter' => $base_info,
);
return $hooks;
}
/**
* Implements hook_views_api().
*/
function commerce_cardonfile_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_cardonfile') . '/includes/views',
);
}
/**
* Implements hook_enable().
*/
function commerce_cardonfile_enable() {
commerce_cardonfile_configure_order_type();
}
/**
* Implements hook_modules_enabled().
*
* - Ensure order fields for newly enabled modules
*/
function commerce_cardonfile_modules_enabled($modules) {
$possible_modules = module_implements('entity_info_alter');
$enabled_possible_modules = array_intersect($modules, $possible_modules);
// configure fields if any enabled module could register a new order bundle
if (!empty($enabled_possible_modules)) {
entity_info_cache_clear();
commerce_cardonfile_configure_order_type();
}
}
// -----------------------------------------------------------------------
// Theme
/**
* Implements hook_theme().
*/
function commerce_cardonfile_theme() {
return array(
'card_data_overview' => array(
'variables' => array('card_data' => array()),
),
'commerce_cardonfile_data_label' => array(
'variables' => array('card_data' => array()),
),
);
}
/**
* Themes a display of stored card data.
*
* @param $variables
* An array of theme variables including:
* - card_data: a data array for the stored card on file
*/
function theme_card_data_overview($variables) {
drupal_add_css(drupal_get_path('module', 'commerce_cardonfile') . '/theme/commerce_cardonfile.css');
$card_data = $variables['card_data'];
// Load the credit card helper functions from the Payment module.
commerce_cardonfile_load_credit_card_helpers();
$card_types = commerce_payment_credit_card_types();
// Extract the name of the card type if possible.
$card_type = t('Credit card');
if (!empty($card_types[$card_data['card_type']])) {
$card_type = $card_types[$card_data['card_type']];
}
// Build an array of data lines to include in the overview.
$lines = array(
t('Type:') => $card_type,
t('Cardholder name:') => check_plain($card_data['card_name']),
t('Number (last 4):') => '******' . check_plain($card_data['card_number']),
t('Expiration date:') => check_plain($card_data['card_exp_month'] . '/' . $card_data['card_exp_year']),
t('Stored status:') => !empty($card_data['status']) ? t('Active') : t('Disabled'),
);
$output = '';
foreach ($lines as $label => $value) {
// Only add a line if it has a value.
if (!empty($value)) {
$output .= '<div class="commerce-card-data-line"><span class="label">' . $label . '</span> ' . $value . '</div>';
}
}
return '<div class="commerce-card-data">' . $output . '</div>';
}
// -----------------------------------------------------------------------
// Field setup
/**
* Ensures the card on file field is present on a specific order bundle.
*
* @param $bundle
* The order bundle to configure. If not provided, then all bundles are
* configured.
*/
function commerce_cardonfile_configure_order_type($bundle = NULL) {
$bundles = array();
if (!empty($bundle)) {
$bundles[] = $bundle;
}
elseif ($bundle_names = commerce_order_type_get_name()) {
$bundles = array_keys($bundle_names);
}
// Exit if no types to configure
if (empty($bundles)) {
return;
}
// intialize field variables
$field_type = 'commerce_cardonfile_reference';
$field_name = 'commerce_cardonfile';
// If a field type we know should exist isn't found, clear the Field cache.
if (!field_info_field_types($field_type)) {
field_cache_clear();
}
// get field type information for defaults
$field_type_info = field_info_field_types($field_type);
// determine defaults
$default_widget = !empty($field_type_info['default_widget']) ? $field_type_info['default_widget'] : 'options_select';
$default_formatter = !empty($field_type_info['default_formatter']) ? $field_type_info['default_formatter'] : 'commerce_cardonfile_reference_label_link';
// Configure the field
$field = field_info_field($field_name);
if (empty($field)) {
$field = array(
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => 1,
'entity_types' => array('commerce_order'),
'translatable' => FALSE,
'locked' => TRUE,
);
$field = field_create_field($field);
}
// configure field instances for each bundle
foreach ($bundles as $bundle) {
$instance = field_info_instance('commerce_order', $field_name, $bundle);
if (!empty($instance)) {
continue;
}
$instance = array(
'field_name' => $field_name,
'entity_type' => 'commerce_order',
'bundle' => $bundle,
'label' => t('Card on file Processed'),
'settings' => array(),
'widget' => array(
'type' => $default_widget,
'weight' => 10,
),
'display' => array(),
);
// Set the default display formatters for various view modes.
foreach (array('default', 'customer', 'administrator') as $view_mode) {
$instance['display'][$view_mode] = array(
'label' => 'above',
'type' => $default_formatter,
'weight' => 10,
);
}
field_create_instance($instance);
}
}
// -----------------------------------------------------------------------
// Checkout
/**
* Implements hook_form_alter().
*
* This implementation alters any checkout form looking for the payment pane
* and seeing if its details are currently for a credit card payment method. If
* so, it adds the necessary form elements for Card on File payment, including a
* select element to use previously stored credit card information and a
* checkbox on the credit card data entry form to store the given credit card on
* file for future usage.
*/
function commerce_cardonfile_form_alter(&$form, &$form_state, $form_id) {
// Exit if the current form ID is for a checkout page form...
if (strpos($form_id, 'commerce_checkout_form_') !== 0 ||
!commerce_checkout_page_load(substr($form_id, 23))) {
return;
}
// Exit if the current page's form does no include the payment checkout pane...
if (empty($form['commerce_payment'])) {
return;
}
// DENY if user is not defined or is anonymous
if (!isset($form_state['account']) || empty($form_state['account']->uid)) {
return;
}
// Exit if no payment method instance id
if (empty($form['commerce_payment']['payment_method']['#default_value'])) {
return;
}
// Exit if not a credit card based payment method
if (empty($form['commerce_payment']['payment_details']['credit_card'])) {
return;
}
// Extact payment method instance id
$instance_id = $form['commerce_payment']['payment_method']['#default_value'];
// Check to see if the currently selected payment method is Card on File
// enabled (via the cardonfile boolean in its info array).
$payment_method = commerce_payment_method_instance_load($instance_id);
// Exit if payment method is not capable of card on file
if (!_commerce_cardonfile_capable_payment_method_check($payment_method)) {
return;
}
// Add a checkbox to the credit card details container to store the
// credit card for future use.
$storage = variable_get('commerce_cardonfile_storage', 'opt-in');
if (in_array($storage, array('opt-in', 'opt-out'))) {
$form['commerce_payment']['payment_details']['credit_card']['cardonfile_store'] = array(
'#type' => 'checkbox',
'#title' => t('Store this credit card on file for future use.'),
'#default_value' => $storage == 'opt-out',
);
}
else {
$form['commerce_payment']['payment_details']['credit_card']['cardonfile_store'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
// Load existing active cards for the payment method instance and user.
$stored_cards = commerce_cardonfile_data_load_multiple_by_id(array(), array(
'uid' => $form_state['account']->uid,
'instance_id' => $payment_method['instance_id'],
'status' => 1,
));
// Build options form
$cardonfile_options_form = array();
$instance_default_card_id = NULL;
// If have stored cards ...
if (!empty($stored_cards)) {
$valid_cards = array_filter($stored_cards, 'commerce_cardonfile_data_check_expiration');
// If have un-expired cards ...
if (!empty($valid_cards)) {
// get options list with labels
$card_option_element_type = variable_get('commerce_cardonfile_selector', 'radios');
$card_options = commerce_cardonfile_element_options_list($valid_cards, $card_option_element_type);
// determine default option
$card_options_default_value = key($card_options);
foreach (array_keys($card_options) as $card_id) {
if (isset($valid_cards[$card_id]) && !empty($valid_cards[$card_id]['instance_default'])) {
$card_options_default_value = $instance_default_card_id = $card_id;
// move instance default to the top of the list
$card_option_label = $card_options[$card_id];
unset($card_options[$card_id]);
$card_options = array($card_id => $card_option_label) + $card_options;
break;
}
}
// create options element
$cardonfile_options_form = array(
'#type' => $card_option_element_type,
'#title' => t('Select a stored credit card'),
'#options' => $card_options,
'#default_value' => $card_options_default_value,
'#weight' => -10,
'#ajax' => array(
'callback' => 'commerce_payment_pane_checkout_form_details_refresh',
'wrapper' => 'payment-details',
),
);
}
}
// update form with options
if (!empty($cardonfile_options_form)) {
$form['commerce_payment']['payment_details']['cardonfile'] = $cardonfile_options_form;
// Add the CSS to hide a sole credit card icon if specified.
if (variable_get('commerce_cardonfile_hide_cc_radio_button', TRUE)) {
if (count($form['commerce_payment']['payment_method']['#options']) == 1) {
$form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_cardonfile') . '/theme/commerce_cardonfile.checkout.css';
}
}
// If the current value for the card selection element is not to use
// a different credit card, then hide the credit card form elements.
if (empty($form_state['values']) || $form_state['values']['commerce_payment']['payment_details']['cardonfile'] !== 'new') {
$form['commerce_payment']['payment_details']['credit_card']['#access'] = FALSE;
}
}
else {
$form['commerce_payment']['payment_details']['cardonfile'] = array(
'#type' => 'value',
'#value' => 'new',
);
}
// Add mark as default element
$instance_default_default_value = 0;
if (!empty($instance_default_card_id)) {
if (empty($form_state['values']) ||
(!empty($form_state['values']['commerce_payment']['payment_details']['cardonfile']) &&
$form_state['values']['commerce_payment']['payment_details']['cardonfile'] == $instance_default_card_id)) {
$instance_default_default_value = 1;
}
}
$force_instance_default = empty($stored_cards);
$form['commerce_payment']['payment_details']['cardonfile_instance_default'] = array(
'#type' => 'checkbox',
'#title' => t('Set as your default card'),
'#default_value' => $instance_default_default_value || $force_instance_default,
'#access' => !$instance_default_default_value,
'#disabled' => $force_instance_default,
'#states' => array(
'invisible' => array(
':input[name$="[cardonfile]"]' => array('value' => 'new'),
),
'visible' => array(
':input[name$="[cardonfile_store]"]' => array('checked' => TRUE),
),
),
);
// Allow others to alter this alter
drupal_alter('commerce_cardonfile_checkout_pane_form', $form['commerce_payment']['payment_details'], $form);
// Add submit handler
if (isset($form['buttons']['continue'])) {
$form['buttons']['continue']['#submit'][] = 'commerce_cardonfile_commerce_checkout_form_submit';
}
}
/**
* Checkout form submit callback to process card on file options
*/
function commerce_cardonfile_commerce_checkout_form_submit($form, &$form_state) {
if (!isset($form_state['order']) || empty($form_state['values']['commerce_payment'])) {
return;
}
$pane_values = &$form_state['values']['commerce_payment'];
$store_card = !empty($pane_values['payment_details']['credit_card']['cardonfile_store']);
// Exit if no card selection
if (empty($pane_values['payment_details']['cardonfile'])) {
return;
}
// Load a fresh copy of the order stored in the form.
$order = commerce_order_load($form_state['order']->order_id);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$account = $form_state['account'];
$instance_id = $pane_values['payment_method'];
// get card on file value
$card_id_selected = NULL;
if ($pane_values['payment_details']['cardonfile'] != 'new') {
$card_id_selected = intval($pane_values['payment_details']['cardonfile']);
}
elseif ($store_card && ($cof_last_inserts = _commerce_cardonfile_data_current_request_last_inserted())) {
// Assumes the new card is the current request's last inserted card that
// matches the user
foreach ($cof_last_inserts as $card_data) {
if ($card_data['uid'] == $account->uid && $card_data['instance_id'] == $instance_id) {
$card_id_selected = $card_data['card_id'];
break;
}
}
}
// Submit actions for card selected
if (!empty($card_id_selected)) {
// Mark as default
if (!empty($pane_values['payment_details']['cardonfile_instance_default'])) {
commerce_cardonfile_data_set_default_card($card_id_selected);
}
// Save card id to the order reference field
$order_wrapper->commerce_cardonfile = array('card_id' => $card_id_selected);
$order_wrapper->save();
}
}
/**
* Returns an options array for selecting a card on file during checkout
*
* @param $stored_cards
* An array of stored card data arrays keyed by card_id.
* @param $element_type
* The form element the options array will be for, 'radios' or 'select'.
* @param $different
* Add an option to use a different credit card.
*
* @return
* An options array for selecting a card on file.
*/
function commerce_cardonfile_element_options_list($stored_cards, $element_type = 'radios', $different = TRUE) {
// Build an options array of stored credit cards.
$options = array();
foreach ($stored_cards as $card_id => $card_data) {
$options[$card_id] = theme('commerce_cardonfile_data_label', array('card_data' => $card_data));
}
// Add an option to use a different credit card if specified.
if ($different) {
$options['new'] = t('Use a different credit card');
}
return $options;
}
// -----------------------------------------------------------------------
// Payment method callback handling
/**
* Returns an array of all available payment method cardonfile callbacks
*/
function commerce_cardonfile_payment_method_available_callbacks() {
return array(
'create callback',
'update callback',
'delete callback',
'process callback',
'get address callback',
'charge callback', // (implemented in commerce_recurring)
);
}
/**
* Returns the Card on File callback function for the given payment method.
*
* @param $payment_method
* The payment method object.
* @param $callback
* The callback function to return, one of:
* - 'create callback',
* - 'update callback'
* - 'delete callback'
* - 'process callback'
* - 'get address callback'
* - 'charge callback' (implemented in commerce_recurring)
*
* @return
* A string containing the name of the callback function or FALSE if it could
* not be found.
*/
function commerce_cardonfile_payment_method_callback($payment_method, $callback) {
if (!empty($payment_method) && !empty($payment_method['method_id'])) {
$implements = commerce_cardonfile_payment_method_implements($callback);
if (!empty($implements) && !empty($implements[$payment_method['method_id']])) {
return $implements[$payment_method['method_id']];
}
}
}
/**
* Returns all payment method instances that implement a specific callback
*
* @param $callback
* The callback function to return, one of:
* - 'create callback',
* - 'update callback'
* - 'delete callback'
* - 'process callback'
* - 'get address callback'
* - 'charge callback' (implemented in commerce_recurring)
*
* @return
* An array of callback function names keyed by payment method id
*/
function commerce_cardonfile_payment_method_implements($callback) {
$cache = &drupal_static(__FUNCTION__);
if (!isset($cache)) {
$cache = array();
// get payment methods and load module implements for hook_commerce_payment_method_info()
// so that cardonfile callback can be in same hook file, ie mymodule.commerce.inc
$payment_methods = _commerce_cardonfile_capable_payment_methods();
$available_callbacks = commerce_cardonfile_payment_method_available_callbacks();
foreach ($payment_methods as $method_id => $payment_method) {
foreach ($available_callbacks as $available_callback) {
if (!empty($payment_method['cardonfile'][$available_callback])) {
$func = $payment_method['cardonfile'][$available_callback];
if (function_exists($func)) {
$cache[$available_callback][$method_id] = $func;
}
}
}
}
}
return isset($cache[$callback]) ? $cache[$callback] : array();
}
/**
* Returns all payment methods that are card on file capable
*
* @return
* An associative array of payment method objects keyed by the method_id.
*/
function _commerce_cardonfile_capable_payment_methods() {
$capable_methods = array();
$payment_methods = commerce_payment_methods();
foreach ($payment_methods as $method_id => $payment_method) {
if (_commerce_cardonfile_capable_payment_method_check($payment_method)) {
$capable_methods[$method_id] = $payment_method;
}
}
return $capable_methods;
}
/**
* Returns TRUE if a payment method is capable of card on file
*
* @param $payment_method
* An associative array of payment method objects keyed by the method_id.
*
* @return
* TRUE if payment method is capable of card on file
*/
function _commerce_cardonfile_capable_payment_method_check($payment_method) {
return !empty($payment_method['cardonfile']);
}
/**
* Returns all payment method instances that are card on file capable
*
* @param $include_disabled
* Return enabled and disabled instances
*
* @return
* An associative array of payment method objects keyed by the instance_id.
*/
function _commerce_cardonfile_capable_payment_method_instances($include_disabled = FALSE) {
$capable_instances = array();
$payment_methods = _commerce_cardonfile_capable_payment_methods();
foreach ($payment_methods as $method_id => $payment_method) {
$method_instances = _commerce_cardonfile_payment_method_instances($method_id, $include_disabled);
$capable_instances += $method_instances;
}
return $capable_instances;
}
/**
* Returns all payment method instances for a given payment method id
*
* @param $method_id
* A payment method id
* @param $include_disabled
* Return enabled and disabled instances
*
* @return
* An array of all loaded payment method instances keyed by instance_id
*/
function _commerce_cardonfile_payment_method_instances($method_id, $include_disabled = FALSE) {
$cached_ids = &drupal_static(__FUNCTION__, array());
$include_disabled = !empty($include_disabled);
if (!array_key_exists($method_id, $cached_ids)) {
$cached_ids[$method_id] = array();
// load all rules ... no easier way
$rules_configs = rules_config_load_multiple(FALSE);
// find all rules with an action to enable this method
foreach ($rules_configs as $rule_name => $rule) {
// Only rules and sub-types have actions.
if (!($rule instanceof Rule)) {
continue;
}
// fast skip if rule does not depend on commerce_payment
if (!isset($rule->dependencies) || !in_array('commerce_payment', $rule->dependencies)) {
continue;
}
foreach ($rule->actions() as $action) {
// skip any actions that are not simple rules actions, ie loops
if (!($action instanceof RulesAction)) {
continue;
}
if ($action->getElementName() == 'commerce_payment_enable_' . $method_id) {
$instance_id = commerce_payment_method_instance_id($method_id, $rule);
$cached_ids[$method_id][$instance_id] = $rule->active;
continue 2; // skip to next rule
}
}
}
}
// load instances
$instances = array();
if (!empty($cached_ids[$method_id])) {
foreach ($cached_ids[$method_id] as $instance_id => $instance_active) {
if ($instance_active || $include_disabled) {
$instances[$instance_id] = commerce_payment_method_instance_load($instance_id);
}
}
}
return $instances;
}
// -----------------------------------------------------------------------
// Card data handling
/**
* Returns a list of cof data statuses
*/
function commerce_cardonfile_data_statuses() {
$statuses = array(
0 => t('Disabled'),
1 => t('Active'),
2 => t('Not deletable'),
);
return $statuses;
}
/**
* Returns the human readable title of any or all data statuses.
*
* @param $name
* Optional parameter specifying the name of the status whose title
* to return.
*
* @return
* Either an array of all status titles keyed by the status_id or a
* string containing the human readable title for the specified status. If a
* status is specified that does not exist, this function returns FALSE.
*/
function commerce_cardonfile_data_status_get_title($name = NULL) {
$statuses = commerce_cardonfile_data_statuses();
// Return a status title if specified and it exists.
if (!empty($name)) {
if (isset($statuses[$name])) {
return $statuses[$name];
}
else {
// Return FALSE if it does not exist.
return FALSE;
}
}
// Otherwise return all titles.
return $statuses;
}
/**
* Wraps commerce_cardonfile_status_get_title().
*/
function commerce_cardonfile_data_status_options_list() {
return commerce_cardonfile_data_status_get_title();
}
/**
* Define property info for line item Kiala Point records
*/
function commerce_cardonfile_data_property_info_callback() {
return array(
'card_id' => array(
'label' => t('Card Data Unique ID'),
'type' => 'integer',
),
'uid' => array(
'label' => t('Owner User ID'),
'type' => 'integer',
),
'owner' => array(
'label' => t('Owner'),
'type' => 'user',
'description' => t('The card owner user object.'),
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'payment_method' => array(
'label' => t('Payment Method ID'),
'type' => 'text',
),
'instance_id' => array(
'label' => t('Payment Method Instance ID'),
'type' => 'text',
'description' => t('The instance id for the payment method instance rules configuration.'),
),
'payment_method_instance' => array(
'label' => t('Payment Method Instance'),
'type' => 'struct',
'description' => t('The loaded payment method instance info array.'),
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'rule_name' => array(
'label' => t('Payment Method Instance Rule Name'),
'type' => 'text',
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'rule_config' => array(
'label' => t('Payment Method Instance Rule Config'),
'type' => 'rules_config',
'description' => t('The rules config object of the payment method instance.'),
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'remote_id' => array(
'label' => t('Remote ID'),
'type' => 'text',
),
'card_type' => array(
'label' => t('Card Type'),
'type' => 'text',
),
'card_name' => array(
'label' => t('Name on the Card'),
'type' => 'text',
),
'card_number' => array(
'label' => t('Card Number (last 4 digits)'),
'type' => 'text',
),
'card_exp_month' => array(
'label' => t('Card Expiration Month'),
'type' => 'integer',
),
'card_exp_year' => array(
'label' => t('Card Expiration Year'),
'type' => 'integer',
),
'card_exp_timestamp' => array(
'label' => t('Card Expiration Timestamp'),
'type' => 'date',
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'card_expired' => array(
'label' => t('Card Expired'),
'type' => 'boolean',
'description' => t('TRUE if the card has expired.'),
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'status' => array(
'label' => t('Status'),
'type' => 'integer',
'options list' => 'commerce_cardonfile_data_statuses',
),
'can_charge' => array(
'label' => t('Can Charge'),
'type' => 'boolean',
'description' => t('TRUE if the card can be charged.'),
'getter callback' => 'commerce_cardonfile_data_property_get',
'computed' => TRUE,
),
'created' => array(
'label' => t('Created Date'),
'type' => 'date',
),
'changed' => array(
'label' => t('Changed Date'),
'type' => 'date',
),
'instance_default' => array(
'label' => t('Instance Default'),
'type' => 'boolean',
'description' => t('TRUE if the card has been marked as the default for the payment method instance.'),
),
);