Ruby Paymill:如何在测试时模拟失败的付款?

Ruby Paymill:如何在测试时模拟失败的付款?,ruby,payment-gateway,payment,stripe-payments,paymill,Ruby,Payment Gateway,Payment,Stripe Payments,Paymill,背景 使用该功能开发应用程序 利用,我创建了一个PaymentProvider类和规范,如下所示 问题 如何使测试付款失败?(例如,卡被拒绝,或在未来的订阅付款中卡过期) 但Paymill似乎没有任何此类文档(英文) 支付提供商.rb class PaymentProvider Paymill.api_key = ENV['PAYMILL_PRIVATE_KEY'] def self.start_new_subscription(email, description, token

背景

  • 使用该功能开发应用程序
  • 利用,我创建了一个
    PaymentProvider
    类和规范,如下所示
问题

如何使测试付款失败?(例如,卡被拒绝,或在未来的订阅付款中卡过期)

但Paymill似乎没有任何此类文档(英文)


支付提供商.rb

class PaymentProvider
  Paymill.api_key = ENV['PAYMILL_PRIVATE_KEY']

  def self.start_new_subscription(email, description, token)
    offer = Paymill::Offer.find(ENV['PAYMILL_OFFER_ID'])
    client = Paymill::Client.create(email: email, description: description)
    payment = Paymill::Payment.create(token: token, client: client.id)
    subscription = Paymill::Subscription.create(client: client.id, offer: offer.id, payment: payment.id)
    subscription.id
  end
end
require 'spec_helper'

describe PaymentProvider do

  describe "#start_new_subscription" do
    it "returns a subscription id, starting 'sub_' when successful" do
      email = "mike@mike.com"
      description = "me"
      token = get_payment_token
      subscription_id = PaymentProvider.start_new_subscription(email, description, token)
      expect(subscription_id[0,4]).to eq('sub_')
    end
  end

  def get_payment_token
    # Simulate the JavaScript bridge we would use in production
    params = {
      'transaction.mode'        => 'CONNECTOR_TEST',
      'channel.id'              => ENV['PAYMILL_PUBLIC_KEY'],
      'jsonPFunction'           => 'any_string',
      'account.number'          => '5500000000000004',
      'account.expiry.month'    => 3.years.from_now.month,
      'account.expiry.year'     => 3.years.from_now.year,
      'account.verification'    => '111'
      #'presentation.amount3D'   => BigDecimal('10.00'),
      #'presentation.currency3D' => 'GBP'
    }
    http = Net::HTTP.new('test-token.paymill.de', 443)
    http.use_ssl = true
    response = http.get url_query_string(params)
    response.body.scan(/tok_\w*\b/).first # Use a regex to pull the token from the (not-quite-JSON) response
  end

  def url_query_string(hash)
    "/?" << URI.escape(hash.collect{|k,v| "#{k}=#{v}"}.join('&'))
  end

end

支付提供商\u规范rb

class PaymentProvider
  Paymill.api_key = ENV['PAYMILL_PRIVATE_KEY']

  def self.start_new_subscription(email, description, token)
    offer = Paymill::Offer.find(ENV['PAYMILL_OFFER_ID'])
    client = Paymill::Client.create(email: email, description: description)
    payment = Paymill::Payment.create(token: token, client: client.id)
    subscription = Paymill::Subscription.create(client: client.id, offer: offer.id, payment: payment.id)
    subscription.id
  end
end
require 'spec_helper'

describe PaymentProvider do

  describe "#start_new_subscription" do
    it "returns a subscription id, starting 'sub_' when successful" do
      email = "mike@mike.com"
      description = "me"
      token = get_payment_token
      subscription_id = PaymentProvider.start_new_subscription(email, description, token)
      expect(subscription_id[0,4]).to eq('sub_')
    end
  end

  def get_payment_token
    # Simulate the JavaScript bridge we would use in production
    params = {
      'transaction.mode'        => 'CONNECTOR_TEST',
      'channel.id'              => ENV['PAYMILL_PUBLIC_KEY'],
      'jsonPFunction'           => 'any_string',
      'account.number'          => '5500000000000004',
      'account.expiry.month'    => 3.years.from_now.month,
      'account.expiry.year'     => 3.years.from_now.year,
      'account.verification'    => '111'
      #'presentation.amount3D'   => BigDecimal('10.00'),
      #'presentation.currency3D' => 'GBP'
    }
    http = Net::HTTP.new('test-token.paymill.de', 443)
    http.use_ssl = true
    response = http.get url_query_string(params)
    response.body.scan(/tok_\w*\b/).first # Use a regex to pull the token from the (not-quite-JSON) response
  end

  def url_query_string(hash)
    "/?" << URI.escape(hash.collect{|k,v| "#{k}=#{v}"}.join('&'))
  end

end
require'spec\u helper'
请描述付款提供者做什么
描述“#开始#新#订阅”做什么
它“返回一个订阅id,成功时启动'sub_'”do
电子邮件=”mike@mike.com"
description=“我”
令牌=获取\付款\令牌
订阅\u id=PaymentProvider.start\u new\u订阅(电子邮件、说明、令牌)
预期(订阅id[0,4])到eq('sub_uu'))
结束
结束
def获取支付令牌
#模拟我们将在生产中使用的JavaScript桥
参数={
'事务处理模式'=>'连接器测试',
'channel.id'=>ENV['PAYMILL\u PUBLIC\u KEY'],
'jsonPFunction'=>'any_string',
'账号'=>'5500000000000004',
'帐户.到期日.月'=>3.5年.从现在开始.月,
'帐户.到期日.年份'=>3.5年。从现在起。年份,
'帐户验证'=>'111'
#'presentation.amount3D'=>BigDecimal('10.00'),
#'presentation.currency3D'=>'英镑'
}
http=Net::http.new('test-token.paymill.de',443)
http.use_ssl=true
response=http.get url\u query\u字符串(参数)
scan(/tok\uw*\b/)。首先,使用正则表达式从(不完全是JSON)响应中提取令牌
结束
def url_查询_字符串(哈希)

“/?”到目前为止,还没有专门的信用卡号来模拟这些问题。然而,由于社区的要求,这项工作目前正在积压待办事项中执行。我建议向支持人员发送一封电子邮件,表示对该功能的兴趣。请求越多,功能实现的速度就越快


编辑:PAYMILL现在提供了一个特殊的万事达卡号,如果使用到期月份和年份的某种组合,该卡号将失败。例如,如果将到期日期发送为2020年2月,卡5100将因响应被列入黑名单而失败

您可以使用字符串而不是数字作为验证值

'account.verification'    => 'abc'

据我所知,即使使用
连接器测试
模式,这也会导致付款失败。

过期日期和错误代码的完整组合列表可在此处找到: