Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Last active June 29, 2017 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagdeepsingh/da5d4897119cd6359996ab27462bd255 to your computer and use it in GitHub Desktop.
Save jagdeepsingh/da5d4897119cd6359996ab27462bd255 to your computer and use it in GitHub Desktop.
NihaoPay Payment Gateway

NihaoPay

External links:

API Url:

Contents:

Setup and configuration

Register for a test account here.

You will receive an email with details about MERCHANT_ID, a TRADE_PASSWORD and login credentials for Beta TMS.

On login to Beta TMS, you will be asked to reset the password.

Now, under Settings -> Certificate, you can enter the TRADE_PASSWORD to generate the ceritificate (or TOKEN) for making requests to the API.

Set the TOKEN as the global token for Nihaopay.

Nihaopay.token = TOKEN

You can see all your test transactions by visiting Beta TMS under Transaction management -> Transaction History.

Back to top

ExpressPay transactions

Purchase

Initialize a credit card object to be used for transaction.

credit_card = Nihaopay::CreditCard.new(number: '6221558812340000',
                                       expiry_year: 2017,
                                       expiry_month: 11,
                                       cvv: '123')
=> #<Nihaopay::CreditCard:0x007f97673ee788 @number="6221558812340000", @expiry_year=2017, @expiry_month=11, @cvv="123">
express_pay = Nihaopay::Transactions::Purchase.start(1000, credit_card, client_ip: '192.168.1.15')
=> #<Nihaopay::Transactions::Purchase:0x007f974b87e788 @token=TOKEN, @transaction_id="20170117085050008054", @type=nil, @status="success", @captured=true, @currency="JPY", @reference="944d6005b9a1475e84d91692ec40e0e6", @amount=2039, @note=nil, @time=2017-01-18 06:55:55 UTC>

NOTE: client_ip is a required parameter.

If you have multiple merchants, then you can build a Nihaopay::Shop object and create transactions using it.

nihaopay_merchant = Nihaopay::Merchant.new(TOKEN)
=> #<Nihaopay::Merchant:0x007f942ee4b4b0 @token=TOKEN>

nihaopay_merchant.purchase(amount, credit_card, client_ip: '192.168.1.15')

Refund

Use a purchased express_pay transaction for refund.

refunded = express_pay.refund
=> #<Nihaopay::Transactions::Refund:0x007f9769176698 @transaction_id="20170117085057000524", @status="success", @refunded=true, @refund_transaction_id="20170117085050008054", @time=2017-01-18 06:55:55 UTC>

Back to top

Securepay transactions

Alipay

# app/controllers/nihaopay_payments_controller.rb
def initiate
  nihaopay_merchant = Nihaopay::Merchant.new(TOKEN)
  response = nihaopay_merchant.ali_pay(1000, 'JPY', { reference: UNQIUE_REFERENCE_ID,
                                                      ipn_url: 'https://domain.com/path/to/ipn/',
                                                      callback_url: 'https://domain.com/path/to/callback/' })
  @html = response.body
end

def callback
end

def ipn
end

Make a JS request to initiate action and replace the whole HTML with response body.

# app/views/initiate.js.haml
= "$('html').html(\"#{escape_javascript(@html)}\");".html_safe

This will take you to AliPay payment screen. Login screen

Enter following details:

Enter captcha and click "Next Step". You will be taken to another screen where you will be asked to enter payment password. Use 111111 as payment password and click "Confirm Payment". Payment screen

On successful payment, you will receive a POST request on your callback action with following params:

{"id"=>"20170629055617012551", "amount"=>"1000", "currency"=>"JPY", "reference"=>"1498715776", "status"=>"success", "time"=>"2017-06-29T05:59:37Z", "note"=>"null", "verify_sign"=>"0d664170cf7d0f4c2beec0c045d9f1f6"}

Back to top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment