Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please consider a trivial example of how to use this gem #80

Open
abitdodgy opened this issue Aug 25, 2014 · 13 comments
Open

Please consider a trivial example of how to use this gem #80

abitdodgy opened this issue Aug 25, 2014 · 13 comments

Comments

@abitdodgy
Copy link

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.

@groe
Copy link

groe commented Aug 28, 2014

+1

1 similar comment
@madmike
Copy link

madmike commented Sep 2, 2014

+1

@madmike
Copy link

madmike commented Sep 9, 2014

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!

@pawel2105
Copy link

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

@madmike
Copy link

madmike commented Sep 11, 2014

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

@pawel2105
Copy link

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.

@wvanbergen
Copy link
Contributor

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

@vijay-meena
Copy link

thanks @madmike your example helped me in integrating payu_in.

@lulalala
Copy link
Contributor

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

@wvanbergen
Copy link
Contributor

@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.

@vijay-meena
Copy link

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

@lakesare
Copy link

lakesare commented Apr 10, 2016

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.

@ums-uk
Copy link

ums-uk commented Nov 22, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants