Joh*_*ana 1 paypal ruby-on-rails paypal-sandbox
我目前正在尝试以固定的价格制作“立即购买”按钮。
用户付款后,我想将它们重定向到根URL并向他们发送带有附件PDF文件的电子邮件。
我一直在研究如何使用Paypal创建简单的结帐,但是没有成功,我发现使用了多年的教程已经过时了,其中一些代码已被弃用。
我已经尝试过使用BRAINTREE,它在测试/沙盒中效果很好,但是由于我目前居住在波多黎各,所以我无法创建生产帐户(这限制了我在支付网关上的选择)。
到目前为止我做了什么
跟随教程
我创建了一个支架的products使用name和unit_price
在我的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
Run Code Online (Sandbox Code Playgroud)
他们在教程中说,这应该足以处理付款,但他们要求单击“立即购买”链接,但我不知道该指向何处或如何创建。
如果问的不多,有人可以在这里向我指出正确的方向(逐步操作->使用Paypal轻松进行一次结帐付款->文档)。
太感谢了。
编辑:
能够创建checkout按钮:
<%= link_to 'checkout', product.paypal_url(products_url) %>
现在它可以工作了,但是我该怎么做,以便您使用重定向到我的网站notice?
谢谢!
好了,经过一整天的研究和测试,我已经设法使几乎所有工作正常。这就是我所做的
步骤1
rails g scaffold product name:string unit_price:decimal
Run Code Online (Sandbox Code Playgroud)
product 控制器:
def index
@products = Product.all
if @products.length != 0
@product = Product.find(1)
end
end
Run Code Online (Sandbox Code Playgroud)
然后创建您的第一个产品
第2步
在产品索引中,您可以放置一个类似这样的按钮以进行贝宝结帐:
<%= link_to 'checkout', @product.paypal_url(payment_notification_index_url, root_url) %>
Run Code Online (Sandbox Code Playgroud)
第三步
在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
Run Code Online (Sandbox Code Playgroud)
您可以找到有关PayPal付款标准HTML变量的更多信息
在这段代码中,对我来说最重要的是:
:return
付款完成后,贝宝会将买家的浏览器重定向到的URL。例如,在您的站点上指定一个显示“谢谢您的付款”页面的URL。
:notify_url
PayPal以即时付款通知消息的形式向其发布有关付款信息的URL。
:cancel_return
如果买家在完成付款前取消结帐,贝宝将重定向到该URL的URL。例如,在您的网站上指定一个显示“付款已取消”页面的URL。
和
:rm
返回方法。用于将数据发送到由返回变量指定的URL的FORM METHOD。允许的值为:
0 –所有购物车付款都使用GET方法
1 –使用GET方法将买方的浏览器重定向到返回URL,但不包括付款变量
2 –使用POST方法将买方的浏览器重定向到返回URL,并且包括所有付款变量
第四步
rails g controller PaymentNotification create
Run Code Online (Sandbox Code Playgroud)
在这里,您需要添加以下内容:
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
Run Code Online (Sandbox Code Playgroud)
第5步
rails g model PaymentNotification
Run Code Online (Sandbox Code Playgroud)
在这里添加以下内容
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
Run Code Online (Sandbox Code Playgroud)
在路线上:
resources :payment_notification, only: [:create]
Run Code Online (Sandbox Code Playgroud)
现在,您应该可以通过贝宝(Paypal)完成付款。
不要忘了rake db:migrate后每个scaffold和modelcreationg。
另一件事,为了获得自动重定向,您必须在paypal的沙箱中指定URL。点击这里了解如何
如果我忘记了让我知道的事情,要工作10多个小时才能使它正常工作