2012-12-20 39 views
8

背景Paymill:如何在測試時模擬失敗的付款?

問題

如何進行測試付款失敗? (例如卡被拒,或卡在未來的訂閱費用到期)

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 = "[email protected]" 
     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 

回答

4

截至今天,沒有特殊的信用卡號碼,以模擬這些問題。但是,由於社區的要求,目前這個項目正在積壓落實。我建議發送電子郵件給支持人員,以表示對此功能的興趣。請求越多,功能實現的速度越快。

編輯:PAYMILL現在提供一個特殊的萬事達卡號碼,如果使用某個過期月份和年份的組合,將會失敗。例如,如果過期日期發送爲02/2020,則卡5105105105105100將由於RESPONSE_BACKEND_BLACKLISTED而失敗。

+0

可在此處找到過期日期和錯誤代碼組合的完整列表:https://developers.paymill.com/guides/reference/testing#how-do-i-test-credit-card-specific-error -codes- – LeEnno

0

你可以做的是使用字符串而不是數字作爲驗證值。

'account.verification' => 'abc' 

這將作爲在使用CONNECTOR_TEST模式,即使一個失敗的支付我的知識的結果。