'PX Access payment complete', 'page callback' => 'uc_dps_pxaccess_callback_process', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK ); $items[PXACCESS_FAIL_RETURN_URL] = array( 'title' => 'PX Access payment failed', 'page callback' => 'uc_dps_pxaccess_callback_process', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK ); return $items; } /** * Implementation of hook_theme() */ function uc_dps_pxaccess_theme() { return array( 'uc_dps_pxaccess_form_alter_buttonset_prefix' => array( 'arguments' => array(), 'file' => 'uc_dps_pxaccess.module', ), 'uc_dps_pxaccess_form_alter_buttonset_suffix' => array( 'arguments' => array(), 'file' => 'uc_dps_pxaccess.module', ), 'uc_dps_pxaccess_returnpage_output_error' => array( 'arguments' => array( 'result' => NULL, 'txn' => NULL, ), 'file' => 'uc_dps_pxaccess.module', ), 'uc_dps_pxaccess_returnpage_output_success' => array( 'arguments' => array( 'result' => NULL, 'txn' => NULL, ), 'file' => 'uc_dps_pxaccess.module', ), 'uc_dps_pxaccess_returnpage_output_declined' => array( 'arguments' => array( 'result' => NULL, 'txn' => NULL, ), 'file' => 'uc_dps_pxaccess.module', ), ) ; } /** * Implementation of hook_perm(). */ function uc_dps_pxaccess_perm() { return array('administer dps pxaccess'); } /** * Configuration for uc_dps_pxaccess on the Payment gateways form. */ function uc_dps_pxaccess_settings_form() { $form = array(); $form['uc_dps_pxaccess']['uc_dps_pxaccess_userid'] = array( '#type' => 'textfield', '#title' => t('PX Access User Id'), '#default_value' => variable_get('uc_dps_pxaccess_userid', ''), '#size' => 70, '#maxlength' => 70, '#desciption' => t('PX Access User Id that was issued by Payment Express'), '#required' => true ); $form['uc_dps_pxaccess']['uc_dps_pxaccess_key'] = array( '#type' => 'textfield', '#title' => t('PX Access Key'), '#default_value' => variable_get('uc_dps_pxaccess_key', ''), '#size' => 70, '#maxlength' => 70, '#desciption' => t('PX Access Key that was issued by Payment Express'), '#required' => true ); $form['uc_dps_pxaccess']['uc_dps_pxaccess_mac_key'] = array( '#type' => 'textfield', '#title' => t('Mac Key'), '#default_value' => variable_get('uc_dps_pxaccess_mac_key', ''), '#size' => 70, '#maxlength' => 70, '#desciption' => t('Mac Key that was issued by Payment Express'), '#required' => true ); $form['uc_dps_pxaccess']['uc_dps_pxaccess_server'] = array( '#type' => 'textfield', '#title' => t('PX Access URL'), '#default_value' => variable_get('uc_dps_pxaccess_server', ''), '#size' => 70, '#maxlength' => 70, '#desciption' => t('Server url that was issued by Payment Express'), '#required' => true ); return $form; } /** * Replace submit button to dps pxaccess payment button on the review order form */ function uc_dps_pxaccess_form(&$form_state, $order) { $form['submit'] = array( '#type' => 'submit', '#value' => variable_get('uc_dps_pxaccess_checkout_button', t('Submit')), ); return $form; } /** * Implementation of hook_form_alter(). * * We alter the checkout review form to capture submission via the DPS submit button, * and redirect the order to DPS PxAccess for transacting. */ function uc_dps_pxaccess_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) { $order = uc_order_load($order_id); if ($order->payment_method == 'uc_dps_pxaccess') { $form['submit'] = array( '#type' => 'submit', '#value' => variable_get('uc_dps_pxaccess_checkout_button', t('Submit')), '#submit' => array( 'uc_dps_pxaccess_submit_form_submit' ), ); /* Grab a theme from Line.321 */ // $form['#prefix'] = theme("uc_dps_pxaccess_form_alter_buttonset_prefix"); // $form['#suffix'] = theme("uc_dps_pxaccess_form_alter_buttonset_suffix"); } } /* When uc_dps_pxaccess form exist */ if ($form_id == 'uc_dps_pxaccess_form') { $form['#submit'] = (array)$form['#submit'] + array('uc_dps_pxaccess_submit_form_submit' => array() ); } } /** * Handle submission of the transaction confirmation form. */ function uc_dps_pxaccess_submit_form_submit($form, &$form_state) { $order = uc_order_load($_SESSION['cart_order']); uc_dps_pxaccess_goto($order); } /** * Redirect to the DPS PXAccess site for transaction * * Robby Ahn 25/06/2008, 8:55 a.m. */ function uc_dps_pxaccess_goto($order) { global $user; module_load_include('inc', 'uc_dps_pxaccess', 'pxaccess'); $pxaccess = new PxAccess(variable_get('uc_dps_pxaccess_server', ''), variable_get('uc_dps_pxaccess_userid', ''), variable_get('uc_dps_pxaccess_key', ''), variable_get('uc_dps_pxaccess_mac_key', '')); $request = new PxPayRequest(); $item_name_args = array( '%site_name' => variable_get('site_name', 'drupal'), '%order_id' => $order->order_id, '%user_name' => $user->name ) ; $item_name = ($user->uid) ? t("%site_name order #%order_id for %user_name", $item_name_args): t("%site_name order #%order_id", $item_name_args); $item_name = strip_tags($item_name); $name = ucfirst($order->billing_first_name) .' '. ucfirst($order->billing_last_name); if ($order->billing_company) { $address = $order->billing_company .', '; } $address .= $order->billing_street1; if ( $order->billing_street2 ) { $address .= ', '. $order->billing_street2; } $address .= ', '. $order->billing_city; $billing_country = uc_get_country_data(array('country_id' => $order->billing_country)); $address .= ', '. $billing_country[0]['country_name']; // Set up PxPayRequest Object $request->setAmountInput($order->order_total); $request->setTxnData1($item_name); /* whatever you want to appear : optional field */ $request->setTxnData2($name); /* whatever you want to appear : Name */ $request->setTxnData3($address); /* whatever you want to appear : Address */ $request->setTxnType("Purchase"); $request->setInputCurrency(variable_get('uc_currency_code', 'NZD')); $request->setMerchantReference($order->order_id); /* Fill this with your Order number (Reference number)*/ $request->setEmailAddress($order->primary_email); $request->setUrlFail(url(PXACCESS_FAIL_RETURN_URL, array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE))); $request->setUrlSuccess(url(PXACCESS_SUCCESS_RETURN_URL, array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE))); // Call makeResponse of PxAccess object to obtain the 3-DES encrypted payment request $request_string = $pxaccess->makeRequest($request); drupal_goto($request_string); } /** * Implementation of hook_payment_method(). */ function uc_dps_pxaccess_payment_method() { $path = base_path() . drupal_get_path('module', 'uc_credit'); $title1 = '' . t('DPS Direct payment system - PXAccess'); $title2 = '
' . t('Includes: '); //$cc_types = array('visa', 'mastercard', 'discover', 'amex', 'echeck'); $cc_types = array('visa', 'mastercard'); foreach ($cc_types as $type) { $title2 .= ' '; } $methods[] = array( 'id' => 'uc_dps_pxaccess', 'name' => t('DPS PXAccess'), 'title' => $title1 . $title2, 'review' => t('Credit Card via PaymentExpress'), 'desc' => t('Redirect users to submit payments through dps.'), 'callback' => 'uc_payment_method_uc_dps_pxaccess', 'weight' => 1, 'checkout' => FALSE, 'no_gateway' => TRUE, ); return $methods; } /* * Implementation of Payment_method */ function uc_payment_method_uc_dps_pxaccess($op, &$arg1) { switch ($op) { case 'order-view': return "A Transaction" ; break; case 'settings': $form['uc_dps_pxaccess_checkout_button'] = array( '#type' => 'textfield', '#title' => t('Submit button text on the Order Review'), '#description' => t('Provide DPS Pxaccess.'), '#default_value' => variable_get('uc_dps_pxaccess_checkout_button', t('Submit')), /* You can modify the name of button (e.g. Pay by dps )*/ ); return $form; } } /** * Capture the details when viewer is redirected back from DPS transaction site. */ function uc_dps_pxaccess_callback_process() { // @TODO Run a reverse DNS lookup on the IP address the callback is coming // from and verify it is from Payment Express $ip = ip_address(); $fullhost = gethostbyaddr($ip); $host = preg_replace("/^[^.]+./", "*.", $fullhost); if (isset($_REQUEST["result"])) { module_load_include('inc', 'uc_dps_pxaccess', 'pxaccess'); $pxaccess = new PxAccess(variable_get('uc_dps_pxaccess_server', ''), variable_get('uc_dps_pxaccess_userid', ''), variable_get('uc_dps_pxaccess_key', ''), variable_get('uc_dps_pxaccess_mac_key', '')); $enc_hex = $_REQUEST["result"]; // getResponse method in PxAccess object returns PxPayResponse object // which encapsulates all the response data $rsp = $pxaccess->getResponse($enc_hex); // the following are the fields available in the PxPayResponse object $result->success = $rsp->getSuccess(); // =1 when request succeeds $result->retry = $rsp->getRetry(); // =1 when a retry might help $result->statusrequired = $rsp->getStatusRequired(); // =1 when transaction "lost" $result->amountsettlement = $rsp->getAmountSettlement(); $result->authcode = $rsp->getAuthCode(); // from bank $result->cardname = $rsp->getCardName(); // e.g. "Visa" $result->dpstxnref = $rsp->getDpsTxnRef(); // the following values are returned, but are from the original request $result->txntype = $rsp->getTxnType(); $result->txndata1 = $rsp->getTxnData1(); $result->txndata2 = $rsp->getTxnData2(); $result->txndata3 = $rsp->getTxnData3(); $result->currencyinput = $rsp->getCurrencyInput(); $result->emailaddress = $rsp->getEmailAddress(); $result->txnid = $rsp->getMerchantReference(); // load the store transaction if ( $result->txnid ) { $txn = uc_order_load($result->txnid); if ( !$txn ) { drupal_not_found(); exit(); } } else { drupal_access_denied(); exit(); } $login = variable_get('uc_new_customer_login', FALSE ) ; if ($result->statusrequired == "1") { // pending uc_order_save($txn); uc_cart_complete_sale($txn, $login); uc_order_comment_save($txn->order_id, 0, t('A payment is pending.'), 'admin'); watchdog('uc_dps_pxaccess', 'An error occured during the processing!', WATCHDOG_ERROR); $output = theme("uc_dps_pxaccess_returnpage_output_error", $result, $txn); } elseif ($result->success == "1") { $comment = t('Order ID: @txn_id', array('@txn_id' => $txn->order_id)); // check if payment already received. $payments = uc_payment_load_payments($txn->order_id); if($payments){ foreach($payments as &$value){ if($value->data == $result->dpstxnref){ // payment recoreded already don't add this payment just show success page $output = theme("uc_dps_pxaccess_returnpage_output_success", $result, $txn); drupal_set_message( 'Your transaction was completed' ) ; return $output; } } } uc_payment_enter($txn->order_id, 'uc_dps_pxaccess', $txn->order_total, $order->uid, $result->dpstxnref, $comment); //uc_payment_enter($txn->order_id, 'uc_dps_pxaccess', $txn->order_total, $order->uid, NULL, $comment); uc_order_save($txn); uc_cart_complete_sale($txn, $login); uc_order_comment_save($txn->order_id, 0, t('A payment has been accepted.'), 'admin'); watchdog('uc_dps_pxaccess', 'Transaction was completed!', WATCHDOG_NOTICE ); $output = theme("uc_dps_pxaccess_returnpage_output_success", $result, $txn); drupal_set_message( 'Your transaction was completed' ) ; return $output ; } else{ uc_order_save($txn); //uc_cart_complete_sale($txn); uc_order_comment_save($txn->order_id, 0, t('A payment has been failed.'), 'admin'); watchdog('uc_dps_pxaccess', 'Transaction was failed!', WATCHDOG_ERROR); drupal_set_message(t('Your DPS payment has failed. Please contact us for assistance.')); $output = theme("uc_dps_pxaccess_returnpage_output_declined", $result, $txn); } } return $output; } /******************************************************************************* * Hook Functions (Ubercart) ******************************************************************************/ /** * Implementation of hook_payment_gateway(). */ function uc_dps_pxaccess_payment_gateway() { $gateways[] = array( 'id' => 'uc_dps_pxaccess', 'title' => t('DPS PxAccess'), 'description' => t('Process credit card payments using DPS on your site.'), 'settings' => 'uc_dps_pxaccess_settings_form', ); return $gateways; } /****************************************************************** * Theme functions ******************************************************************/ function theme_uc_dps_pxaccess_form_alter_buttonset_prefix() { $output = '
'; return $output; } function theme_uc_dps_pxaccess_form_alter_buttonset_suffix() { $output = ''. drupal_get_form('uc_dps_pxaccess_form', $order) .'
'; return $output; } // format the output when the transaction is declined function theme_uc_dps_pxaccess_returnpage_output_declined($result, $txn) { drupal_set_title('Payment Declined'); $output .= '
'. l(t('Go back to shopping cart'), 'cart') .'
'; return $output; } // format the output when the transaction is successful function theme_uc_dps_pxaccess_returnpage_output_success($result, $txn) { drupal_set_title('Payment has been completed'); // to theme the output here, you'd probably want to inspect the // order itself - eg a link to the download page if you sold a // downloadable item, or confirmation of shipping details if you // sold a shippable item ... $order = uc_order_load($result->txnid); // products array in $order->products $targs = array( '!link' => l('Order #'. $result->txnid .' details', 'user/'. $order->uid .'/order/'. $result->txnid), '!txntype' => $result->txntype, '!order_id' => $result->txnid, '!email' => $result->emailaddress, '!amount' => $result->currencyinput .' '. uc_currency_format($result->amountsettlement), ) ; // only link straight to the order if they're logged in now, // UC can be configured to do this automatically at checkout global $user ; if ( $user->uid != 0 ) { $items[] = t('!link', $targs); } else { $items[] = t('Order ID: !order_id', $targs); } $items[] = t('Transaction type: !txntype', $targs) ; $items[] = t('Email: !email', $targs); $items[] = t('Amount: !amount', $targs) ; $output = theme_item_list($items); $output .= '

'. l(t('Continue Shopping'), variable_get('uc_continue_shopping_url', '')) .'

'; return $output; } // format the output when the transaction is pending function theme_uc_dps_pxaccess_returnpage_output_error($result, $txn) { drupal_set_title('Payment Output Error during the processing'); $output = '

'. $result->txnid .'

'; $output .= '
'. l(t('Go back to Shopping Cart'), 'cart') .'
'; return $output; }