Simplepay

A ruby gem for the easy integration of the Simplepay payment platform

Installation

Add this line to your application's Gemfile:

gem 'simplepayNG'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install simplepayNG

Basic Usage

How to get started

The HTML Form

  <style>
    body {
      background-color: #efefef;
    }
  </style>
  <div class="container">
    <div class="row">
      <div class="col-md-2"></div>
      <div class="col-md-8" style="background-color: #ffffff; margin: 20px; padding: 20px;">
        <%= simple_form_for('', url: pay_verify_path, html: {id: 'checkout_form'}) do |f| %>
            <%= f.input :sp_token, as: 'hidden', input_html: {id: 'sp_token'} %>
            <%= f.input :sp_amount, as: 'hidden', input_html: {id: 'sp_amount'} %>
            <%= f.input :sp_status, as: 'hidden', input_html: {id: 'sp_status'} %>
            <%= f.input :sp_currency, as: 'hidden', input_html: {id: 'sp_currency', value: 'NGN'} %>
            <%= f.input :transaction_id, as: 'hidden', input_html: {id: 'transaction_id'} %>
            <%= f.input :email, as: 'email', required: false, input_html: {id: 'email'}   %>
            <%= f.input :amount, input_html: {id: 'amount'}  %>
            <%= f.input :phone, required: false, input_html: {id: 'phone'}  %>
            <%= f.submit :submit, class: 'btn btn-primary btn-block', id: 'btn-checkout' %>
        <% end %>
      </div>
      <div class="col-md-2"></div>
    </div>
  </div>
  <script src="https://checkout.simplepay.ng/v2/simplepay.js"></script>
  <script type="text/javascript">
      // Use the "token" to validate the payment
      function processPayment(token, paid) {
          // put token, status, amount and transaction ID to be sent forward
          // token, status and amount need to be passed forward
          $('#sp_token').val(token);
          $('#sp_status').val(paid);
          $('#sp_amount').val(SimplePay.amountToLower($('#amount').val()));
          $('#transaction_id').val('1234TRID');
          $('#checkout_form').submit();
      }

      var handler = SimplePay.configure({
          token: processPayment,
          key: 'test_pu_demo'  // put you public key here
      });

      $('#btn-checkout').on('click', function (e) {
          e.preventDefault();
          handler.open(SimplePay.CHECKOUT,
            {
                email: '[email protected]',
                phone: '+23412345678',
                description: 'My Test Store Checkout 123-456',
                address: '31 Kade St, Abuja, Nigeria',
                postal_code: '110001',
                city: 'Abuja',
                country: 'NG',
                amount: SimplePay.amountToLower($('#amount').val()),
                currency: 'NGN'
            });
      });
  </script>

To start processing payments, first you'd have to declare your simplepay private keys, through

Environment Variables

  simple_pay_secret: "test_pr_demo"

OR

pass it on every request

For Example

    api_key = 'test_pr_demo'
    token = params['sp_token'] #Token Gotten From Form
    amount = params['sp_amount'] #Amount Gotten From Form
    amount_currency = params['sp_currency'] #Currency your willingg to accept
    status = params['sp_status'] #Payment status
    transaction_id = params['transaction_id'] #Transaction id Gotten From Form
    transaction = Simplepay.new(token, amount, amount_currency, status, api_key: api_key)

Accepting payments

Basic Example

    token = params['sp_token'] #Token Gotten From Form
    amount = params['sp_amount'] #Amount Gotten From Form
    amount_currency = params['sp_currency'] #Currency your willingg to accept
    status = params['sp_status'] #Payment status
    transaction_id = params['transaction_id'] #Transaction id Gotten From Form
    transaction = Simplepay.new(token, amount, amount_currency, status)

Full Example

  def verify
      token = params['sp_token']
      amount = params['sp_amount']
      amount_currency = params['sp_currency']
      status = params['sp_status']
      transaction_id = params['transaction_id']
      transaction = Simplepay.new(token, amount, amount_currency, status)
      if transaction['success'].eql? true
        respond_to do |format|
          format.html { redirect_to success_path, notice: transaction['message'] }
        end
      else
        respond_to do |format|
            format.html { redirect_to failed_path, alert: transaction['message'] }
        end
      end
    end

Transaction Response

If a transaction is successful the response it sends is the

    {response: res, message: 'Payment Completed Successfully', success: true}
  1. Response From Api
  { "response": {
    "id":"trans_iKdhkLxPZEaGoNodfWoEFW",
    "amount":110000,
    "currency":"NGN",
    "captured":true,
    "created":1455897064,
    "customer":{
        "id": "cus_NpX9DDh5qCP5FYyxobPJk8",
        "email": "[email protected]",
        "phone": "+23412345678",
        "address": "31 Kade St, Abuja, Nigeria",
        "address_city": "Nigeria",
        "address_country": "NG",
        "address_state": null,
        "address_postal": "110001"
    },
    "payment_reference": "56297034526707",
    "livemode":true,
    "source":{
        "id":"card_xcgz3en8iyNqjrkUyuispC",
        "object":"card",
        "brand":"VISA",
        "exp_month":12,
        "exp_year":2034,
        "funding":"credit",
        "last4":"1111"
    },
    "response_code":20000
 }
}
  1. message
  2. Success: true

Else if it fails it sends the

    {message: 'Payment could not be processed', logs: res, success: false}.to_json
  1. message
  2. logs (This is to troubleshoot the error the api is sending back)
  3. success: false

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/patrickoramah/simplepayNG. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.