Paymill:如何在测试时模拟失败的支付?
Posted
技术标签:
【中文标题】Paymill:如何在测试时模拟失败的支付?【英文标题】:Paymill: How do I simulate a failed payment while testing? 【发布时间】:2012-12-08 04:40:56 【问题描述】:背景
使用Paymill's subscription billing 功能开发应用程序。 利用Ruby wrapper,我创建了一个PaymentProvider
类和规范,如下所示。
问题
如何使测试付款失败? (例如,卡被拒绝,或卡在未来的订阅付款中过期)
Stripe would let me do this using special card numbers,但 Paymill 似乎没有任何此类文档(英文)。
payment_provider.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
payment_provider_spec.rb
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
【问题讨论】:
【参考方案1】:截至今天,没有专门的信用卡号来模拟这些问题。但是,由于社区的需求,目前正在积压中实施。我建议向支持人员发送电子邮件,以表明对此功能的兴趣。请求越多,实现该功能的速度就越快。
编辑:PAYMILL 现在提供一个特殊的万事达卡号码,如果使用到期月份和年份的特定组合,该号码将失效。例如,如果到期日期为 02/2020,则卡 5105105105105100 将由于 RESPONSE_BACKEND_BLACKLISTED 而失败。
【讨论】:
到期日期和错误代码组合的完整列表可以在这里找到:developers.paymill.com/guides/reference/…【参考方案2】:您可以做的是使用字符串而不是数字作为验证值。
'account.verification' => 'abc'
据我所知,即使使用CONNECTOR_TEST
模式,这也会导致付款失败。
【讨论】:
以上是关于Paymill:如何在测试时模拟失败的支付?的主要内容,如果未能解决你的问题,请参考以下文章