验证用户的API凭据 - Rails,ActiveMerchant和PayPal Express Gateway

时间:2009-02-03 21:40:47

标签: ruby-on-rails e-commerce paypal activemerchant

我正在构建一个使用PayPal Express的市场应用程序。我有一个表单供卖家输入他们的PayPal API凭证,但我需要一种方法来通过对PayPal进行某种调用来验证它们。

我在ActiveMerchant中使用PaypalExpressGateway,除了标准的购买控件之外我什么都看不到。是否可以使用任何类型的空操作?

非常感谢任何帮助!

7 个答案:

答案 0 :(得分:2)

我正在使用TransactionSearch操作来实现此目的。通过指定STARTDATE=2100-01-01 00:00:00,它基本上会导致无操作。

它将为您验证凭据,而无需卖家提供任何其他输入。

答案 1 :(得分:1)

我个人没有答案。但我知道Railscasts.com的Ryan Bates最近专门为ActiveMerchant和Paypal投了六集(!)剧集。查看railscasts.com上的#141到#146集。

答案 2 :(得分:1)

出于安全原因,无法检查电子邮件是否为有效的PayPal帐户。如果确实需要帐户有效性,您可以随时进行小额交易,然后将其取消。

答案 3 :(得分:1)

好的,4个小时后......

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    class PaypalExpressGateway < Gateway

      def get_balance(options = {})
        commit 'GetBalance', build_get_balance_request(options)
      end

    private
      def build_get_balance_request(options)
        xml = Builder::XmlMarkup.new :indent => 2
        xml.tag! 'GetBalanceReq', 'xmlns' => PAYPAL_NAMESPACE do
          xml.tag! 'GetBalanceRequest', 'xmlns:n2' => EBAY_NAMESPACE do
            xml.tag! 'n2:Version', API_VERSION
            xml.tag! 'n2:ReturnAllCurrencies', '1'
          end
        end

        xml.target!
      end
    end
  end
end

class SellerMerchantValidator < ActiveModel::Validator
  def validate(record)
    paypal_attrs = ['paypal_api_username', 'paypal_api_password', 'paypal_api_signature']
    if record.paypal_merchant? && (record.changed - paypal_attrs).size < record.changed.size # one of paypal_attrs changed
      response = record.gateway.get_balance
      unless response.params['balance'].present?
        record.errors[:base] << "Please check the PayPal details and make sure all three are entered correctly."
      end
    end
  end
end

感谢Neil的检查交易搜索的想法。

如果有更好的方法可以检查api字段是否发生变化,请告诉我。

答案 4 :(得分:1)

API中还有一个GetBalance调用。 Some sample code

看起来像最简单(也是最快?)的方式。

答案 5 :(得分:0)

PayPal确实有AddressVerify API。它确认邮政地址和邮政编码是否与指定的PayPal帐户持有人匹配。事实上,我现在正在我们的网站上实施它。

你可以在这里阅读更多相关信息:
https://www.x.com/docs/DOC-1162#id0862M0QH02L

在这里:
https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_AddressVerify

答案 6 :(得分:0)

是的,如果您想使用ActiveMerchant测试用户的凭据,请在网关上使用transaction_search方法

https://github.com/Shopify/active_merchant/blob/cb72e0f9c58f57b1293e6e976229b26cfbfee6a8/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb

此示例将返回成功(确保填写您的测试凭据)

@username = ''
@password = ''
@signature = ''
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(
  login:      @username,
  password:   @password,
  signature:  @signature,
  test:       true
)

gateway.transaction_search({start_date: DateTime.now})
相关问题