var jm = jm || {};
if((typeof exports !== 'undefined' && typeof module !== 'undefined')){
jm = require('jm-sdk-core');
}
(function(){
var sdk = jm.sdk;
var storage = sdk.storage;
var ERR = sdk.consts.ERR;
sdk.on('init', function (opts) {
var model = 'pay';
opts[model] = opts[model] || {};
opts[model].uri = opts[model].uri || opts.uri;
opts[model].timeout = opts[model].timeout || opts.timeout;
sdk[model].init(opts[model]);
});
/**
* 支付对象
* @class pay
* @param {Object} opts 配置属性
* 格式:{uri:'网络地址'}
*/
sdk.pay = {
init: function(opts) {
opts = opts || {};
var uri = opts.uri;
var prefix = opts.prefix || '/pay';
this.uri = uri + prefix;
jm.enableEvent(this);
sdk.enableAjax(this,{timeout:opts.timeout||0});
},
/**
* 预请求支付,生成支付凭据
* @function pay#prepay
* @param {Object} [opts={}] 参数
* @example
* opts参数:{
* "way": 支付平台(必填)//['platform','swiftpass','pingxx'].
* "channel": 支付方式(可选)//默认为'platform',根据way有所不同.
* "title": 商品的标题(必填)//该参数最长为 32 个 Unicode 字符.
* "content": 商品的描述信息(必填)//该参数最长为 128 个 Unicode 字符.
* "amount": 金额(必填)//整数,单位分.
* "currency": 货币种类(必填)//['cny','tb']
* "orderId": 订单id(必填)//由商城产生或其它途径产生,根据type有所不同.
* "note": 附加描述(可选)
* "succeeded": 成功回调地址,用于后端(可选,支付成功后对后期处理)
* "successURL": 成功回调地址,用于前端(可选|必填,不同渠道会有不同)
* "attach": { //附加数据(可选)
* userId: 用户id //向谁支付
* }
* }
* @param {callback} [cb=function(err,result){}] 回调
*/
prepay: function(opts, cb) {
var self = this;
opts = opts || {};
cb = cb || function () {};
opts.token = opts.token || storage.getItem("token");
if (!opts.token) return cb(new Error('预请求支付失败'), ERR.FA_NOAUTH);
this.post({
url: this.uri + '/pays',
data: opts
}, cb);
},
/**
* 请求退款
* @function pay#refund
* @param {Object} [opts={}] 参数
* @example
* opts参数:{
* "payid": 支付id(必填).
* "amount": 金额(必填)//整数,单位分.
* }
* @param {callback} [cb=function(err,result){}] 回调
*/
refund: function(opts, cb) {
var self = this;
opts = opts || {};
cb = cb || function () {};
opts.token = opts.token || storage.getItem("token");
if (!opts.token) return cb(new Error('请求退款失败'), ERR.FA_NOAUTH);
this.post({
url: this.uri + '/refund',
data: opts
}, cb);
}
};
sdk.enableAjax(sdk.pay);
})();