-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (73 loc) · 2.4 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
var uuid=require("uuid");
module.exports = class paymentIndex
{
constructor(config,dist)
{
this.config=config.statics
this.context=this.config.context
this.bootstrap=require('./bootstrap.js')
this.enums=require('./struct.js')
this.tempConfig=require('./config.js')
this.drivers={}
if(this.config.drivers)
for(var a of this.config.drivers)
{
this.drivers[a.name] = new (require('./drivers/'+a.type+'.js'))(a);
}
//global.acc=new accountManager(dist)
}
async getLog(msg,func,self)
{
var dt=msg.data;
var session=msg.session;
var log=await global.db.Search(self.context,'payment_log',{where:{userid:session.userid}},dt)
return func(null,log)
}
async request(msg,func,self)
{
var dt=msg.data;
var driver=self.drivers[dt.name]
var session=msg.session;
if(!driver)
return func({m:'payment001'});
var data= await driver.request(dt.amount,session.userid);
var log={_id:data._id,submitDate:new Date(),amount:dt.amount,data:data.obj,userid:session.userid,type:dt.name}
await global.db.Save(self.context,'payment_log',["_id"],log)
return func(null,data);
}
async purchase(msg,func,self)
{
var dt=msg.data;
var session=msg.session;
var driver=self.drivers[dt.name]
if(!driver)
return func({m:'payment001'});
var id=await driver.getId(dt,driver);
if(!id)
return func({m:"payment002"})
var log=await global.db.SearchOne(self.context,'payment_log',{where:{_id:id}})
if(!log)
return func({m:"payment002"})
var resp= await driver.purchase(dt,log,driver);
if(!resp || !resp.isDone)
return func({m:"payment003"})
await global.db.Save(self.context,'payment_log',["_id"],{_id:id,resp,});
if( driver.wallet)
{
var amount=log.amount;
if(resp.amount)
amount=resp.amount
console.log('|||||||||------------>',id,driver.wallet.name,amount,log.userid)
try{
amount=parseInt(amount.toString());
var walletResponse = await global.wallet.request(id,driver.wallet.name,null,amount,session.userid);
console.log('|||||||||------------>',walletResponse)
return func(null,{active:true});
}catch(exp)
{
console.log('|||||||||------------>',exp)
}
}
return func({});
}
}