t('Pay with Credit Card'), 'description' => t('Trustcommerce credit card payment.'), 'active' => TRUE, 'offsite' => FALSE, ); return $payment_methods; } /** * Payment method callback: submit form. */ function commerce_payment_tc_submit_form($payment_method, $pane_values, $checkout_pane, $order) { module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card'); // Default to a known test credit card number. For valid numbers of other card // types see: http://www.rimmkaufman.com/blog/credit-card-test-numbers/09112007/ //return commerce_payment_credit_card_form( array('number' => '', 'code' => '','owner' => '')); return commerce_payment_credit_card_form( array('number' => '', 'code' => '')); } /** * Payment method callback: submit form validation. */ function commerce_payment_tc_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) { // Validate the credit card fields. module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card'); $settings = array( 'form_parents' => array_merge($form_parents, array('credit_card')), ); // Allow empty cvv field if (empty($pane_values['credit_card']['code'])) { unset($pane_values['credit_card']['code']); } // Even though a form error triggered by the validate handler would be enough // to stop the submission of the form, it's not enough to stop it from a // Commerce standpoint because of the combined validation / submission going // on per-pane in the checkout form. Thus even with a call to form_set_error() // this validate handler must still return FALSE. if (!commerce_payment_credit_card_validate($pane_values['credit_card'], $settings)) { return FALSE; } } /** * Payment method callback: submit form submission. */ function commerce_payment_tc_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) { //if ($order->hostname == '74.94.156.210'){ $iphost=ip2long($order->hostname); db_set_active('civicrm'); $result=db_query("SELECT * FROM civicrm.trustcommerce_blacklist WHERE $iphost >= start AND $iphost <= end")->fetchField(); db_set_active(); if ($result){ drupal_set_message(t('The payment failed (error 9001), please contact with us at donate@fsf.org'), 'error'); drupal_goto(commerce_checkout_order_uri($order)); commerce_payment_redirect_pane_previous_page($order); return; } //return; } $tc = array(); $tc['checkcvv'] = 'y';// credit verification value $tc['custid'] = variable_get('tcuser'); $tc['password'] = variable_get('tcpassword'); $tc['avs'] = 'y'; // address verification service $tc['demo'] = "n"; // ensure that it is in demo mode by default $tc['ticket'] = 'fsfshop-'.$order->order_id; // information transferred to bank; $order_wrapper = entity_metadata_wrapper('commerce_order', $order->order_id); $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value(); $tc['name'] = $billing_address['name_line']; $tc['address1'] = $billing_address['thoroughfare']; #$tc['address2'] = $billing_address['premise']; $tc['city'] = $billing_address['locality']; $tc['state'] = $billing_address['administrative_area']; $tc['zip'] = $billing_address['postal_code']; $tc['country'] = $billing_address['country']; #$tc['email'] = $order->mail; $tc['cc'] = $pane_values['credit_card']['number']; $tc['exp'] = $pane_values['credit_card']['exp_month'] . substr($pane_values['credit_card']['exp_year'],2); $tc['cvv'] = $pane_values['credit_card']['code']; #$tc['ip'] = $order->hostname; $tc['media']= 'cc'; $tc['action'] = 'sale'; $tc['amount'] = $charge['amount']; $response = _trustcommerce_tclink($tc); $tctmp= $tc; $tctmp['cc'] = "****"; $tctmp['exp'] = "****"; $tctmp['cvv'] = "****"; $tctmp['password'] = "****"; if ( array_key_exists('declinetype', $response)){ $error=sprintf("TrustCommerce: IP=%s ORDER=%s NAME=%s MAIL=%s AMOUNT=%s STATUS=%s AVS=%s TRANSACTION_ID=%s DECLINETYPE=%s", $order->hostname, $order->order_number, $tctmp['name'], $order->mail, $charge['amount'], $response['status'], $response['avs'], $response['transid'], $response['declinetype'] ); } else{ $error=sprintf("TrustCommerce: ORDER=%s IP=%s NAME=%s MAIL=%s AMOUNT=%s STATUS=%s AVS=%s TRANSACTION_ID=%s", $order->hostname, $order->order_number, $tctmp['name'], $order->mail, $charge['amount'], $response['status'], $response['avs'], $response['transid'] ); } error_log($error,0); //DEBUG $myfile = fopen("/tmp/tc.log", "a"); fwrite($myfile, "------------------------------------------\n"); fwrite($myfile, $error); fwrite($myfile, "\n"); fwrite($myfile, date("Y-m-d H:i:s")); fwrite($myfile, "\n"); fwrite($myfile, $order->hostname); fwrite($myfile, "\n"); fwrite($myfile, print_r($order, true)); fwrite($myfile, "\n"); fwrite($myfile, print_r($charge, true)); fwrite($myfile, "\n"); fwrite($myfile, print_r($response, true)); fwrite($myfile, "\n"); fwrite($myfile, print_r($tctmp, true)); fwrite($myfile, "\n"); fclose($myfile); // Just as an example, we might store information in the order object from the // payment parameters, though we would never save a full credit card number, // even in examples! $number = $pane_values['credit_card']['number']; $pane_values['credit_card']['number'] = 0; //substr($number, 0, 4) . str_repeat('-', strlen($number) - 8) . substr($number, -4); $order->data['commerce_payment_tc'] = $pane_values; // Every attempted transaction should result in a new transaction entity being // created for the order to log either the success or the failure. commerce_payment_tc_transaction($payment_method, $order, $charge, $response); } /** * Creates an tc payment transaction for the specified charge amount. * * @param $payment_method * The payment method instance object used to charge this payment. * @param $order * The order object the payment applies to. * @param $charge * An array indicating the amount and currency code to charge. */ function commerce_payment_tc_transaction($payment_method, $order, $charge, $response) { $card_details = $order->data['commerce_payment_tc']['credit_card']; $txid=$response['transid']; $status=$response['status']; $transaction = commerce_payment_transaction_new('commerce_payment_tc', $order->order_id); $transaction->remote_id = $txid; $transaction->instance_id = $payment_method['instance_id']; $transaction->amount = $charge['amount']; $transaction->currency_code = $charge['currency_code']; $transaction->status = $status; //$transaction->message = 'Number: @number
Expiration: @month/@year'; $transaction->message = print_r($response, true); $transaction->message_variables = array( '@number' => $card_details['number'], '@month' => $card_details['exp_month'], '@year' => $card_details['exp_year'], ); if ($status == 'approved'){ $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; drupal_set_message(t('Order approved'), 'status'); commerce_payment_transaction_save($transaction); } else { $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE; commerce_payment_transaction_save($transaction); //https://vault.trustcommerce.com/downloads/TCDevGuide.html#Decline%20Type|outline 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'); elseif ($response['declinetype']=='decline') drupal_set_message(t('The credit card was declined, please contact your bank.'), 'error'); 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'); elseif ($response['declinetype']=='call') drupal_set_message(t('The credit card must be authorized manually over the phone, please contact your bank.'), 'error'); elseif ($response['declinetype']=='expiredcard ') drupal_set_message(t('The credit card is expired.'), 'error'); 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'); elseif ($response['declinetype']=='fraud') drupal_set_message(t('The credit card fraud score is too high, please contact your bank.'), 'error'); elseif ($response['declinetype']=='blacklist') drupal_set_message(t('The credit card is blacklisted, please contact your bank.'), 'error'); elseif ($response['declinetype']=='velocity') drupal_set_message(t('The credit card was used too recently, please try again later.'), 'error'); elseif ($response['declinetype']=='dailylimit') drupal_set_message(t('The credit card has reached its daily limit, please contact your bank.'), 'error'); elseif ($response['declinetype']=='weeklylimit') drupal_set_message(t('The credit card has reached its weekly limit, please contact your bank.'), 'error'); elseif ($response['declinetype']=='monthlylimit') drupal_set_message(t('The credit card has reached its monthly limit, please contact your bank.'), 'error'); else drupal_set_message(t('The payment failed, please contact with us at donate@fsf.org'), 'error'); drupal_goto(commerce_checkout_order_uri($order)); commerce_payment_redirect_pane_previous_page($order); } return $transaction; } function _trustcommerce_tclink($tc) { // verify that the module is loaded if (!extension_loaded("tclink")) { if (!dl("tclink.so")) { drupal_set_message("tclink.so is not loaded- aborting"); exit(1); } } $answer = tclink_send($tc); //send it to the payment gateway return $answer; }