newebpay-rails 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +7 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +234 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +12 -0
  11. data/Rakefile +12 -0
  12. data/app/controllers/newebpay/application_controller.rb +23 -0
  13. data/app/controllers/newebpay/cancel_auth_notify_callbacks_controller.rb +12 -0
  14. data/app/controllers/newebpay/donation_notify_callbacks_controller.rb +12 -0
  15. data/app/controllers/newebpay/mpg_callbacks_controller.rb +23 -0
  16. data/app/controllers/newebpay/notify_callbacks_controller.rb +18 -0
  17. data/app/controllers/newebpay/payment_code_callbacks_controller.rb +18 -0
  18. data/app/controllers/newebpay/periodical_callbacks_controller.rb +23 -0
  19. data/app/controllers/newebpay/periodical_notify_callbacks_controller.rb +23 -0
  20. data/app/helpers/newebpay/application_helper.rb +7 -0
  21. data/app/helpers/newebpay/donation_helper.rb +36 -0
  22. data/app/helpers/newebpay/mpg_helper.rb +35 -0
  23. data/app/helpers/newebpay/periodical_helper.rb +35 -0
  24. data/app/jobs/newebpay/rails/application_job.rb +6 -0
  25. data/bin/rails +13 -0
  26. data/config/initializers/inflections.rb +3 -0
  27. data/config/routes.rb +13 -0
  28. data/lib/generators/newebpay/install_generator.rb +19 -0
  29. data/lib/generators/newebpay/templates/newebpay_initializer.rb +117 -0
  30. data/lib/newebpay.rb +46 -0
  31. data/lib/newebpay/attr_key_helper.rb +7 -0
  32. data/lib/newebpay/bank_codes.rb +17 -0
  33. data/lib/newebpay/cancel_auth/request.rb +69 -0
  34. data/lib/newebpay/cancel_auth/response.rb +38 -0
  35. data/lib/newebpay/close_fund/base.rb +69 -0
  36. data/lib/newebpay/close_fund/refund.rb +10 -0
  37. data/lib/newebpay/close_fund/request.rb +10 -0
  38. data/lib/newebpay/close_fund/response.rb +26 -0
  39. data/lib/newebpay/config.rb +65 -0
  40. data/lib/newebpay/donation/form.rb +90 -0
  41. data/lib/newebpay/donation/response.rb +38 -0
  42. data/lib/newebpay/engine.rb +9 -0
  43. data/lib/newebpay/error_codes.rb +131 -0
  44. data/lib/newebpay/mpg/form.rb +92 -0
  45. data/lib/newebpay/mpg/response.rb +27 -0
  46. data/lib/newebpay/newebpay_helper.rb +98 -0
  47. data/lib/newebpay/periodical/form.rb +83 -0
  48. data/lib/newebpay/periodical/response.rb +25 -0
  49. data/lib/newebpay/query_trade/response.rb +35 -0
  50. data/lib/newebpay/query_trade/search.rb +53 -0
  51. data/lib/newebpay/rails.rb +6 -0
  52. data/lib/newebpay/rails/engine.rb +9 -0
  53. data/lib/newebpay/version.rb +3 -0
  54. data/newebpay-rails.gemspec +50 -0
  55. metadata +233 -0
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency 'newebpay/application_controller'
4
+
5
+ module Newebpay
6
+ class MPGCallbacksController < ApplicationController
7
+ skip_before_action :verify_authenticity_token
8
+
9
+ def newebpay_response
10
+ @newebpay_response ||= Newebpay::MPG::Response.new(params['TradeInfo'])
11
+ end
12
+
13
+ def proceed
14
+ unless Newebpay.config.mpg_callback.is_a? Proc
15
+ raise NotImplementedError, 'Newebpay.config.mpg_callback is not a proc.'
16
+ end
17
+ raise InvalidResponseError if params['TradeInfo'].blank?
18
+
19
+ instance_exec(Newebpay::MPG::Response.new(params['TradeInfo']), self, ::Rails.application.routes.url_helpers, &Newebpay.config.mpg_callback)
20
+ redirect_to '/' unless performed?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require_dependency 'newebpay/application_controller'
2
+
3
+ module Newebpay
4
+ class NotifyCallbacksController < ApplicationController
5
+ skip_before_action :verify_authenticity_token
6
+
7
+ def newebpay_response
8
+ @newebpay_response ||= Newebpay::MPG::Response.new(params['TradeInfo'])
9
+ end
10
+
11
+ def proceed
12
+ raise NotImplementedError, 'Newebpay.config.notify_callback is not a proc.' unless Newebpay.config.notify_callback.is_a? Proc
13
+ raise InvalidResponseError if params["TradeInfo"].blank?
14
+ instance_exec(Newebpay::MPG::Response.new(params["TradeInfo"]), self, ::Rails.application.routes.url_helpers, &Newebpay.config.notify_callback)
15
+ head 200
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require_dependency 'newebpay/application_controller'
2
+
3
+ module Newebpay
4
+ class PaymentCodeCallbacksController < ApplicationController
5
+ skip_before_action :verify_authenticity_token
6
+
7
+ def newebpay_response
8
+ @newebpay_response ||= Newebpay::MPG::Response.new(params['TradeInfo'])
9
+ end
10
+
11
+ def proceed
12
+ raise NotImplementedError, 'Newebpay.config.payment_code_callback is not a proc.' unless Newebpay.config.payment_code_callback.is_a? Proc
13
+ raise InvalidResponseError if params["TradeInfo"].blank?
14
+ instance_exec(Newebpay::MPG::Response.new(params["TradeInfo"]), self, ::Rails.application.routes.url_helpers, &Newebpay.config.payment_code_callback)
15
+ redirect_to '/' unless performed?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency 'newebpay/application_controller'
4
+
5
+ module Newebpay
6
+ class PeriodicalCallbacksController < ApplicationController
7
+ skip_before_action :verify_authenticity_token
8
+
9
+ def newebpay_response
10
+ @newebpay_response ||= Newebpay::MPG::Response.new(params['Period'])
11
+ end
12
+
13
+ def proceed
14
+ unless Newebpay.config.periodical_callback.is_a? Proc
15
+ raise NotImplementedError, 'Newebpay.config.periodical_callback is not a proc.'
16
+ end
17
+ raise InvalidResponseError if params['Period'].blank?
18
+
19
+ instance_exec(Newebpay::Periodical::Response.new(params['Period']), self, ::Rails.application.routes.url_helpers, &Newebpay.config.periodical_callback)
20
+ redirect_to '/' unless performed?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency 'newebpay/application_controller'
4
+
5
+ module Newebpay
6
+ class PeriodicalNotifyCallbacksController < ApplicationController
7
+ skip_before_action :verify_authenticity_token
8
+
9
+ def newebpay_response
10
+ @newebpay_response ||= Newebpay::MPG::Response.new(params['Period'])
11
+ end
12
+
13
+ def proceed
14
+ unless Newebpay.config.periodical_notify_callback.is_a? Proc
15
+ raise NotImplementedError, 'Newebpay.config.periodical_notify_callback is not a proc.'
16
+ end
17
+ raise InvalidResponseError if params['Period'].blank?
18
+
19
+ instance_exec(Newebpay::Periodical::Response.new(params['Period']), self, ::Rails.application.routes.url_helpers, &Newebpay.config.periodical_notify_callback)
20
+ head 200
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Newebpay
2
+ module ApplicationHelper
3
+ include MPGHelper
4
+ include PeriodicalHelper
5
+ include DonationHelper
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Newebpay
4
+ module DonationHelper
5
+ def render_newebpay_donation_form(donation_form_object, options = {})
6
+ unless donation_form_object.is_a? Newebpay::Donation::Form
7
+ raise ArgumentError, 'The first argument must be a Newebpay::Donation::Form.'
8
+ end
9
+
10
+ title = options[:title] || 'Go'
11
+ submit_class = options[:class] || ''
12
+ submit_id = options[:id] || ''
13
+ data = options[:data] || {}
14
+
15
+ form_tag(donation_form_object.donation_url, method: :post) do
16
+ donation_form_object.form_attrs.each do |key, value|
17
+ concat hidden_field_tag key, value
18
+ end
19
+
20
+ concat button_tag title, class: submit_class, id: submit_id, data: data
21
+ end
22
+ end
23
+
24
+ def newebpay_donation_pay_button(title, donation_url, options)
25
+ form = Newebpay::Donation::Form.new(donation_url, options)
26
+
27
+ render_newebpay_donation_form(
28
+ form,
29
+ title: title,
30
+ class: options[:class],
31
+ id: options[:id],
32
+ data: options[:data]
33
+ )
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Newebpay
4
+ module MPGHelper
5
+ def render_newebpay_mpg_form(mpg_form_object, options = {})
6
+ unless mpg_form_object.is_a? Newebpay::MPG::Form
7
+ raise ArgumentError, 'The first argument must be a Newebpay::MPG::Form.'
8
+ end
9
+
10
+ title = options[:title] || 'Go'
11
+ submit_class = options[:class] || ''
12
+ submit_id = options[:id] || ''
13
+ data = options[:data] || {}
14
+
15
+ form_tag(Newebpay.config.mpg_gateway_url, method: :post) do
16
+ mpg_form_object.form_attrs.each do |key, value|
17
+ concat hidden_field_tag key, value
18
+ end
19
+ concat button_tag title, class: submit_class, id: submit_id, data: data
20
+ end
21
+ end
22
+
23
+ def newebpay_mpg_pay_button(title, options = {})
24
+ form = Newebpay::MPG::Form.new(options)
25
+
26
+ render_newebpay_mpg_form(
27
+ form,
28
+ title: title,
29
+ class: options[:class],
30
+ id: options[:id],
31
+ data: options[:data]
32
+ )
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Newebpay
4
+ module PeriodicalHelper
5
+ def render_newebpay_periodical_form(periodical_form_object, options = {})
6
+ unless periodical_form_object.is_a? Newebpay::Periodical::Form
7
+ raise ArgumentError, 'The first argument must be a Newebpay::Periodical::Form.'
8
+ end
9
+
10
+ title = options[:title] || 'Go'
11
+ submit_class = options[:class] || ''
12
+ submit_id = options[:id] || ''
13
+ data = options[:data] || {}
14
+
15
+ form_tag(Newebpay.config.periodical_gateway_url, method: :post) do
16
+ periodical_form_object.form_attrs.each do |key, value|
17
+ concat hidden_field_tag key, value
18
+ end
19
+ concat button_tag title, class: submit_class, id: submit_id, data: data
20
+ end
21
+ end
22
+
23
+ def newebpay_periodical_pay_button(title, options = {})
24
+ form = Newebpay::Periodical::Form.new(options)
25
+
26
+ render_newebpay_periodical_form(
27
+ form,
28
+ title: title,
29
+ class: options[:class],
30
+ id: options[:id],
31
+ data: options[:data]
32
+ )
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ module Newebpay
2
+ module Rails
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
2
+ ENGINE_PATH = File.expand_path('../../lib/newebpay/rails/engine', __FILE__)
3
+ APP_PATH = File.expand_path('../../spec/test_app/config/application', __FILE__)
4
+
5
+ # Set up gems listed in the Gemfile.
6
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
7
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
8
+
9
+ require 'action_controller/railtie'
10
+ require 'action_mailer/railtie'
11
+ require 'active_job/railtie'
12
+
13
+ require 'rails/engine/commands'
@@ -0,0 +1,3 @@
1
+ ActiveSupport::Inflector.inflections(:en) do |inflect|
2
+ inflect.acronym 'MPG'
3
+ end
@@ -0,0 +1,13 @@
1
+ Newebpay::Engine.routes.draw do
2
+ post 'mpg_callbacks', to: 'mpg_callbacks#proceed'
3
+ post 'payment_code_callbacks', to: 'payment_code_callbacks#proceed'
4
+ post 'notify_callbacks', to: 'notify_callbacks#proceed'
5
+
6
+ post 'periodical_callbacks', to: 'periodical_callbacks#proceed'
7
+ post 'periodical_notify_callbacks', to: 'periodical_notify_callbacks#proceed'
8
+
9
+ get 'donation_callbacks', to: 'donation_callbacks#proceed'
10
+ post 'donation_notify_callbacks', to: 'donation_notify_callbacks#proceed'
11
+
12
+ post 'cancel_auth_notify_callbacks', to: 'cancel_auth_notify_callbacks#proceed'
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Newebpay
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc 'Creates the Newebpay initializer and mounts Newebpay::Rails::Engine.'
9
+
10
+ def copy_initializer_file
11
+ template 'newebpay_initializer.rb', ::Rails.root.join('config', 'initializers', 'newebpay.rb')
12
+ end
13
+
14
+ def mount_engine
15
+ route "mount Newebpay::Engine => '/newebpay'"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,117 @@
1
+ Newebpay.configure do |config|
2
+ # 預設商店代號
3
+ config.merchant_id = 'place_your_merchant_id_here'
4
+ # Hash Key
5
+ config.hash_key = 'place_your_hash_key_here'
6
+ # Hash IV
7
+ config.hash_iv = 'place_your_hash_iv_here'
8
+
9
+ # for development 測試環境
10
+ config.mpg_gateway_url = 'https://ccore.newebpay.com/MPG/mpg_gateway' #MPG金流
11
+ config.periodical_gateway_url = 'https://ccore.newebpay.com/MPG/period' #定期定額
12
+ config.query_trade_url = "https://ccore.newebpay.com/API/QueryTradeInfo" #交易狀態查詢
13
+ config.cancel_auth_url = "https://ccore.newebpay.com/API/CreditCard/Cancel" #信用卡取消授權
14
+ config.close_fund_url = "https://ccore.newebpay.com/API/CreditCard/Close" #信用卡請退款
15
+
16
+ # for production 正式環境
17
+ # config.mpg_gateway_url = "https://core.newebpay.com/MPG/mpg_gateway" #MPG金流
18
+ # config.period_gateway_url = "https://core.newebpay.com/MPG/period" #定期定額
19
+ # config.query_trade_url = "https://core.newebpay.com/API/QueryTradeInfo" #交易狀態查詢
20
+ # config.cancel_auth_url = "https://core.newebpay.com/API/CreditCard/Cancel" #信用卡取消授權
21
+ # config.request_fund_url = "https://core.newebpay.com/API/CreditCard/Close" #信用卡請退款
22
+
23
+ # 支付完成後使用者導回到網站觸發的callback(前景)
24
+ config.mpg_callback do |newebpay_response|
25
+ # 若已在 notify_callback 中處理資料更新或商業邏輯,請勿在此重複處理。
26
+ # 使用者完成付款後(notify_callback),使用者導回到網站頁面(mpg_callback)。
27
+ #
28
+ # 範例,假設已在notify_callback中處理商業邏輯
29
+ # if newebpay_response.success?
30
+ # flash[:success] = newebpay_response.message
31
+ # else
32
+ # flash[:error] = newebpay_response.message
33
+ # end
34
+ #
35
+ # redirect_to root_path
36
+ end
37
+
38
+
39
+ # #付款後觸發的callback(背景)。若僅使用即時交易支付方式,且已在 mpg_callback 中處理商業邏輯,可忽略此設定。
40
+ # config.notify_callback do |newebpay_response|
41
+ # # 範例
42
+ #
43
+ # if newebpay_response.success?
44
+ # Order.find_by(serial: newebpay_response.merchant_order_no)
45
+ # .update_attributes!(paid: true)
46
+ # else
47
+ # Rails.logger.info "Newebpay Payment Not Succeed: #{newebpay_response.status}: #{newebpay_response.message} (#{newebpay_response.to_json})"
48
+ # end
49
+ # end
50
+
51
+ # #取號完成觸發的callback。 接收 ATM轉帳(VACC)、超商代碼繳費(CVS)、超商條碼繳費 (BARCODE)、超商取貨付款(CVSCOM) 的付款資訊
52
+ # #若想直接在藍新金流頁面顯示付款資訊,且使用者不需回到網站,可忽略此設定。
53
+ # config.payment_code_callback do |newebpay_response|
54
+ # # 範例
55
+ #
56
+ # if newebpay_response.success? &&
57
+ # newebpay_response.payment_type == 'VACC'
58
+ #
59
+ # bank_code = newebpay_response.bank_code
60
+ # account_number = newebpay_response.code_no
61
+ # expired_at =
62
+ # DateTime.parse("#{newebpay_response.expire_date} #{newebpay_response.expire_time} UTC+8")
63
+ # Order.find_by(serial: newebpay_response.merchant_order_no)
64
+ # .update_attributes!(bank_code: bank_code, account_number: account_number, expired_at: expired_at)
65
+ # flash[:info] =
66
+ # "Please transfer the money to bank code #{bank_code}, account number #{account_number} before #{I18n.l(expired_at)}"
67
+ # else
68
+ # Rails.logger.error "Newebpay Payment Code Receive Not Succeed: #{newebpay_response.status}: #{newebpay_response.message} (#{newebpay_response.to_json})"
69
+ # flash[:error] = "Our apologies, but an unexpected error occured, please try again"
70
+ # end
71
+ #
72
+ # redirect_to orders_path
73
+ # end
74
+ #------------------------
75
+
76
+ # # 定期定額委託單建立完成後使用者導回到網站觸發的callback。若不啟用,付款人將停留在藍新金流交易完成頁面
77
+ # config.periodical_callback do |newebpay_response|
78
+ # # 範例
79
+ # if newebpay_response.success?
80
+ # p "定期定額建立"
81
+ # flash[:success] = newebpay_response.message
82
+ # else
83
+ # flash[:error] = newebpay_response.message
84
+ # end
85
+ # redirect_to root_path
86
+ # end
87
+
88
+ # # 每期交易完成後觸發的callback,
89
+ # config.periodical_notify_callback do |newebpay_response|
90
+ # p "定期定額notify"
91
+ # if newebpay_response.success?
92
+ # PerTransaction.find_by(period_no: newebpay_response.period_no)
93
+ # .update_attributes!(paid: true)
94
+ # else
95
+ # Rails.logger.info "Newebpay Periodical Not Succeed: #{newebpay_response.status}: #{newebpay_response.message} (#{newebpay_response.to_json})"
96
+ # end
97
+ # end
98
+ #-----------------------
99
+
100
+ # #捐款付款後觸發的callback。
101
+ # config.donation_notify_callback do |newebpay_response|
102
+ # if newebpay_response.success?
103
+ # # Donation.find_by(merchant_order_no: newebpay_response.merchant_order_no).update_attributes!(paid: true)
104
+ # else
105
+ # Rails.logger.info "Newebpay Donation Not Succeed: #{newebpay_response.status}: #{newebpay_response.message} (#{newebpay_response.to_json})"
106
+ # end
107
+ # end
108
+
109
+ #-----------------------
110
+
111
+ #信用卡取消授權的callback。
112
+ config.cancel_auth_notify_callback do |newebpay_response|
113
+ if newebpay_response.success?
114
+ else
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'http'
5
+
6
+ require 'newebpay/version'
7
+ require 'newebpay/config'
8
+ require 'newebpay/error_codes'
9
+ require 'newebpay/bank_codes'
10
+ require 'newebpay/engine'
11
+ require 'newebpay/newebpay_helper'
12
+ require 'newebpay/mpg/form'
13
+ require 'newebpay/mpg/response'
14
+ require 'newebpay/periodical/form'
15
+ require 'newebpay/periodical/response'
16
+ require 'newebpay/donation/form'
17
+ require 'newebpay/donation/response'
18
+ require 'newebpay/query_trade/search'
19
+ require 'newebpay/query_trade/response'
20
+ require 'newebpay/cancel_auth/request'
21
+ require 'newebpay/cancel_auth/response'
22
+ require 'newebpay/close_fund/base'
23
+ require 'newebpay/close_fund/refund'
24
+ require 'newebpay/close_fund/request'
25
+ require 'newebpay/close_fund/response'
26
+ module Newebpay
27
+ def self.host
28
+ @host ||= ::Rails.application.routes.default_url_options[:host]
29
+ end
30
+
31
+ def self.configure
32
+ yield config
33
+ end
34
+
35
+ def self.config
36
+ @config ||= Config.new
37
+ end
38
+
39
+ def self.get_error_message(code)
40
+ ErrorCodes.error_codes[code.to_sym]
41
+ end
42
+
43
+ def self.bank(bank_code)
44
+ BankCodes.bank_codes(bank_code.to_sym)
45
+ end
46
+ end