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

时间:2013-01-28 00:30:35

标签: ruby-on-rails paypal activemerchant

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

我发现ActiveMerchant目前还没有完整记录,所以它根本没有用。

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

我希望有人可以通过Ruby-on-Rails了解PayPal客户/买家是否拥有经过验证/未经验证的帐户。 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

2 个答案:

答案 0 :(得分:3)

我使用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

答案 1 :(得分:0)

我不熟悉ActiveMerchant所以我不能告诉您具体的内容,但是对于任何Express Checkout实现,您可以使用GetExpressCheckoutDetails获取付款人信息,其中包含PAYERSTATUS参数,其值为“已核实”或“未经核实”。

这是一个可选的调用,但我认为ActiveMerchant可能包含它,所以你只需要跟踪它并相应地拉出该参数。

或者,您可以使用Instant Payment Notification (IPN)来接收交易信息,您也可以在此处获得payer_status参数。