【问题标题】:Step by step to create a simple checkout using paypal on rubyonrails在 ruby​​onrails 上使用 paypal 逐步创建简单的结帐
【发布时间】:2015-11-06 16:18:18
【问题描述】:

我目前正在尝试制作一个价格固定的“立即购买”按钮。

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

我一直在研究如何使用 paypal 创建一个简单的结帐,但没有成功,我发现教程已有多年历史,其中一些代码已被弃用。

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

到目前为止我做了什么

按照教程进行操作

我用nameunit_priceproducts 创建了一个脚手架

在我的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

在教程中,他们说这应该足以处理付款,但他们要求点击“立即购买”链接,我不知道该指向哪里或如何创建它。

如果问的不多,有人可以在这里指出我正确的方向吗(逐步 -> 使用贝宝轻松单次结帐付款 -> 文档)。

谢谢一百万。

编辑:

能够创建checkout 按钮:

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

现在它可以工作了,但是我如何才能让你通过notice 重定向回我的网站?

谢谢!

【问题讨论】:

    标签: ruby-on-rails paypal paypal-sandbox


    【解决方案1】:

    好的,经过一整天的研究和测试,我已经设法让几乎所有东西都能正常工作。这就是我所做的

    第 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

    买家完成付款后,PayPal 将他们的浏览器重定向到的 URL。例如,在您的网站上指定一个显示“感谢您的付款”页面的 URL。

    :notify_url

    PayPal 以即时付款通知消息的形式发布付款信息的 URL。

    :cancel_return

    如果买家在完成付款前取消结帐,PayPal 会将他们的浏览器重定向到该 URL。例如,在您的网站上指定一个显示“已取消付款”页面的 URL。

    :rm

    返回方法。用于将数据发送到指定 URL 的 FORM METHOD 通过返回变量。允许的值为:

    0 - 所有购物车付款都使用 GET 方法

    1 – 买家的浏览器通过使用 GET 方法,但不包含付款变量

    2 – 买家的浏览器通过使用 POST方式,包含所有支付变量

    第 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]
    

    现在您应该可以通过贝宝完成付款了。

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

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

    如果我忘记了什么,请告诉我,我已经工作了 10 多个小时才可以正常工作,哈哈

    【讨论】:

    • 谢谢,但您忘记列出迁移中的字段。我可以从帖子中找出它们,但不是直接。
    猜你喜欢
    • 2011-04-19
    • 2020-12-06
    • 2021-03-30
    • 2011-12-21
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2015-10-25
    相关资源
    最近更新 更多