Paymill subscriptions in Ruby on Rails

Paymill is a Stripe clone available for those of us in Europe. There’s some controversy with the cloning side of things, but either way this is a long awaited opportunity for web businesses here in the Old Continent.

I created a simple rails SaaS site using paymill. You can check out the code on github, or keep reading for more details on using paymill with ruby and rails.

Paymill with ruby

We’ll be using the paymill-ruby gem (github), a ruby wrapper for the Paymill API. Right now it’s in a very early stage, but it works fine for the basics. To install it: gem install paymill.

The steps for accepting payments are very simple. First, create a paymill account and get the test API keys in “My Account -> Settings”. The public key is used to generate credit card tokens via paymill’s javascript bridge. For our testing, we’ll use a basic html form to get valid tokens:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript">
        var PAYMILL_PUBLIC_KEY = 'your_public_paymill_key';
    </script>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="https://bridge.paymill.com/"></script>
    <script type="text/javascript">
        $(document).ready(function () {
          function PaymillResponseHandler(error, result) {
              if (error) {
                  $(".payment-errors").text(error.apierror);
              } else {
                  $(".payment-errors").text("");
                  $(".payment-token").text("Token: "+result.token);
              }
          }
          $("#payment-form").submit(function (event) {
              PAYMILL_PUBLIC_KEY = $('.paymill-key').val();
              paymill.createToken({
                  number:$('.card-number').val(),
                  exp_month:$('.card-expiry-month').val(),
                  exp_year:$('.card-expiry-year').val(),
                  cvc:$('.card-cvc').val(),
                  cardholdername:$('.card-holdername').val(),
                  currency:'EUR',
                  amount:'100'
              }, PaymillResponseHandler);
              return false;
          });
          $('.paymill-key').val(PAYMILL_PUBLIC_KEY);
        });
    </script>
</head>
<body>
<h1>Paymill Tokens</h1>
<form id="payment-form" action="request.php" method="POST">
    <div class="form-row"><label>Paymill Public Key</label>
        <input class="paymill-key" type="text" size="45"/>
    </div>
    <div class="form-row"><label>Card number</label>
        <input class="card-number" type="text" size="20" value="4111111111111111"/>
    </div>
    <div class="form-row"><label>CVC</label>
        <input class="card-cvc" type="text" size="4" value="111"/>
    </div>
    <div class="form-row"><label>Cardholder name</label>
        <input class="card-holdername" type="text" size="20" value="John Doe"/>
    </div>
    <div class="form-row"><label>Expiration date (MM/YYYY)</label>
        <input class="card-expiry-month" type="text" size="2" value="12"/>
        <span> / </span>
        <input class="card-expiry-year" type="text" size="4" value="2015"/>
    </div>
    <button class="submit-button" type="submit">Get token</button>
</form>
<div class="payment-token" style="color:green;">Token:</div>
<div class="payment-errors" style="color:red;"></div>
</body>
</html>

Each time we submit the form paymill gives us a new token that we can use in the terminal to do our testing. Tokens are strings beginning with tok_ and are only valid for a single transaction.

Once we have the token supply we can now fire up irb and start testing the API. For example, making a 10€ transaction with the description “Testing paymill-ruby”:

require 'paymill'
Paymill.api_key = "your_private_paymill_key" # private key from paymill settings
Paymill::Transaction.create amount: 1000, currency:'EUR', token: 'tok_c05cfc4f1e1b8a7cf1f5', description: 'Testing paymill-ruby'

Hopefully the transaction will appear immediately in our paymill Dashboard. For more information about what is possible with the API and the gem, check out the Paymill API Reference and the paymill-ruby source code and documentation.

Subscriptions

The paymill control panel lets you create Offers, which are how Subscription Plans are called in paymill. Once created, the plans can be used to subscribe clients via the API. Offers have names beginning with offer_. These are the plan IDs that will be needed to setup the subscription with a client and credit card.

client = Paymill::Client.create email: '[email protected]', description: 'Subscription test'
payment = Paymill::Payment.create token: 'tok_2cf2314e1c619b588668', client: client.id
Paymill::Subscription.create offer: 'offer_20b8398c8d06e697bd1a', client: client.id, payment: payment.id

Rails app

Integrating paymill subscriptions in a rails app is simple. I created a sample app based on Railscast #288 Billing with stripe (code), but since the APIs are very similar it shouldn’t be difficult to convert some other stripe-based site.