Syndicode
Contact Us
Alexander Germashov

Ruby on Rails online payments with Adyen (Part II)

In the previous article we uncovered how to do Ruby on Rails online payments with Adyen using encrypted form without redirect of users to external checkout pages. Now it’s time for another type of integration: Hosted Payment Pages (HPP).

Why Ruby on Rails online payments with Adyen HPP

You can easily do Ruby on Rails online payments with Stripe, BrainTree, and many others. We have experience integrating these online payments to Ruby on Rails web app during just one day. However, competitive advantage of Adyen’s HPP is that in addition to PayPal and credit card, Adyen’s HPP can allow your Ruby on Rails direct bank payment, with such mathods as SEPA, SOFORT, iDEAL, etc.

HPP provides a secure, flexible and easy way for customers to purchase services and goods:

  • Customers go to your site, then they select and add the services/items to a shopping cart.
  • Next, they are redirected to the hosted payment page, where they enter the billing details to process the payment.
  • After submitting they are redirected back to your web site, where they can see a summary information displaying the result of the payment processing.
  • You can customise the look of the HPP using Adyen’s skin technology and toolset to create a seamless checkout for your customers.

Prepare account for using HPP

Login to the Adyen Customer Area, create a new skin and configure the allowed payment methods.

Generagte HMAC key to be used in your payment request’s signature and add a method to prepare your HPP URI:

def gs(i) # adyen merchant signature params escaping
  i.gsub(':', ':').gsub('', '\')
end

def hpp_uri(order)
  user = order.user
  merchant_reference = "your_prefix_" + user.id.to_s + "_" + order.id.to_s + "_" +  rand(99999999).to_s
  merchant_account = ENV
  payment_amount = (order.total*100).to_i.to_s
  currncy = order.currency
  session_validity = 1.day.from_now.strftime('%Y-%m-%dT%H:%M:%SZ')
  ship_before_date = order.shipment.date.strftime('%Y-%m-%d')

  # ---- merchant signature ----
  string = "currencyCode:merchantAccount:merchantReference:paymentAmount:recurringContract:sessionValidity:shipBeforeDate:shopperEmail:shopperLocale:shopperReference:skinCode"
  string += ':'+gs(currency)
  string += ':'+gs(merchant_account)
  string += ':'+gs(merchant_reference)
  string += ':'+gs(payment_amount)
  string += ':ONECLICK'
  string += ':'+gs(session_validity)
  string += ':'+gs(ship_before_date)
  string += ':'+gs(user.email)
  string += ':'+gs(I18n.locale.to_s)
  string += ':'+gs(user.shopper_reference)
  string += ':'+gs('YOUR_HPP_SKIN_CODE')

  hmac = Order.create_hmac(string)
  # ---- merchant signature ----

  uri = ENV # 'https://test.adyen.com/hpp/pay.shtml'
  uri += '?'
  uri += "merchantSig="+Rack::Utils.escape(hmac)
  uri += "&sessionValidity="+session_validity
  uri += "&shipBeforeDate="+ship_before_date
  uri += "&shopperLocale="+I18n.locale.to_s
  uri += "&merchantAccount="+merchant_account
  uri += "&paymentAmount="+payment_amount
  uri += "&currencyCode="+currency
  uri += "&skinCode="+'YOUR_HPP_SKIN_CODE'
  uri += "&merchantReference="+merchant_reference
  uri += "&shopperReference="+user.shopper_reference
  uri += "&recurringContract=ONECLICK"
  uri += "&shopperEmail="+user.email
end

Prepare your application to process HPP payments

Create a new endpoint in your Orders controller where user will be redirected after payment processing:

def payment_processed
  return redirect_to root_url, alert: 'Adyen request invalid' unless params

  order_id = params.split('_').to_i
  order = Order.find order_id

  if  .include?(params.downcase) ||
      Adyen.capture(params, (order.total*100).to_i, order.currency) != ''

    # process failed payment
    redirect_to order, alert: I18n.t('payment_failed')
  else
    # process success payment
    redirect_to order
  end
end

Add this endpoint to your Adyen’s HPP skin:

Снимок экрана от 2016-04-28 13:29:12

In your Orders controller #create action setup redirect to Adyen HPP:

def create
  order = Order.new(order_params)
  if order.save
    redirect_to Adyen.hpp_uri(order)
  else
    render :new
  end
end

And now you are ready for global online payments and EU direct bank payments!