Class: UnionPay::Service
- Inherits:
-
Object
- Object
- UnionPay::Service
- Defined in:
- lib/unionpay/service.rb
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#args ⇒ Object
Returns the value of attribute args.
Class Method Summary collapse
- .back_pay(param) ⇒ Object
- .front_pay(param) ⇒ Object
- .query(param) ⇒ Object
- .responce(param) ⇒ Object
- .sign(param) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
13 14 15 |
# File 'lib/unionpay/service.rb', line 13 def api_url @api_url end |
#args ⇒ Object
Returns the value of attribute args.
13 14 15 |
# File 'lib/unionpay/service.rb', line 13 def args @args end |
Class Method Details
.back_pay(param) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/unionpay/service.rb', line 33 def self.back_pay(param) new.instance_eval do param['orderTime'] ||= Time.now.strftime('%Y%m%d%H%M%S') #交易时间, YYYYmmhhddHHMMSS param['orderCurrency'] ||= UnionPay::CURRENCY_CNY #交易币种,CURRENCY_CNY=>人民币 param['transType'] ||= UnionPay::REFUND @api_url = UnionPay.back_pay_url self.args = PayParamsEmpty.merge(PayParams).merge(param) @param_check = PayParamsCheck trans_type = param['transType'] if [UnionPay::CONSUME, UnionPay::PRE_AUTH].include? trans_type if !self.args['cardNumber'] && !self.args['pan'] raise('consume OR pre_auth transactions need cardNumber!') end else raise('origQid is not provided') if UnionPay.empty? self.args['origQid'] end service end end |
.front_pay(param) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/unionpay/service.rb', line 15 def self.front_pay(param) new.instance_eval do param['orderTime'] ||= Time.now.strftime('%Y%m%d%H%M%S') #交易时间, YYYYmmhhddHHMMSS param['orderCurrency'] ||= UnionPay::CURRENCY_CNY #交易币种,CURRENCY_CNY=>人民币 param['transType'] ||= UnionPay::CONSUME trans_type = param['transType'] if [UnionPay::CONSUME, UnionPay::PRE_AUTH].include? trans_type @api_url = UnionPay.front_pay_url self.args = PayParamsEmpty.merge(PayParams).merge(param) @param_check = UnionPay::PayParamsCheck else # 前台交易仅支持 消费 和 预授权 raise("Bad trans_type for front_pay. Use back_pay instead") end service end end |
.query(param) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/unionpay/service.rb', line 78 def self.query(param) new.instance_eval do @api_url = UnionPay.query_url param['version'] = UnionPay::PayParams['version'] param['charset'] = UnionPay::PayParams['charset'] param['merId'] = UnionPay::PayParams['merId'] if UnionPay.empty?(UnionPay::PayParams['merId']) && UnionPay.empty?(UnionPay::PayParams['acqCode']) raise('merId and acqCode can\'t be both empty') end if !UnionPay.empty?(UnionPay::PayParams['acqCode']) acq_code = UnionPay::PayParams['acqCode'] param['merReserved'] = "{acqCode=#{acq_code}}" else param['merReserved'] = '' end self.args = param @param_check = UnionPay::QueryParamsCheck service end end |
.responce(param) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/unionpay/service.rb', line 53 def self.responce(param) new.instance_eval do if param.is_a? String pattern = /(?<=cupReserved=)(\{.*?\})/ cup_reserved = pattern.match(param).to_s param.sub! pattern, '' param = Rack::Utils.parse_nested_query param param['cupReserved'] = cup_reserved end cup_reserved ||= (param['cupReserved'] ||= '') arr_reserved = Rack::Utils.parse_nested_query cup_reserved[1..-2] if !param['signature'] || !param['signMethod'] raise('No signature Or signMethod set in notify data!') end param.delete 'signMethod' if param.delete('signature') != Service.sign(param) raise('Bad signature returned!') end self.args = param.merge arr_reserved self.args.delete 'cupReserved' self end end |
.sign(param) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/unionpay/service.rb', line 102 def self.sign(param) sign_str = param.sort.map do |k,v| "#{k}=#{v}&" unless UnionPay::SignIgnoreParams.include? k end.join Digest::MD5.hexdigest(sign_str + Digest::MD5.hexdigest(UnionPay.security_key)) end |
Instance Method Details
#[](key) ⇒ Object
128 129 130 |
# File 'lib/unionpay/service.rb', line 128 def [](key) self.args[key] end |
#form(options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/unionpay/service.rb', line 109 def form(={}) attrs = .map { |k, v| "#{k}='#{v}'" }.join(' ') html = [ "<form #{attrs} action='#{@api_url}' method='post'>" ] args.each do |k, v| html << "<input type='hidden' name='#{k}' value='#{v}' />" end if block_given? html << yield html << "</form>" end html.join end |
#post ⇒ Object
124 125 126 |
# File 'lib/unionpay/service.rb', line 124 def post Curl.post @api_url, self.args end |