Project

newebpay

0.0
The project is in a healthy, maintained state
The offsite payments implemented for NewebPay
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Project Readme

NewebPay

Build Status Test Coverage Maintainability

The Offsite Payments implement for NewebPay(藍新金流)

Installation

Add this line to your application's Gemfile:

gem 'newebpay'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install newebpay

Usage

Configure Newebpay

# config/initializes/newebpay.rb

# Set to test mode if under development
OffsitePayments.mode = :test

Newebpay::Config.config do |c|
  c.hash_key = ''
  c.hash_iv = ''
end

Temporary switch config

config = Newebpay::Config.new do |c|
  c.hash_key = 'A' * 32
  c.hash_iv = 'B' * 16
end

Newebpay.use(config) do
  Newebpay::Config.current == config # => true
end

Create payment button

In your controller

options = {
  amount: 40, # TWD
  description: 'My Product',
  return_url: newebpay_return_url,
  notify_url: newebpay_notify_url
}

@helper = Newebpay.helper(order_id, merchant_id, options)

The Newebpay.helper is an alias to OffsitePayment::Integrations::Newebpay::Helper.new

In your view

<!-- payment.erb -->

<form action="<%= Newebpay.service_url %>" method="POST">
  <% @helper.form_fields.each do |key, value| %>
    <input type="hidden" name="<%= key %>" value="<%= value %>" />
    <% end %>
  <button type="submit">Process</button>
</form>

Above example is tested under Sinatra but you can use form helper in Rails to improve it.

Process Return Data

In your controller

@return = Newebpay.return(request.body.read)
@return.trade_info # The decrypted TradeInfo from NewebPay

Please notice the request.body may cause Rails params not work due to we didn't rewind it

Process Notification Data

In you controller

@notification = Newebpay.notification(request.body.read)
@notification.trade_info # The decrypted TradeInfo from NewebPay

Roadmap

  • MPG Support
  • Allow switch config
  • Refund API
  • Unauthorized API
  • Order Status API
  • Rails Support
    • Form Object
    • Form Helper

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

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

Code of Conduct

Everyone interacting in the Newebpay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.