This repository has been archived by the owner on Sep 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuc_stripejs.module
225 lines (188 loc) · 8.3 KB
/
uc_stripejs.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
<?php
define('stripe_sk', 'STRIPE_SECRET_KEY');
define('stripe_pk', 'STRIPE_PUBLIC_KEY');
drupal_add_js('https://js.stripe.com/v2/');
drupal_add_js(array('stripe_js_pk' => stripe_pk), 'setting');
drupal_add_js(drupal_get_path('module', 'uc_stripejs') . '/uc_stripejs.js');
/**
* Implements hook_uc_payment_method().
*/
function uc_stripejs_uc_payment_method() {
$title1 = t('Credit/Debit card payments via Stripe.');
$cc_types = array(
'visa' => t('Visa'),
'mastercard' => t('MasterCard'),
'discover' => t('Discover'),
'amex' => t('American Express'),
'echeck' => t('eCheck'),
);
$methods[] = array(
'id' => 'stripe_js',
'name' => t('StripeJS Payments'),
'title' => $title1,
'review' => t('Credit / Debit Card'),
'desc' => t('Takes credit card payments via StripeJS.'),
'callback' => 'uc_stripejs_uc_payment_method_callback',
'credit' => 'uc_stripejs_charge',
'weight' => 1,
'checkout' => FALSE,
'no_gateway' => TRUE,
);
return $methods;
}
/**
* Implements hook_uc_payment_method().
*/
function uc_stripejs_uc_payment_method_callback($op, &$order, $form = NULL, &$form_state = NULL) {
switch($op){
case 'cart-details':
$details = uc_stripejs_method_credit_form(array(), $form_state, $order);
return $details;
break;
case 'cart-process':
// Fetch the CC details from the $_POST directly.
$cc_data = $form_state['values']['panes']['payment']['details'];
// Go ahead and put the CC data in the payment details array.
$order->payment_details = $cc_data;
$return = TRUE;
// Validate that a StripeToken has been set
if (empty($cc_data['stripeToken'])) {
form_set_error('panes][payment][details][cc_number', t('You have entered an invalid credit card number.'));
$return = FALSE;
}
// Initialize the encryption key and class.
$key = uc_credit_encryption_key();
$crypt = new UbercartEncryption();
// Store the encrypted details in the session for the next pageload.
// We are using base64_encode() because the encrypt function works with a
// limited set of characters, not supporting the full Unicode character
// set or even extended ASCII characters that may be present.
// base64_encode() converts everything to a subset of ASCII, ensuring that
// the encryption algorithm does not mangle names.
$_SESSION['sescrd'] = $crypt->encrypt($key, base64_encode(serialize($order->payment_details)));
// Log any errors to the watchdog.
uc_store_encryption_errors($crypt, 'uc_credit');
// If we're going to the review screen, set a variable that lets us know
// we're paying by CC.
if ($return) {
$_SESSION['stripe_pay'] = TRUE;
}
return $return;
break;
}
}
function uc_stripejs_method_credit_form($form, &$form_state, $order){
if (!isset($order->payment_details)) {
$order->payment_details = array();
}
$form['cc_policy'] = array(
'#markup' => '<p>' . variable_get('uc_credit_policy', t('Your billing information must match the billing address for the credit card entered below or we will be unable to process your payment.')) . '</p>'
);
$form['cc_error'] = array(
'#markup' => '<div class="messages error stripe-error-message" style="display: none;"><span class="message"></span></div>'
);
$form['cc_number'] = array(
'#type' => 'textfield',
'#title' => t('Card number'),
'#attributes' => array('autocomplete' => 'off', 'data-stripe' => 'number'),
'#size' => 20,
'#maxlength' => 19,
'#name' => ''
);
/**
* Expiration Date
*/
$form['cc_exp_month'] = uc_select_month(t('Expiration date'), 1) + array('#name' => '', '#attributes' => array('data-stripe' => 'exp-month'));
$form['cc_exp_year'] = uc_select_year(t('Expiration year'), date('Y')) + array('#name' => '', '#attributes' => array('data-stripe' => 'exp-year'));
if (variable_get('uc_credit_cvv_enabled', TRUE)) {
$form['cc_cvv'] = array(
'#type' => 'textfield',
'#title' => t('CVV'),
'#default_value' => NULL,
'#attributes' => array('autocomplete' => 'off', 'data-stripe' => 'cvc'),
'#size' => variable_get('uc_credit_amex', TRUE) ? 4 : 3,
'#maxlength' => variable_get('uc_credit_amex', TRUE) ? 4 : 3,
'#field_suffix' => theme('uc_credit_cvv_help'),
'#name' => ''
);
}
$form['stripeToken'] = array(
'#type' => 'hidden',
'#size' => 20,
'#maxlength' => 19,
);
unset($_SESSION['clear_cc']);
return $form;
}
/**
* Implements hook_uc_order().
*/
function uc_stripejs_uc_order($op, $order, $arg2) {
// Set up the encryption key and object for saving and loading.
if (isset($order->payment_method) && $order->payment_method == 'stripe_js' && ($op == 'save' || $op == 'load')) {
// Log an error if encryption isn't configured properly.
if (!uc_credit_encryption_key()) {
watchdog('uc_credit', 'Credit card encryption must be set up to process credit cards.');
}
}
switch ($op) {
case 'submit':
if (isset($order->payment_method) && $order->payment_method == 'stripe_js') {
require_once 'sites/all/libraries/stripe/lib/Stripe.php';
Stripe::setApiKey(stripe_sk);
// Clear out that session variable denoting this as a CC paid order.
unset($_SESSION['cc_pay']);
// Process CC transactions when an order is submitted after review.
$gateway_id = uc_credit_default_gateway();
$data = array(
'txn_type' => variable_get('uc_pg_' . $gateway_id . '_cc_txn_type', UC_CREDIT_AUTH_CAPTURE),
);
// Attempt to process the CC payment.
$order->payment_details = uc_credit_cache('load');
$total = $order->order_total * 100;
if($total / 100 != $order->order_total){
return false;
}
/**
* Process the payment here
*/
$pass = true;
$stripeToken = $order->payment_details['stripeToken'];
try {
Stripe_Charge::create(
array(
"amount" => $total,
"currency" => "gbp",
"card" => $stripeToken
)
);
}
catch (Exception $e) {
$pass = false;
}
// If the payment failed, store the data back in the session and
// halt the checkout process.
if (!$pass) {
$message = variable_get('uc_credit_fail_message', t('We were unable to process your credit card payment. Please verify your details and try again. If the problem persists, contact us to complete your order.'));
return array(array('pass' => FALSE, 'message' => $message));
}
return true;
}
break;
case 'save':
if (isset($order->payment_method) && $order->payment_method == 'credit' && !empty($order->payment_details)) {
_uc_credit_save_cc_data_to_order($order->payment_details, $order->order_id);
}
break;
case 'load':
if (isset($order->payment_method) && $order->payment_method == 'credit') {
// Load the CC details from the credit cache if available.
$order->payment_details = uc_credit_cache('load');
// Otherwise load any details that might be stored in the data array.
if (empty($order->payment_details) && isset($order->data['cc_data'])) {
$order->payment_details = uc_credit_cache('save', $order->data['cc_data']);
}
}
break;
}
}