Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paymill: How do I simulate a failed payment while testing?

Background

  • Developing an app using Paymill's subscription billing feature.
  • Making use of the Ruby wrapper, I have created a PaymentProvider class and spec as below.

Question

How do I make a test payment fail? (e.g. card is declined, or card expired in future subscription payments)

Stripe would let me do this using special card numbers but there doesn't appear to be any such documentation (in English) for 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
like image 864
Mike Avatar asked Dec 20 '12 15:12

Mike


People also ask

How do I test failed payments Stripe?

Test payment failures To test the effects of payment failure on an active subscription, attach the 4000 0000 0000 0341 card as the customer's default payment method, but use a trial period to defer the attempt (a trial of a few seconds or minutes is sufficient).

What does this mean your card was declined your request was in test mode but used a non test card?

Your request was in test mode, but used a non test (live) card. Cause: Your account is connected to Stripe in test mode. Solution: Go to the 'Payment Options' page and switch the Gateway Mode from "Test" to "Live". If the mode is already on Live, you will still need to re-connect your Stripe account.

How do you use Stripe in test mode?

To begin, you'll need to log in to your Stripe account. Then click on Payments in the menu at the top of the screen. Next, near the top right corner of the screen, toggle on the Test Mode option. This will show you an overview of the test payments you've received in your Stripe account.

What is test payment?

Test Payment method allows merchants to check how the payment process works without making real payments. You can find this method in Payment Systems section of your Project.


1 Answers

As of today, there are no special credit card numbers to simulate those problems. However, due to demands from the community, this is currently in the backlog for being implemented. I'd suggest to send an email to the support, to show interest on this feature. The more requests, the faster the feature will be implemented.

EDIT: PAYMILL now offers a special MasterCard number, which will fail if a certain combination of expiration month and year is used. For instance, the card 5105105105105100 will fail due to RESPONSE_BACKEND_BLACKLISTED if the expiration date is sent as 02/2020.

like image 135
jpkrohling Avatar answered Sep 20 '22 04:09

jpkrohling