Hi,

I am working on a contrib module to provide support for the SagePay form payment gateway which is of the redirected gateway type.
I am trying to understand how to call back to the Drupal Commerce site - ie what URL structure to use.

Originally, I created a new menu callback in my custom module, but on further investigation it seems there's a standard way of doing this.

What I think I need to do is set up the "success" and "failure" urls I send to the gateway so the following function processes the callback:

function commerce_payment_redirect_pane_checkout_form(&$form, &$form_state, $checkout_pane, $order) {
...

Following the logic here:

  // If the user came to the return URL...
  if (arg(3) == 'return' && arg(4) == $order->data['payment_redirect_key']) {
    // Check for a validate handler on return.
    $validate_callback = commerce_payment_method_callback($payment_method, 'redirect_form_validate');

I think that means I should set the return url to be :

$redirect_key = $order->data['payment_redirect_key'];
$success_url = $GLOBALS['base_url'] . '/checkout/commerce_sagepay/return/' . $redirect_key,

However, I when I do this, I get a 404 from the checkout_router function. Am I missing something or is there no menu entry to pick up the redirected callback yet?

Thanks!

Richard

CommentFileSizeAuthor
#1 commerce_sagepay.zip7.44 KBikos

Comments

ikos’s picture

StatusFileSize
new7.44 KB

Code attached for reference.

rszrama’s picture

Status: Active » Fixed

Sorry I didn't get back to you sooner, Richard. I think you've already figured it out, but yeah, the gist of it is that user initiated cancellations can come back to the cancel URL and the completed process can send them back to the return URL. At the return step, your module can perform a validation to determine if payment failed or succeeded. If it failed, the user will go back to the previous step in checkout. If it completed, they'll go forward.

Here's the code in my PayPal WPS module I use to construct my URLs:

    // Return to the previous page when payment is canceled
    'cancel_return' => url('checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),

    // Return to the payment redirect page for processing successful payments
    'return' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array('absolute' => TRUE)),

And here's my validate handler:

/**
 * Payment method callback: redirect form return validation.
 */
function commerce_paypal_wps_redirect_form_validate($order, $payment_method) {
  if (!empty($payment_method['settings']['ipn_logging']) &&
    $payment_method['settings']['ipn_logging'] == 'full_ipn') {
    watchdog('commerce_paypal_wps', 'Customer returned from PayPal with the following POST data:<pre>' . check_plain(print_r($_POST, TRUE)) . '</pre>', array(), WATCHDOG_NOTICE);
  }

  // This may be an unnecessary step, but if for some reason the user does end
  // up returning at the success URL with a Failed payment, go back.
  if (!empty($_POST['payment_status']) && $_POST['payment_status'] == 'Failed') {
    return FALSE;
  }
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.