-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
125 lines (105 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict'
var Common = require("./common"),
Alipay = require("./lib/alipay"),
Weixin = require("./lib/weixin"),
Huawei = require("./lib/huawei"),
fs = require('fs'),
path = require('path'),
debug = require("debug")("third-payment");
class Lib {
constructor(config){
if(typeof config === "string"){
try{
if(!fs.existsSync(path.join(__dirname , config))){
throw new Error("配置文件不存在");
}
var conf = fs.readFileSync(path.join(__dirname , config)).toString();
conf = JSON.parse(conf);
this.conf = conf;
} catch(e) {
debug(e);
throw e;
}
}
if(typeof config === "object"){
this.conf = config;
}
if(!config){
throw new Error("缺少支付参数配置")
}
};
set conf(value){
var verify = Common.vefiyConf(value)
if(!verify){
var err = Common.getErr();
throw new Error(err);
}
this._conf = value;
};
get conf(){
return this._conf;
}
getChannel(type,vendor){
switch(type){
case "weixin":
case "weixin_app" :
case "weixin_mp" :
case "weixin_native" :
case "weixin_web" :
var temp = this.conf["weixin"];
return new Weixin(temp[vendor]);
case "alipay":
case "alipay_app" :
case "alipay_mp" :
case "alipay_web" :
case "alipay_native" :
var temp = this.conf["alipay"];
return new Alipay(temp[vendor]);
case "huawei" :
var temp = this.conf["huawei"];
return new Huawei(temp[vendor]);
default :
debug("支付类型错误: type");
return false;
}
}
getPrePay(datas){
var Channel = this.getChannel(datas.type,datas.vendor);
if(!Channel){
debug("支付类型错误: type");
return Promise.reject("支付类型错误: type");
}
return Channel.create(datas);
};
verifyResponse(datas,response,format){
var Channel = this.getChannel(datas.type,datas.vendor);
Channel.type = datas.type;
return Channel.verifyResponse(response,format).then(
res=>{
if(res && res.result == true){
return Channel.formatOut();
}else{
debug("verify response fail , ",res);
return Channel.formatOut(res.msg || "数据校验错误");
}
}
).catch(err=>{
debug("verify fail !",err);
return Channel.formatOut(err);
});
}
queryTrade(datas,trade){
var Channel = this.getChannel(datas.type,datas.vendor);
Channel.type = datas.type;
return Channel.query(trade);
}
refundTrade(trade){
var Channel = this.getChannel(trade.type,trade.vendor);
return Channel.refund(trade);
}
transfer(datas){
var Channel = this.getChannel("alipay_app","app");
return Channel.transfer(datas);
}
}
module.exports = Lib;