-
Notifications
You must be signed in to change notification settings - Fork 3
/
uc_braintree.module
128 lines (111 loc) · 4.5 KB
/
uc_braintree.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
<?php
// $Id: uc_braintree.module
require_once 'braintree/lib/Braintree.php';
function uc_braintree_payment_gateway() {
$gateways[] = array(
'id' => 'uc_braintree',
'title' => t('Braintree'),
'description' => t('Process credit card payments through Braintree.'),
'settings' => 'uc_braintree_settings_form',
'credit' => 'uc_braintree_charge',
);
return $gateways;
}
/*
* Implementation of hook_settings_form
*/
function uc_braintree_settings_form() {
$form['uc_braintree_environemnt'] = array(
'#type' => 'select',
'#title' => t('Environment'),
'#default_value' => variable_get('uc_braintree_environemnt', 'sandbox'),
'#options' => drupal_map_assoc(array('sandbox','production')),
);
$form['braintree_keys'] = array(
'#type' => 'fieldset',
'#title' => t('API Keys'),
'#description' => t('This information is required for Ubercart to interact with your payment gateway account. You can access your API keys on your Braintree control panel'),
);
$form['braintree_keys']['uc_braintree_merchant_id'] = array(
'#type' => 'textfield',
'#title' => t('Merchant ID'),
'#description' => t('Your Braintree merchant ID'),
'#default_value' => variable_get('uc_braintree_merchant_id', ''),
);
$form['braintree_keys']['uc_braintree_public_key'] = array(
'#type' => 'textfield',
'#title' => t('Public Key'),
'#description' => t('Your Braintree public key'),
'#default_value' => variable_get('uc_braintree_public_key', ''),
);
$form['braintree_keys']['uc_braintree_private_key'] = array(
'#type' => 'textfield',
'#title' => t('Private Key'),
'#description' => t('Your Braintree private key'),
'#default_value' => variable_get('uc_braintree_private_key', ''),
);
return $form;
}
/*
* Implementation of hook_charge
*/
function uc_braintree_charge($order_id, $amount, $data) {
try {
Braintree_Configuration::environment( variable_get('uc_braintree_environemnt', 'sandbox') );
Braintree_Configuration::merchantId( variable_get('uc_braintree_merchant_id','') );
Braintree_Configuration::publicKey( variable_get('uc_braintree_public_key','') );
Braintree_Configuration::privateKey( variable_get('uc_braintree_private_key','') );
// Load the user and order
global $user;
$order = uc_order_load($order_id);
// Create a Braintree transaction and submit information to create a payment
$response = Braintree_Transaction::sale(array(
'amount' => $amount,
'creditCard' => array(
'cardholderName' => $order->payment_details['cc_owner'],
'number' => $order->payment_details['cc_number'],
'expirationDate' => $order->payment_details['cc_exp_month'] . "/" . $order->payment_details['cc_exp_year'],
'cvv' => $order->payment_details['cc_cvv']
)
));
$success = $response->success;
// If payment was successful, add comment
if ($success) {
$context = array(
'revision' => 'formatted-original',
'type' => 'amount',
);
$message = t('Credit card charged: !amount', array('!amount' => uc_price($amount, $context)));
uc_order_comment_save($order_id, $user->uid, $message, 'admin');
} else {
if ($response->transaction->processorResponseCode != NULL) {
// Handle processor response codes appropriately
$message = "Error " . $response->transaction->processorResponseCode . ": " . $response->transaction->processorResponseText;
drupal_set_message(t($message), 'error');
// Log order comment
uc_order_comment_save($order_id, $user->uid, $message, 'admin');
} else {
// Handle validation errors
foreach($response->errors->deepAll() AS $error) {
drupal_set_message(t($error->message), 'error');
}
}
}
// Return result to have payment or error recorded
$result = array(
'success' => $success,
'comment' => t('Card charged'),
'message' => $success ? t('Credit card payment processed successfully.') : t('Credit card charge failed.'),
'uid' => $user->uid,
// 'data' => $data,
);
return $result;
} catch (Braintree_Exception_Configuration $e) {
// Catch missing configuration variables
drupal_set_message(t("Braintree is missing configuration variables; check your gateway settings"), 'error');
return;
} catch (Exception $e) {
drupal_set_message(t("Braintree returned an error: ". $e->getMessage()), 'error');
return;
}
}