activemerchant / offsite_payments

A simple and unified API to handle dozens of different offsite payment pages.
MIT License
169 stars 201 forks source link

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

Open abitdodgy opened 10 years ago

abitdodgy commented 10 years ago

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 commented 10 years ago

+1

madmike commented 10 years ago

+1

madmike commented 10 years ago

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 commented 10 years ago

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

madmike commented 10 years ago

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 commented 10 years ago

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 commented 9 years ago

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

vijay-meena commented 9 years ago

thanks @madmike your example helped me in integrating payu_in.

lulalala commented 9 years ago

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

wvanbergen commented 9 years ago

@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 commented 9 years ago

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 commented 8 years ago

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 commented 7 years ago

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.