changed the message given for TC 'cvv' error
[com-trustcommerce.git] / commerce_payment_tc.module
CommitLineData
0a90033f
AE
1<?php
2
3/**
4 * @file
5 * Provides an tc payment method for Drupal Commerce for testing and
6 * development.
7 */
8
9
10/**
11 * Implements hook_commerce_payment_method_info().
12 */
13function commerce_payment_tc_commerce_payment_method_info() {
14 $payment_methods = array();
15
16 $payment_methods['commerce_payment_tc'] = array(
17 'title' => t('Pay with Credit Card'),
18 'description' => t('Trustcommerce credit card payment.'),
19 'active' => TRUE,
20 'offsite' => FALSE,
21 );
22
23 return $payment_methods;
24}
25
26/**
27 * Payment method callback: submit form.
28 */
29function commerce_payment_tc_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
30 module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
31
32 // Default to a known test credit card number. For valid numbers of other card
33 // types see: http://www.rimmkaufman.com/blog/credit-card-test-numbers/09112007/
34 //return commerce_payment_credit_card_form( array('number' => '', 'code' => '','owner' => ''));
35 return commerce_payment_credit_card_form( array('number' => '', 'code' => ''));
36}
37
38/**
39 * Payment method callback: submit form validation.
40 */
41function commerce_payment_tc_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
42 // Validate the credit card fields.
43 module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
44
45 $settings = array(
46 'form_parents' => array_merge($form_parents, array('credit_card')),
47 );
48
49 // Allow empty cvv field
50 if (empty($pane_values['credit_card']['code']))
51 {
52 unset($pane_values['credit_card']['code']);
53 }
54
55 // Even though a form error triggered by the validate handler would be enough
56 // to stop the submission of the form, it's not enough to stop it from a
57 // Commerce standpoint because of the combined validation / submission going
58 // on per-pane in the checkout form. Thus even with a call to form_set_error()
59 // this validate handler must still return FALSE.
60 if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) {
61 return FALSE;
62 }
63}
64
65/**
66 * Payment method callback: submit form submission.
67 */
68function commerce_payment_tc_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
69
70
71//if ($order->hostname == '74.94.156.210'){
72
73 $iphost=ip2long($order->hostname);
74
75 db_set_active('civicrm');
76 $result=db_query("SELECT * FROM civicrm.trustcommerce_blacklist WHERE $iphost >= start AND $iphost <= end")->fetchField();
77 db_set_active();
78
79 if ($result){
80 drupal_set_message(t('The payment failed (error 9001), please contact with us at donate@fsf.org'), 'error');
81 drupal_goto(commerce_checkout_order_uri($order));
82 commerce_payment_redirect_pane_previous_page($order);
83 return;
84 }
85
86//return; }
87
88 $tc = array();
89
90 $tc['checkcvv'] = 'y';// credit verification value
91 $tc['custid'] = variable_get('tcuser');
92 $tc['password'] = variable_get('tcpassword');
93 $tc['avs'] = 'y'; // address verification service
94 $tc['demo'] = "n"; // ensure that it is in demo mode by default
95 $tc['ticket'] = 'fsfshop-'.$order->order_id; // information transferred to bank;
96
97 $order_wrapper = entity_metadata_wrapper('commerce_order', $order->order_id);
98 $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
99 $tc['name'] = $billing_address['name_line'];
100 $tc['address1'] = $billing_address['thoroughfare'];
101 #$tc['address2'] = $billing_address['premise'];
102 $tc['city'] = $billing_address['locality'];
103 $tc['state'] = $billing_address['administrative_area'];
104 $tc['zip'] = $billing_address['postal_code'];
105 $tc['country'] = $billing_address['country'];
106 #$tc['email'] = $order->mail;
107
108 $tc['cc'] = $pane_values['credit_card']['number'];
109 $tc['exp'] = $pane_values['credit_card']['exp_month'] . substr($pane_values['credit_card']['exp_year'],2);
110 $tc['cvv'] = $pane_values['credit_card']['code'];
111 #$tc['ip'] = $order->hostname;
112 $tc['media']= 'cc';
113
114 $tc['action'] = 'sale';
115 $tc['amount'] = $charge['amount'];
116 $response = _trustcommerce_tclink($tc);
117
118$tctmp= $tc;
119$tctmp['cc'] = "****";
120$tctmp['exp'] = "****";
121$tctmp['cvv'] = "****";
122$tctmp['password'] = "****";
123
124if ( array_key_exists('declinetype', $response)){
125 $error=sprintf("TrustCommerce: IP=%s ORDER=%s NAME=%s MAIL=%s AMOUNT=%s STATUS=%s AVS=%s TRANSACTION_ID=%s DECLINETYPE=%s",
126 $order->hostname, $order->order_number, $tctmp['name'], $order->mail, $charge['amount'], $response['status'], $response['avs'], $response['transid'], $response['declinetype'] );
127}
128else{
129 $error=sprintf("TrustCommerce: ORDER=%s IP=%s NAME=%s MAIL=%s AMOUNT=%s STATUS=%s AVS=%s TRANSACTION_ID=%s",
130 $order->hostname, $order->order_number, $tctmp['name'], $order->mail, $charge['amount'], $response['status'], $response['avs'], $response['transid'] );
131}
132error_log($error,0);
133
134//DEBUG
135$myfile = fopen("/tmp/tc.log", "a");
136fwrite($myfile, "------------------------------------------\n");
137fwrite($myfile, $error);
138fwrite($myfile, "\n");
139fwrite($myfile, date("Y-m-d H:i:s"));
140fwrite($myfile, "\n");
141fwrite($myfile, $order->hostname);
142fwrite($myfile, "\n");
143fwrite($myfile, print_r($order, true));
144fwrite($myfile, "\n");
145fwrite($myfile, print_r($charge, true));
146fwrite($myfile, "\n");
147fwrite($myfile, print_r($response, true));
148fwrite($myfile, "\n");
149fwrite($myfile, print_r($tctmp, true));
150fwrite($myfile, "\n");
151fclose($myfile);
152
153 // Just as an example, we might store information in the order object from the
154 // payment parameters, though we would never save a full credit card number,
155 // even in examples!
156 $number = $pane_values['credit_card']['number'];
157 $pane_values['credit_card']['number'] = 0; //substr($number, 0, 4) . str_repeat('-', strlen($number) - 8) . substr($number, -4);
158
159 $order->data['commerce_payment_tc'] = $pane_values;
160 // Every attempted transaction should result in a new transaction entity being
161 // created for the order to log either the success or the failure.
162 commerce_payment_tc_transaction($payment_method, $order, $charge, $response);
163}
164
165/**
166 * Creates an tc payment transaction for the specified charge amount.
167 *
168 * @param $payment_method
169 * The payment method instance object used to charge this payment.
170 * @param $order
171 * The order object the payment applies to.
172 * @param $charge
173 * An array indicating the amount and currency code to charge.
174 */
175function commerce_payment_tc_transaction($payment_method, $order, $charge, $response) {
176 $card_details = $order->data['commerce_payment_tc']['credit_card'];
177
178 $txid=$response['transid'];
179 $status=$response['status'];
180
181 $transaction = commerce_payment_transaction_new('commerce_payment_tc', $order->order_id);
182 $transaction->remote_id = $txid;
183 $transaction->instance_id = $payment_method['instance_id'];
184 $transaction->amount = $charge['amount'];
185 $transaction->currency_code = $charge['currency_code'];
186 $transaction->status = $status;
187 //$transaction->message = 'Number: @number<br/>Expiration: @month/@year';
188 $transaction->message = print_r($response, true);
189 $transaction->message_variables = array(
190 '@number' => $card_details['number'],
191 '@month' => $card_details['exp_month'],
192 '@year' => $card_details['exp_year'],
193 );
194
195 if ($status == 'approved'){
196 $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
197 drupal_set_message(t('Order approved'), 'status');
198 commerce_payment_transaction_save($transaction);
199 }
200 else {
201 $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
202 commerce_payment_transaction_save($transaction);
203 //https://vault.trustcommerce.com/downloads/TCDevGuide.html#Decline%20Type|outline
204 if ($response['declinetype']=='avs') drupal_set_message(t('The credit card charge failed, please verify that the "Card Owner" field matches the name on the card, and that the "Billing Address" matches the one set at your bank for that credit card'), 'error');
205 elseif ($response['declinetype']=='decline') drupal_set_message(t('The credit card was declined, please contact your bank.'), 'error');
8bd779cd 206 elseif ($response['declinetype']=='cvv') drupal_set_message(t('Your transaction was declined. Please check the correctness of your credit card information, including CC number, expiration date and CVV code.'), 'error');
0a90033f
AE
207 elseif ($response['declinetype']=='call') drupal_set_message(t('The credit card must be authorized manually over the phone, please contact your bank.'), 'error');
208 elseif ($response['declinetype']=='expiredcard ') drupal_set_message(t('The credit card is expired.'), 'error');
209 elseif ($response['declinetype']=='carderror') drupal_set_message(t('Card number is invalid, which could be a typo, or sometimes a card reported stolen, please contact your bank.'), 'error');
210 elseif ($response['declinetype']=='fraud') drupal_set_message(t('The credit card fraud score is too high, please contact your bank.'), 'error');
211 elseif ($response['declinetype']=='blacklist') drupal_set_message(t('The credit card is blacklisted, please contact your bank.'), 'error');
212 elseif ($response['declinetype']=='velocity') drupal_set_message(t('The credit card was used too recently, please try again later.'), 'error');
213 elseif ($response['declinetype']=='dailylimit') drupal_set_message(t('The credit card has reached its daily limit, please contact your bank.'), 'error');
214 elseif ($response['declinetype']=='weeklylimit') drupal_set_message(t('The credit card has reached its weekly limit, please contact your bank.'), 'error');
215 elseif ($response['declinetype']=='monthlylimit') drupal_set_message(t('The credit card has reached its monthly limit, please contact your bank.'), 'error');
216 else drupal_set_message(t('The payment failed, please contact with us at donate@fsf.org'), 'error');
217 drupal_goto(commerce_checkout_order_uri($order));
218 commerce_payment_redirect_pane_previous_page($order);
219 }
220 return $transaction;
221}
222
223function _trustcommerce_tclink($tc) {
224 // verify that the module is loaded
225 if (!extension_loaded("tclink")) {
226 if (!dl("tclink.so")) {
227 drupal_set_message("tclink.so is not loaded- aborting");
228 exit(1);
229 }
230 }
231 $answer = tclink_send($tc); //send it to the payment gateway
232 return $answer;
233}