0

私は現在、PayPal の Express Checkout と ActiveMerchant を通じて PayPal 支払いを受け入れる Ruby-on-Rails Web アプリケーションに取り組んでいます。顧客/買い手が検証済みまたは未検証の PayPal アカウントを使用して支払いを行ったかどうかを判断するための ActiveMerchant のサポートについていくつかの調査を行いましたが、役立つガイドを見つけることができませんでした.

また、ActiveMerchant は現在十分に文書化されていないため、まったく役に立ちません。

以下は、私のプロジェクトが現在使用している関連コードです。PaymentsController#purchase では、EXPRESS_GATEWAY.purchase メソッド呼び出しによって返されるオブジェクト#params['protection_eligibility']の メソッドと#params['protection_eligibility_type']メソッドを一時的に使用して、PayPal の顧客/購入者が検証済み/未検証の PayPal アカウントを持っているかどうかを評価しました。ActiveMerchant::Billing::PaypalExpressResponse後で、これがお客様の口座状況を知るための信頼できる根拠ではないことがわかりました。

Ruby-on-Rails の ActiveMerchant を使用して、または Rails で他の代替手段を使用して、PayPal の顧客/購入者が検証済み/未検証のアカウントを持っているかどうかを知るための知恵を誰かが教えてくれることを願っています。

# 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