NewebPay
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 = ''
endTemporary 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
endCreate 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.helperis an alias toOffsitePayment::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 NewebPayPlease notice the
request.bodymay cause Railsparamsnot 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 NewebPayRoadmap
- 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.