Johhan Santana
Johhan Santana

Reputation: 2425

Step by step to create a simple checkout using paypal on rubyonrails

I'm currently trying to make a 'Buy now' button with a fixed price amount.

After the user pays I want to redirect them to the root url and send them an email with an attached PDF file.

I've been researching how to create a simple checkout using paypal but with no success, I've found tutorials that are years old and some of the code is deprecated.

I've tried using BRAINTREE and it worked perfectly on testing/sandbox but I am unable to create a production account since I currently live in Puerto Rico(this limits my options for payment gateways).

What I've done so far

Following a tutorial

I've created a scaffold for products with name and unit_price

In my product model:

# This defines the paypal url for a given product sale
def paypal_url(return_url)
values = {
:business => YOUR_MERCHANT_EMAIL,
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => UNIQUE_INTEGER
}

values.merge!({
"amount_1" => unit_price,
"item_name_1" => name,
"item_number_1" => id,
"quantity_1" => '1'
})

# This is a paypal sandbox url and should be changed for production.
# Better define this url in the application configuration setting on environment
# basis.
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query

end

In the tutorial they said that this should be enough to be able to process the payment but they ask to click the 'Buy now' link which I have no idea where to point it or how to create it.

If it's not much to ask, could someone point me in the right direction here (step by step -> easy single checkout payment with paypal -> documentations).

Thanks a million.

EDIT:

Was able to create the checkout button:

<%= link_to 'checkout', product.paypal_url(products_url) %>

Now it works BUT how do I make it so you get redirected back to my website with a notice?

Thanks!

Upvotes: 1

Views: 1675

Answers (1)

Johhan Santana
Johhan Santana

Reputation: 2425

Ok so after a full day of research and testing, I've manage to get almost everything working. Here's what I've done

Step 1

rails g scaffold product name:string unit_price:decimal

product controller:

def index
    @products = Product.all
    if @products.length != 0
      @product = Product.find(1)
    end
  end

Then create your first product

Step 2

In the index for products you can put a button like this for a paypal checkout:

<%= link_to 'checkout', @product.paypal_url(payment_notification_index_url, root_url) %>

Step 3

in the product model

# This defines the paypal url for a given product sale
    def paypal_url(return_url, cancel_return) 
    values = { 
    :business => '[email protected]',
       :cmd => '_xclick',
    :upload => 1,
    :return => return_url,
    :rm => 2,
    # :notify_url => notify_url,
    :cancel_return => cancel_return
    }   
    values.merge!({ 
    "amount" => unit_price,
    "item_name" => name,
    "item_number" => id,
    "quantity" => '1'
    })
         # For test transactions use this URL
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end

has_many :payment_notifications

You can find more info about the HTML Variables for PayPal Payments Standard

In this code the most important ones for me are:

:return

The URL to which PayPal redirects buyers' browser after they complete their payments. For example, specify a URL on your site that displays a "Thank you for your payment" page.

:notify_url

The URL to which PayPal posts information about the payment, in the form of Instant Payment Notification messages.

:cancel_return

A URL to which PayPal redirects the buyers' browsers if they cancel checkout before completing their payments. For example, specify a URL on your website that displays a "Payment Canceled" page.

and

:rm

Return method. The FORM METHOD used to send data to the URL specified by the return variable. Allowable values are:

0 – all shopping cart payments use the GET method

1 – the buyer's browser is redirected to the return URL by using the GET method, but no payment variables are included

2 – the buyer's browser is redirected to the return URL by using the POST method, and all payment variables are included

Step 4

rails g controller PaymentNotification create

In here you need to add the following:

class PaymentNotificationController < ApplicationController
    protect_from_forgery except: [:create]

  def create
    # @payment = PaymentNotification.create!(params: params, product_id: params[:invoice], status: params[:payment_status], transaction_id: params[:txn_id] )
    @payment = PaymentNotification.create!(params: params, product_id: 1, status: params[:payment_status], transaction_id: params[:txn_id])
    # render nothing: true

    if @payment.status == 'Completed'
        redirect_to root_url, notice: 'Success!'
      else
        redirect_to root_url, notice: 'Error'
      end

  end
end

Step 5

rails g model PaymentNotification

in here add the following

class PaymentNotification < ActiveRecord::Base
  belongs_to :product
  serialize :params
  after_create :success_message

  private

  def success_message
    if status == "Completed"
      puts 'Completed'
      ...
    else
      puts 'error'
      ...
    end
  end
end

in routes:

resources :payment_notification, only: [:create]

And now you should be able to have a complete processing payment via paypal.

Don't forget to rake db:migrate after each scaffold and model creationg.

Another thing, in order to get the automatic redirect, you have to specify the url in the sandbox of paypal. Click here to know how

If I forgot something let me know, been working for more than 10 hours to get this working lol

Upvotes: 5

Related Questions