offsite_payments icon indicating copy to clipboard operation
offsite_payments copied to clipboard

Please consider a trivial example of how to use this gem

Open abitdodgy opened this issue 10 years ago • 13 comments

Some examples on how this gem could be used would be extremely helpful. I would be happy to write them myself if someone can take a couple of minutes to familiarize me with how this gem is used.

abitdodgy avatar Aug 25 '14 17:08 abitdodgy

+1

groe avatar Aug 28 '14 10:08 groe

+1

madmike avatar Sep 02 '14 10:09 madmike

Well, i've managed with it, so here is a little example. To tell the truth some examples of using ActiveMerchant would fit the needs.

First of all, create a configuration (config/offsite_payments.yml) file with passwords and options for ur pay method. I use russian payment system robokassa:

robokassa:
  test_mode: false
  login: your_login
  password1: "pas1"
  password2: "pas2"

Then create an initializer (config/initializers/offsite_payments.rb):

require 'offsite_payments'
require 'offsite_payments/action_view_helper'

ActionView::Base.send(:include, OffsitePayments::ActionViewHelper)
OffsitePayments.mode = :test # for testing server
Rails.configuration.offsite_payments = YAML.load_file("#{Rails.root}/config/offsite_payments.yml")

Then you need to create a controller, which would get an incoming request from payment system to mark a payment as successful or not:

class RobokassaController < ApplicationController
  # Robokassa call this action after transaction
  def paid
    create_notification 'password2'

    if @notification.acknowledge # check if it’s genuine Robokassa request
      @order.approve! # project-specific code
      render text: @notification.success_response
    else
      head :bad_request
    end
  end


  def success
    create_notification 'password1'

    if @notification.acknowledge
      @order.approve!
      redirect_to user_home_path, notice: 'Successful payment'
    else
      render text: 'fraud'
    end
  end

  def fail
    redirect_to user_home_path, notice: 'Payment failed.'
  end

private

  def create_notification(password)
    @notification = OffsitePayments.integration(:robokassa).notification(request.raw_post, secret: Rails.configuration.offsite_payments['robokassa'][password])
    find_payment
  end

  def find_payment
    @order = Order.find(@notification.item_id)
  end
end

Then add some routes:

scope 'robokassa' do
  post 'paid'    => 'robokassa#paid',    as: :robokassa_paid
  post 'success' => 'robokassa#success', as: :robokassa_success
  post 'fail'    => 'robokassa#fail',    as: :robokassa_fail
end

and use a helper to create a pay button where you need it on your site:

<%= payment_service_for order.id, Rails.configuration.offsite_payments['robokassa']['login'],
           amount: @order.price,
           service: :robokassa,
           secret: Rails.configuration.offsite_payments['robokassa']['password1'] do |s| %>
  <%= submit_tag "Pay!", class: 'button blue', style: 'padding-bottom: 3px;' %>
<% end %>

Or if you work on for example backbone application, and need to render pay button in backbone template, you can pass to backbone model a payment signature, generated on a server side and use it in javascript helper:

def robokassa_signature
  OffsitePayments.integration(:robokassa).helper(
    id,
    Rails.configuration.offsite_payments['robokassa']['login'],
    amount: price,
    secret: Rails.configuration.offsite_payments['robokassa']['password1']
  ).generate_signature
end

Good luck!

madmike avatar Sep 09 '14 07:09 madmike

It's unfortunate that this requires Rails and doesn't work with Sinatra. Thanks for the example @madmike

pawel2105 avatar Sep 11 '14 07:09 pawel2105

It doesn't require Rails, you could use it with any framework you like, just write your own view helper using OffsitePayments.integration(:payment_system).helper method

madmike avatar Sep 11 '14 07:09 madmike

Interesting, I get this error when trying to fire up a sinatra app that requires offsite_payments:

undefined method `class_attribute' for ActiveMerchant::PostData:Class (NoMethodError)

EDIT: Oh this goes away when requiring active_merchant. This should probably also be documented.

pawel2105 avatar Sep 13 '14 09:09 pawel2105

@pawel2105 offsite_payments no longer depends on active_merchant, so that issue should be fixed now.

wvanbergen avatar Jan 19 '15 16:01 wvanbergen

thanks @madmike your example helped me in integrating payu_in.

vijay-meena avatar Apr 18 '15 14:04 vijay-meena

@wvanbergen @madmike offsite_payments requires ActiveSupport, and also actionpack, so yeah it still depends on a lot of Rails libraries.

lulalala avatar Apr 20 '15 08:04 lulalala

@madmike @lulalala As you can see the error happens on ActiveMerchant::PostData, i.e. ActiveMerchant not OffsitePayments. The link between these two libraries was severed a while ago, so if you see the class_attribute error while you are not using ActiveMerchant yourself directly, you're probably on an old version of OffsitePayments.

Both libraries still depend on ActiveSupport, and OffsitePayments depends on ActionPack as well.

wvanbergen avatar Apr 20 '15 12:04 wvanbergen

I generated form using payment_service_for and it successfully submits to payu_in gateway. Now I got a different requirement. I want to perform below validation on "submit" button

  1. want to validate that item is still available to buy
  2. want to save booking if validate success. After these two actions, automatically my form should submit to integration service.

Thus instead of generating html form using payment_service_for, is it possible to redirect_to gateway with params from rails controller itself ? There should be a method to achieve this task from the ruby code itself instead of html form post

vijay-meena avatar Apr 22 '15 17:04 vijay-meena

I wrote a small tutorial on my ongoing experience on building the integration: https://coderwall.com/p/dqed4a/how-to-create-a-payment-gateway-for-offsite_payments-active_merchant.

lakesare avatar Apr 10 '16 15:04 lakesare

This gem looks so promising - shame there is such a lack of information. Would love to use this for Sagepay but there doesn't seem to be any useful documentation at all. Even 2 years after the initial request there's nothing. Such a shame.

ums-uk avatar Nov 22 '16 18:11 ums-uk