ruby-on-rails - ActiveMerchant 支持确定 PayPal Express Checkout 客户/买家的帐户状态(已验证/未验证)

标签 ruby-on-rails paypal activemerchant

我目前正在开发一个 Ruby-on-Rails 网络应用程序,它通过 PayPal 的 Express Checkout 和 ActiveMerchant 接受 PayPal 付款。我对 ActiveMerchant 支持确定客户/买家是否使用经过验证或未经验证的 PayPal 帐户进行了多项研究,但我没有找到有用的指南。

我还发现 ActiveMerchant 目前没有很好的文档记录,所以它根本没有帮助。

下面是我的项目目前正在使用的相关代码。在 PaymentsController#purchase 上,我临时使用了 ActiveMerchant::Billing 的 #params['protection_eligibility']#params['protection_eligibility_type'] 方法: :PaypalExpressResponse 由 EXPRESS_GATEWAY.purchase 方法调用返回的对象,用于评估 PayPal 客户/买家是否拥有已验证/未验证的 PayPal 帐户。后来我发现这并不是了解客户账户状态的可靠依据。

我希望有人能告诉我 PayPal 客户/买家是否有使用 Ruby-on-Rails 的 ActiveMerchant 或使用 Rails 上的其他替代方案的已验证/未验证帐户的智慧。

# config/environments/development.rb
config.after_initialize do
  ActiveMerchant::Billing::Base.mode = :test
  paypal_options = {
      # credentials removed for this StackOverflow question
      :login => "",
      :password => "",
      :signature => ""
  }
  ::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
end

# app/models/payment.rb
class Payment < ActiveRecord::Base
  # ...
  PAYPAL_CREDIT_TO_PRICE = {
      # prices in cents(US)
      1  =>  75_00,
      4  => 200_00,
      12 => 550_00
  }
  STATUSES  = ["pending", "complete", "failed"]
  TYPES     = ["paypal", "paypal-verified", "paypal-unverified", "wiretransfer", "creditcard"]
  # ...
end

# app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
  # ...
  def checkout
    session[:credits_qty] = params[:credit_qty].to_i

    total_as_cents = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
    setup_purchase_params = {
        :allow_guest_checkout => true,
        :ip => request.remote_ip,
        :return_url => url_for(:action => 'purchase', :only_path => false),
        :cancel_return_url => url_for(:controller => 'payments', :action => 'new', :only_path => false),
        :items => [{
                       :name => pluralize(session[:credits_qty], "Credit"),
                       :number => 1,
                       :quantity => 1,
                       :amount => Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
                   }]
    }

    setup_response = EXPRESS_GATEWAY.setup_purchase(total_as_cents, setup_purchase_params)
    redirect_to EXPRESS_GATEWAY.redirect_url_for(setup_response.token)
  end

  def purchase
    if params[:token].nil? or params[:PayerID].nil?
      redirect_to new_payment_url, :notice => I18n.t('flash.payment.paypal.error')
      return
    end

    total_as_cents = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
    purchase_params = {
        :ip => request.remote_ip,
        :token => params[:token],
        :payer_id => params[:PayerID],
        :items =>  [{
                        :name => pluralize(session[:credits_qty], "Credit"),
                        :number => 1,
                        :quantity => 1,
                        :amount => Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
                    }]
    }

    purchase = EXPRESS_GATEWAY.purchase total_as_cents, purchase_params
    if purchase.success?
      payment = current_user.payments.new
      payment.paypal_params = params
      payment.credits = session[:credits_qty]
      payment.amount  = Payment::PAYPAL_CREDIT_TO_PRICE[session[:credits_qty]]
      payment.currency = "USD"
      payment.status = "complete"
      if(purchase.params["receipt_id"] && (purchase.params["success_page_redirect_requested"] == "true"))
        payment.payment_type = "creditcard"
      else
        if purchase.params['protection_eligibility'] == 'Eligible' && purchase.params['protection_eligibility_type'] == 'ItemNotReceivedEligible,UnauthorizedPaymentEligible'
          payment.payment_type = 'paypal-verified'
        else
          payment.payment_type = 'paypal-unverified'
        end
      end
      payment.ip_address = request.remote_ip.to_s
      payment.save!

      SiteMailer.paypal_payment(payment).deliver
      SiteMailer.notice_payment(payment).deliver

      notice = I18n.t('flash.payment.status.thanks')
      redirect_to profile_url, :notice => notice
    else
      notice = I18n.t('flash.payment.status.failed', :message => purchase.message)
      redirect_to new_payment_url, :notice => notice
    end
  end
  # ...
end

最佳答案

我使用 PayPal 的 paypal-sdk-merchant gem 来完成 ActiveMerchant 无法帮助我的工作(获取付款人账户的状态:已验证/未验证的 PayPal 账户。我遵循了 paypal-sdk-merchant 的安装在 https://github.com/paypal/merchant-sdk-ruby

  • gem 'paypal-sdk-merchant'
  • 捆绑安装
  • rails 生成 paypal:sdk:install

然后我设置了他们的样本 https://github.com/paypal/merchant-sdk-ruby#samples .设置示例后,转到您的 Rails 应用程序的/samples,您会发现许多代码生成器,可满足您从 PayPal 的 API 中需要的任何功能。不要忘记配置您的 config/paypal.yml。我配置了用户名、密码和签名(可以从您的 PayPal 沙盒中获取,特别是您的测试卖家帐户)并在我的案例中删除了 app_id。

我使用 PayPal 的示例 (/samples) 获得了以下用于获取 PayPal 付款人帐户状态(已验证/未验证)的代码,并将其放在我的应用程序模型的类方法中:

def self.get_paypal_payer_status(transaction_id)
  require 'paypal-sdk-merchant'
  api = PayPal::SDK::Merchant::API.new

  get_transaction_details = api.build_get_transaction_details({
    :Version => "94.0",
    :TransactionID => transaction_id
  })
  get_transaction_details_response = api.get_transaction_details(get_transaction_details)

  get_transaction_details_response.payment_transaction_details.payer_info.payer_status
end

关于ruby-on-rails - ActiveMerchant 支持确定 PayPal Express Checkout 客户/买家的帐户状态(已验证/未验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553933/

相关文章:

ruby-on-rails - RabbitMQ 3.5.6 无法与任何配置的主机建立 TCP 连接 (Bunny::TCPConnectionFailedForAllHosts)

paypal - 买方支付给卖方并保留佣金

ruby-on-rails - FactoryGirl 没有给出有用的验证错误

ruby-on-rails - Rails 与没有模型的表的关联

php - Paypal "Invalid Response"

ssl - PayPal ExpressCheckout 是否需要 SSL 证书

ruby-on-rails - 验证用户的 API 凭证 - Rails、ActiveMerchant 和 PayPal Express Gateway

ruby-on-rails-3 - 使用 Active Merchant 从 Authorize.net 获取 ARB 订阅信息?

ruby-on-rails - 如何在我的项目中包含 active_merchant 的 Raymond Law 分支?

ruby-on-rails - 在 rspec 中找不到 Font-awesome-rails 帮助程序