node-client
介绍
本项目是币丰支付(pay.coinport.com)的node.js SDK,封装所有支付的api,支持promise和callback两种模式。
Installation
npm install coinport-node-sdk
How to use
coinportPay = require 'coinport-node-sdk'
pay = coinportPay.createClient
apiToken: 'your api Token'
secretKey: 'your api secretKey'
verifySign: true
baseUrl: 'base Url'
# promise 调用
pay.createInvoice
price: 0.9
currency: 'CNY'
orderId: '29349'
.then (invoice)->
# do something here
, (err)->
# handle error
# callback 调用
pay.createInvoice
price: 0.9
currency: 'CNY'
orderId: '29349'
, (err,invoice)->
if err
# handle error
else
# do something here
Main API
createClient(options)
创建一个Client实例,需要传入一个配置对象(options),包含以下属性:
-
apiToken
:API令牌。必填项。
-
verifySign
:表示是否对消息体进行签名。默认值为true
,当值为true
时,secretKey
必须要有值。
-
secretKey
:用于消息签名的secret key,请在币丰支付个人账户页面获取。
-
baseUrl
: 请求API的地址,默认为:https://pay.coinport.com
如何使用:
pay.createClient
apiToken: '345345'
secretKey: 'AZYlsdjgklsdjgSWDNU'
verifySign: true
baseUrl: 'http://pay.coinport.com'
whiteIps
返回币丰港官方的ip组成的数组。用于商户在接受invoice状态更新时,判断是否这个信息由官方发出。
如何使用:
# 回调地址,接收订单状态变更
# 特别需要注意的是,一定要确保发送状态变更的ip是币丰港公布的ip
# 如果是使用nginx代理,还需要额外配置,不然读取的ip为本地ip
# 详情:http://expressjs.com/guide/behind-proxies.html
server.post '/pay/callback', (req, res)->
if req.ip not in pay.whiteIps
return res.sendStatus 401
body = req.body
console.log body
注意:以下所有API,如果没有传入callback函数,则默认返回promise。
createInvoice(invoice, [cb])
创建invoice,传入的参数为要生成的invoice。
执行后的结果,返回生成的invoice。
getBtcRate(currency, [cb])
读取比特币汇率。
getInvoice(params, [cb])
根据收据的状态和产生时间进行相应的查询操作。
pay.getInvoice
status:'new;paid'
begin:'2014-3-1'
end:'2014-7-1'
status是可选字段,如果不输入,系统默认设置status为已支付(paid)、已完成(complete)和已确认(confirmed)三种状态的集合
getInvoiceByOrder(orderId, [cb])
根据订单ID来查询收据。
refund(params, [cb])
提交退款请求:
pay.refund
amount:200
address:'1MxwyRqxGhVzo2MT23HZuEYahMpVywaPdp'
invoiceId: 'i5Yfc1T6EXyakctNWTWwHA'
返回的数据格式为:
{
refundId: 1000000001
}
queryRefund(refundId, [cb])
根据生成的refundId来查询refund记录。
返回的数据格式为:
{
"id":"1000000001",
"date":"1414749791399",
"status":"Pending",
"refundAmount":"30000",
"refundCurrency":"Cny",
"amount":"10",
"currency":"Btc",
"address":"1MxwyRqxGhVzo2MT23HZuEYahMpVywaPdp"
}
cancelRefund(refundId, [cb])
取消退款申请。
confirmRefund(refundId, [cb])
确认退款申请。
updateLogistics(logistics, [cb])
更新订单物流信息。
pay.updateLogistics
orderId:'23434'
logisticsCode:'234234234234'
logisticsOrgCode:'234sdf'
logisticsOrgName:'Express123'
其中orderId
和logisticsCode
是必填项。
payForAnother(params, [cb])
代付,向指定比特币地址发送比特币。
pay.payForAnother
amount:23
currency:'btc'
address:'1LwYygG9y4ikZkV5wyRNDRC2Fg9TTrZvpW'
notificationEmail:'hi@coinport.com'
note:'普通代付'
其中amount
、currency
和address
是必填项。
返回的数据格式为:
{
"id":1000000000012,
"merchantId":1000000001,
"status":"accepted",
"currency":"btc",
"amount":"0.537",
"note":"普通代付",
"created":"2014-10-10 12:12:12",
"address":"1MxwyRqxGhVzo2MT23HZuEYahMpVywaPdp"
}
queryPayForAnother(paras, [cb])
根据代付记录编号查询特定记录,或者顺序遍历所有代付记录。
pay.queryPayForAnother
id:234234324
status:'succeeded;failed'
skip:2
limit:10
详细参数设置,点击此处
Testing
npm test
License
The MIT License (MIT)
Copyright 2015 coinport
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.