逐步在 ruby​​onrails 上使用 paypal 创建一个简单的结帐

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

我目前正在尝试制作一个具有固定价格金额的 'Buy now' 按钮。

用户付款后,我想将他们重定向到根目录 url 并向他们发送一封附有 PDF 文件的电子邮件。

我一直在研究如何使用 paypal 创建一个简单的结账,但没有成功,我发现教程已经有几年的历史了,而且一些代码已被弃用。

我试过使用 BRAINTREE,它在 testing/sandbox 上运行良好,但我无法创建生产帐户,因为我目前住在波多黎各(这限制了我对支付网关的选择)。

到目前为止我做了什么

学习教程

我用 nameunit_price

products 创建了一个脚手架

在我的 product 模型中:

# 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

在教程中,他们说这应该足以处理付款,但他们要求单击 'Buy now' link,我不知道该指向何处或如何指向创建它。

如果没有什么要问的,有人可以在这里为我指明正确的方向吗(一步一步 -> 使用 paypal 轻松单一结账付款 -> 文档)。

万分感谢。

编辑:

能够创建 checkout 按钮:

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

现在它可以工作了,但是我该怎么做才能让您通过 notice 重定向回我的网站?

谢谢!

好的,经过一整天的研究和测试,我已经设法使几乎所有功能正常运行。这是我所做的

步骤 1

rails g scaffold product name:string unit_price:decimal

product 控制器:

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

然后创建您的第一个产品

步骤 2

在产品索引中,您可以像这样放置一个用于贝宝结账的按钮:

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

步骤 3

product 模型中

# This defines the paypal url for a given product sale
    def paypal_url(return_url, cancel_return) 
    values = { 
    :business => 'your_sandbox_facilitato_email@example.com',
       :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

您可以找到有关 HTML Variables for PayPal Payments Standard

的更多信息

在这段代码中,对我来说最重要的是:

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

: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

步骤 4

rails g controller PaymentNotification create

这里需要添加以下内容:

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

步骤 5

rails g model PaymentNotification

在此处添加以下内容

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

在路线中:

resources :payment_notification, only: [:create]

现在您应该可以通过 paypal 进行完整的处理付款了。

不要忘记在每次 scaffoldmodel 创建后 rake db:migrate

另外,为了获得自动重定向,你必须在paypal的沙盒中指定url。 Click here to know how

如果我忘记了什么,请告诉我,我已经工作了 10 多个小时才开始工作,哈哈