Giter Club home page Giter Club logo

unionpay's Introduction

unionpay

银联支付api

Usage

Install

gem install unionpay

Config

UnionPay.environment  = :development    ## 测试环境, :pre_production  #预上线环境, 默认 # 线上环境
UnionPay.mer_id       = '105550149170027'
UnionPay.mer_abbr     = '商户名称'
UnionPay.security_key = '88888888'

Generate front payment form

param = {}
param['orderAmount']  = 11000                                           #交易金额
param['orderNumber']  = Time.now.strftime('%Y%m%d%H%M%S')               #订单号,必须唯一
param['customerIp']   = '127.0.0.1'
param['frontEndUrl']  = "http://www.example.com/sdk/utf8/front_notify.php"    #前台回调URL
param['backEndUrl']   = "http://www.example.com/sdk/utf8/back_notify.php"     #后台回调URL

# 非必填字段
#   param['transType']          = UnionPay::CONSUME                            #交易类型,CONSUME or PRE_AUTH
#   param['commodityUrl']       = "http://www.example.com/product?name=商品"   #商品URL
#   param['commodityName']      = '商品名称'         # 商品名称
#   param['commodityUnitPrice'] = 11000             #商品单价
#   param['commodityQuantity']  = 1                 #商品数量
#

# 其余可填空的参数可以不填写

service = UnionPay::Service.front_pay(param)
service.args   ## get args
service.form(target: '_blank', id: 'form'){"<input type='submit' />"}  ## get form

Verify notify

# example in rails
# The notify url MUST be set when generate payment url
def unionpay_notify
  # except :controller_name, :action_name, :host, etc.
  notify_params = params.except(*request.path_parameters.keys)
  args = UnionPay::Service.response(notify_params).args
  if args['respCode'] == UnionPay::RESP_SUCCESS
    # valid notify, code your business logic.
    render :text => 'success'
  else
    render :text => 'error'
  end
end

Generate back payment post params

param = {}
# 交易类型 退货=REFUND 或 消费撤销=CONSUME_VOID, 如果原始交易是PRE_AUTH,那么后台接口也支持对应的
#  PRE_AUTH_VOID(预授权撤销), PRE_AUTH_COMPLETE(预授权完成), PRE_AUTH_VOID_COMPLETE(预授权完成撤销)
# param['transType']        = UnionPay::REFUND
param['origQid']          = '201110281442120195882' #原交易返回的qid, 从数据库中获取
param['orderAmount']      = 11000        #交易金额
param['orderNumber']      = '20131220151706'   #订单号,必须唯一(不能与原交易相同)
param['customerIp']       = '127.0.0.1'  #用户IP
param['frontEndUrl']      = ""     #前台回调URL, 后台交易可为空
param['backEndUrl']       = "http://www.example.com/sdk/utf8/back_notify.php"    #后台回调URL

service = UnionPay::Service.back_pay(param)
service.args   ## get args
res = service.post   ## do post
response = UnionPay::Service.response res.body
if response['respCode'] != UnionPay::RESP_SUCCESS
  raise("Error #{response['respCode']}: #{response['respMsg']}:")
end

Query

param = {}
param['transType'] = UnionPay::CONSUME
param['orderNumber'] = "20111108150703852"
param['orderTime'] = "20111108150703"
query = UnionPay::Service.query(param)
res = query.post
response = UnionPay::Service.response res.body

query_result = response['queryResult']
resp_code = response['respCode']

if query_result == UnionPay::QUERY_FAIL
  puts "交易失败[respCode:#{resp_code}]!"
elsif query_result == UnionPay::QUERY_INVALID
  puts "不存在此交易!"
elsif resp_code==UnionPay::RESP_SUCCESS && query_result == UnionPay::QUERY_SUCCESS
  puts '交易成功!'
elsif resp_code==UnionPay::RESP_SUCCESS && query_result == UnionPay::QUERY_WAIT
  puts '交易处理中,下次再查!'
end

unionpay's People

Contributors

kikyous avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.