0

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

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

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

我希望有人能给我一个智慧,让我知道 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
4

2 回答 2

3

我使用 PayPal 的 paypal-sdk-merchant gem 来完成 ActiveMerchant 无法帮助我完成的工作(获取付款人帐户的状态:已验证/未验证的 PayPal 帐户。我在https 上安装了 paypal-sdk-merchant: //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
于 2013-03-01T20:49:52.840 回答
0

我对 ActiveMerchant 不熟悉,所以我无法具体告诉您,但是对于任何 Express Checkout 实施,您都可以使用GetExpressCheckoutDetails来获取付款人信息,其中包括一个值为“已验证”或“未验证”的 PAYERSTATUS 参数.

这是一个可选调用,但我认为 ActiveMerchant 可能包含它,因此您只需要跟踪它并相应地提取该参数。

或者,您可以使用即时付款通知 (IPN)接收交易信息,您也将在此处返回一个 payer_status 参数。

于 2013-01-28T02:22:09.077 回答