function commerce_payleap_cof_submit_form_submit in Commerce Payleap 7
Payment method callback: checkout form submission - Card on file.
See also
MerchantServices.svc/ProcessCreditCard – Recurring Billing (SCM API Guide)
File
- ./
commerce_payleap.module, line 271 - Implements PayLeap payment services for use in Drupal Commerce.
Code
function commerce_payleap_cof_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge) {
$info = array();
$payment_method['settings']['txn_payleap_type'] = PAYLEAP_TXN_TYPE_RECURRING_CAPTURE;
$billing_data = commerce_payleap_get_billing_info($order);
// If Card on File storage is enabled and the form says to store data.
if (module_exists('commerce_cardonfile')) {
// If the customer specified payment using a card on file, attempt that now
// and simply return the result.
if (!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] !== 'new') {
$card_data = commerce_cardonfile_data_load($pane_values['cardonfile']);
$ids = explode('|', $card_data['remote_id']);
$info['CustomerKey'] = $ids[0];
$info['CcInfoKey'] = $ids[1];
}
else {
if (!empty($pane_values['credit_card']['cardonfile_store']) && $pane_values['credit_card']['cardonfile_store']) {
// First look to see if we already have cards on file for the user.
$stored_cards = commerce_cardonfile_data_load_multiple($order->uid, $payment_method['instance_id']);
// Prepare card to save.
$info['CustomerKey'] = '';
$new_card_data = array(
'uid' => $order->uid,
'payment_method' => $payment_method['method_id'],
'instance_id' => $payment_method['instance_id'],
'card_type' => 'card',
'card_name' => $billing_data['name_on_card'],
'card_number' => substr($pane_values['credit_card']['number'], -4),
'card_exp_month' => $pane_values['credit_card']['exp_month'],
'card_exp_year' => $pane_values['credit_card']['exp_year'],
'status' => 1,
);
// If we didn't find any card on file, attempt to make a new Customer Profile now.
if (empty($stored_cards)) {
// Submit a request to create the Customer Profile.
if ($response = commerce_payleap_customer_profile_request($payment_method, $order, $info, 'Add')) {
// If the Customer Profile creation was a success, store the new card.
if ($response['status']) {
// Get the remote ID.
$info['CustomerKey'] = (string) $response['xml']->CustomerKey;
// Save and log the creation of the Customer Profile.
watchdog('commerce_payleap', 'Customer Profile @profile_id created and saved to user @uid.', array(
'@profile_id' => $info['CustomerKey'],
'@uid' => $order->uid,
));
}
else {
// Could not save the a Customer Profile.
watchdog('commerce_payleap', 'Customer Profile save error. Unable to save profile for user @uid. @msg', array(
'@uid' => $order->uid,
'@msg' => $response['msg'],
));
}
}
}
else {
// Extract the user's Customer Profile ID from the first card's remote ID.
$card_data = reset($stored_cards);
$ids = explode('|', $card_data['remote_id']);
$info['CustomerKey'] = $ids[0];
}
// Could not get a Customer Profile.
if (empty($info['CustomerKey'])) {
$msg = t('Unable to save credit card profile.');
if (isset($response['msg'])) {
$msg = t('Unable to save credit card profile - @msg.', array(
'@msg' => $response['msg'],
));
}
drupal_set_message($msg, 'error');
return FALSE;
}
else {
$info += array(
'CardNum' => $pane_values['credit_card']['number'],
'ExpDate' => $pane_values['credit_card']['exp_month'] . substr($pane_values['credit_card']['exp_year'], 2, 2),
'NameOnCard' => $billing_data['name_on_card'],
'Street' => $billing_data['street'],
'Zip' => $billing_data['zip'],
'ExtData' => $billing_data['ext_data'],
);
$response = commerce_payleap_card_profile_request($payment_method, $info, 'Add');
// If the CreditCardInfo creation was a success, store the new card on
// file data locally.
if ($response['status']) {
// Build a remote ID that includes the Customer Profile ID and the new
// CreditCardInfo ID.
$info['CcInfoKey'] = (string) $response['xml']->CcInfoKey;
$new_card_data['remote_id'] = $info['CustomerKey'] . '|' . $info['CcInfoKey'];
// Save and log the creation of the new card on file.
commerce_cardonfile_data_save($new_card_data);
watchdog('commerce_payleap', 'Credit Card @card_id added to Customer Profile @profile_id for user @uid.', array(
'@card_id' => $info['CcInfoKey'],
'@profile_id' => $info['CustomerKey'],
'@uid' => $order->uid,
));
}
else {
// But if we could not find a Customer Profile, assume the existing
// Customer Profile ID we had is no longer valid and deactivate the card
// data that resulted in the error.
$card_data['status'] = 0;
commerce_cardonfile_data_save($card_data);
watchdog('commerce_payleap', 'Credit Card save error. Unable to save credit card for user @uid. @msg', array(
'@uid' => $order->uid,
'@msg' => $response['msg'],
));
return FALSE;
}
}
}
}
}
if (empty($info['CustomerKey']) || empty($info['CcInfoKey'])) {
// Fallback on Direct Transaction.
$payment_method['settings']['txn_payleap_type'] = PAYLEAP_TXN_TYPE_DIRECT_CAPTURE;
return commerce_payleap_direct_submit_form_submit($payment_method, $pane_form, $pane_values, $order, $charge);
}
// Send the tracking code.
$info['ExtData'] = '<CertifiedVendorId>' . PAYLEAP_COMMERCE_PARTNER_ID . '</CertifiedVendorId>';
$info += array(
'Amount' => commerce_currency_amount_to_decimal($charge['amount'], $charge['currency_code']),
);
return commerce_payleap_transaction_process($payment_method, $info, $order, $charge);
}