简体   繁体   English

Rails 4 Paypal REST API集成

[英]Rails 4 Paypal REST API integration

I've installed the gem and GitHub says to do. 我已经安装了gem和GitHub所说的。 I've made a separate model for creating a payment part of the instructions but I'm not sure how to call making a payment from within my orders controller . 我已经创建了一个用于创建付款说明的单独模型,但是不确定如何从我的订单控制器中调用付款 Also I need to be able to set the Payment attributes dynamically from my order but I've tried a couple of different ways of trying to achieve this with no luck. 另外,我还需要能够从订单中动态设置“付款”属性,但是我尝试了几种不同的尝试来实现这一点,但是没有运气。

I've tried calling Payments.pay in my orders controller and I get an error saying the method pay doesn't exist but I do have the method in the Payments class. 我尝试在订单控制器中调用Payments.pay,但出现错误,提示不存在方法pay,但在Payments类中确实有该方法。 The partial code for the Payments class is: Payments类的部分代码为:

class Payments < PayPal::SDK::REST::Payment

def pay
  @payment = Payment.new({
      :intent => "sale",
      :payer => {
        :payment_method => "credit_card",
        :funding_instruments => [{
           :credit_card => {
           :type => "visa",
           :number => "4567516310777851",
           :expire_month => "11",
           :expire_year => "2018",
           :cvv2 => "874",
           :first_name => order.first_name,  # Need to do something similar to this but doesn't work 
           # etc...
    end
end  

To sum up what I need help with is how can I make a call to this method in another controller or model so the payment can be processed and also how can i set the fields dynamically? 总结一下,我需要帮助的是如何在另一个控制器或模型中调用此方法,以便可以处理付款以及如何动态设置字段?

I see a few potential issues. 我看到了一些潜在的问题。

First of all, you're using the method as a class method, so it should be 首先,您将方法用作类方法,因此应该

def self.pay end def self.pay结束

in order for Payments.pay to work. 为了使Payments.pay正常工作。 The way you define it currently is as an instance method. 当前定义它的方式是作为实例方法。

If you wanted to call this method in a controller and pass in field dynamically you'd need to edit the pay function to accept arguments. 如果要在控制器中调用此方法并动态传递字段,则需要编辑pay函数以接受参数。 And possible import this class in to the controller. 并可能将此类导入控制器。

You might just want to create a before_action :pay callback in one of your controllers and define the method / call Paypal SDK like below. 您可能只想在其中一个控制器中创建一个before_action:pay回调并定义方法/调用Paypal SDK,如下所示。 This would allow you to grab params and use them in the method (although I don't think you should be handling CC numbers, Paypal will probably handle this and pass you some sort of token that you then use). 这将允许您获取参数并在方法中使用它们(尽管我不认为您应该处理抄送号码,但Paypal可能会处理此事并传递给您某种您随后使用的令牌)。 Also look out for the way Paypal namespaces the library, it can be tricky when you're calling Paypal::SDK::REST etc. 还请注意Paypal命名空间库的方式,在调用Paypal :: SDK :: REST等时可能会很棘手。

class PaymentsController < ApplicationController
  before_action :pay

  def create
    @order = Order.find(params[:id])
  end 

  private

  def pay
    @payment = PayPal::SDK::REST::Payment.new({
      :intent => "sale",
      :payer => {
        :payment_method => "credit_card",
        :funding_instruments => [{
           :credit_card => {
           :type => "visa",
           :number => params[:number],
           :expire_month => params[:month],
           :expire_year => params[:year],
           :cvv2 => params[:cvv],
           :first_name => @order.first_name,  # Need to do something similar to this but doesn't work 
           # etc...
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM