/
BaseAccount.php
452 lines (411 loc) · 14.4 KB
/
BaseAccount.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
namespace lubaogui\account;
use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\base\Model;
use lubaogui\account\models\UserAccount;
use lubaogui\account\models\UserAccountLog;
use lubaogui\account\models\Trans;
use lubaogui\account\models\Bill;
use lubaogui\account\models\Freeze;
use lubaogui\payment\Payment;
use lubaogui\account\behaviors\ErrorBehavior;;
use yii\base\Exception;
/**
* 该类属于对账户所有对外接口操作的一个封装,账户的功能有充值,提现,担保交易,直付款交易等,账户操作中包含利润分账,但是分账最多支持2个用户分润
*/
class BaseAccount extends Model
{
private $config;
/**
* @brief
*
* @return public function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/06 10:12:27
**/
public function init() {
$this->config = ArrayHelper::merge(
require(__DIR__ . '/config/main.php'),
require(__DIR__ . '/config/main-local.php')
);
}
/**
* @brief 默认的错误behaviors列表,此处主要是追加错误处理behavior
*
* @return public function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/30 16:55:03
**/
public function behaviors() {
return [
ErrorBehavior::className(),
];
}
/**
* @brief 获取某个账户, 如果用户账户不存在,则自动为用户开通一个账号,账户信息不允许被缓存,当开启一个事务时,必须
* 重新加载账号
*
* @return object 用户账户对象
* @see
* @note
* @author 吕宝贵
* @date 2015/12/06 17:59:27
**/
public function getUserAccount($uid, $accountType = UserAccount::ACCOUNT_TYPE_NORMAL) {
if (empty($uid)) {
$this->addError('display-error', '提交的用户id为空');
return false;
}
$userAccount = UserAccount::findOne($uid);
if (!$userAccount) {
$userAccount = UserAccount::createAccount($uid, $accountType);
if (! $userAccount) {
$this->addError('display-error', '为用户开户失败');
return false;
}
}
return $userAccount;
}
/**
* @brief 每个交易最后确认交易完成时所进行的操作,主要为打款给目的用户,分润,收取管理费等
*
* @return protected function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/06 17:31:52
**/
protected function finishPayTrans($trans) {
//收款账户处理逻辑
Yii::warning('进入卖家结算逻辑', __METHOD__);
$sellerAccount = $this->getUserAccount($trans->to_uid);
if (! $this->plus($sellerAccount->uid, $trans->money, $trans->id, '产品售卖收入')) {
Yii::warning('卖家结算失败', __METHOD__);
return false;
}
Yii::warning('卖家结算成功', __METHOD__);
//分润账号处理逻辑
Yii::warning('进入平台利润结算', __METHOD__);
if ($trans->profit > 0) {
if (!$this->processProfit('pay', $trans)) {
Yii::warning('平台利润结算失败', __METHOD__);
$this->addError('display-error', '处理利润收入失败');
return false;
}
}
Yii::warning('平台利润结算成功', __METHOD__);
//手续费逻辑处理
if ($trans->fee > 0) {
if (! $this->processFee('pay', $trans)) {
Yii::warning('手续费结算失败', __METHOD__);
return false;
}
Yii::warning('手续费结算成功', __METHOD__);
}
Yii::warning('结算成功', __METHOD__);
return true;
}
/**
* @brief 退款的后续处理操作,完成交易和付款成功有不同的退款逻辑, 此方法主要完成退款后买方和手续费等处理操作
*
* @return protected function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/06 18:02:59
**/
protected function finishRefundTrans($trans) {
//退款是否收取手续费,可以在这里做逻辑判断,此处退款退给用户多少钱,需要确定
$buyerAccount = UserAccount::findOne($trans->from_uid);
if (!$this->plus($buyerAccount->uid, $trans->total_money - $trans->earnest_money, $trans->id, '订单退款')) {
$this->addError('display-error', '为用户退款时发生错误');
return false;
}
//对于支付交易已经完成的订单,需要退款手续费,利润,还有保证金等操作,一期先不做。
if ($trans->status === Trans::PAY_STATUS_FINISHED) {
$this->addError('display-error', '交易已经确认完成,目前不支持此种退款, 如果需要申诉,请联系客服处理');
return false;
}
//保存交易状态
$trans->status = Trans::PAY_STATUS_REFUNDED;
if ($trans->save()) {
return true;
}
else {
$this->addError(__METHOD__, '保存交易状态变更时出错');
return false;
}
}
/**
* @brief
*
* @return protected function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/05 12:45:03
**/
protected function processProfit($action, $trans) {
$profitAccount = UserAccount::getProfitAccount();
switch ($action) {
case 'pay': {
return $this->plus($profitAccount->uid, $trans->profit, $trans->id, '利润收入');
break;
}
case 'refund': {
return $this->minus($profitAccount->uid, $trans->profit, $trans->id, '利润退款');
break;
}
default:break;
return false;
}
return false;
}
/**
* @brief
*
* @return
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
protected function processFee($action, $trans) {
$feeAccount = UserAccount::getFeeAccount();
switch ($action) {
case 'pay': {
$this->plus($feeAccount->uid, $trans->fee, $trans->id, '手续费收入');
break;
}
case 'refund': {
$this->minus($feeAccount->uid, $trans->fee, $trans->id, '手续费退款');
break;
}
default:break;
}
}
/**
* @brief 用户帐户增加描述为description的$money,关联交易为transId
*
* @return
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
public function plus($uid, $money, $transId, $description, $currency = 1 ) {
Yii::warning($uid . ' income ' . $money, __METHOD__);
return $this->balance($uid, UserAccount::BALANCE_TYPE_PLUS, $money, $transId, $description, $currency);
}
/**
* @brief 用户帐户减少描述为description的$money
*
* @return protected function
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
public function minus($uid, $money, $transId, $description, $currency = 1) {
Yii::warning($uid . ' outcome ' . $money, __METHOD__);
return $this->balance($uid, UserAccount::BALANCE_TYPE_MINUS, $money, $transId, $description, $currency);
}
/**
* @brief 用户账户需要冻结$money的金额,UserAccount类中也应该有freeze方法
*
* @return protected function
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
public function freeze($uid, $money, $type, $sourceId, $description, $currency = 1) {
//产生冻结记录
$freeze = new Freeze();
$freeze->uid = $uid;
$freeze->type = $type;
$freeze->money = $money;
$freeze->currency = $currency;
$freeze->source_id = $sourceId ? $sourceId : 0;
$freeze->status = Freeze::FREEZE_STATUS_FREEZING;
$freeze->description = $description;
if ($freeze->save()) {
//账户结算
return $this->balance($uid, UserAccount::BALANCE_TYPE_FREEZE, $money, $freeze->id, $description, $currency);
}
else {
$this->addErrors($freeze->getErrors);
return false;
}
}
/**
* @brief 用户帐户需要解除交易id为$transId的冻结记录
*
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
public function unFreeze($uid, $freezeId, $description, $currency) {
//获取trans相对应的freeze记录,并解除冻结
$freeze = Freeze::findOne(['uid'=>$uid, 'trans_id'=>$transId]);
if (empty($freeze)) {
$this->addError(__METHOD__, '该交易并没有关联的冻结记录');
return false;
}
//如果解除冻结操作成功,则对账户进行操作
if ($freeze->unFreeze()) {
return $this->balance($uid, UserAccount::BALANCE_TYPE_UNFREEZE, $freeze->money, $freeze->id, $description, $currency);
}
else {
$this->addErrors($freeze->getErrors());
return false;
}
}
/**
* @brief 用户帐户需要完成交易id为$transId的冻结记录
*
* @return protected function
* @retval
* @author 吕宝贵
* @date 2015/12/05 12:45:52
**/
public function finishFreeze($uid, $transId) {
//获取trans相对应的freeze记录
$freeze = Freeze::findOne(['uid'=>$uid, 'trans_id'=>$transId]);
if (empty($freeze)) {
$this->addError(__METHOD__, '该交易并没有关联的冻结记录');
return false;
}
if ($freeze->finishFreeze()) {
return $this->balance($uid, UserAccount::BALANCE_TYPE_FINISH_FREEZE, $freeze->money, $freeze->id, '完成冻结', $freeze->currency);
}
else {
$this->addErrors($freeze->getErrors());
return false;
}
}
/**
* @brief 用户帐户变动的核心函数,所有的变动都需要通过此函数来记录
*
* @return public function
* @retval
* @see
* @note
* @author 吕宝贵
* @date 2015/12/30 14:39:00
**/
protected function balance($uid, $balanceType, $money, $transId, $description, $currency) {
$freezeCat = false;
$freeze = null;
$trans = null;
Yii::warning('进入balance页面', __METHOD__);
Yii::warning('uid and balanceType:' . $uid . '---' . $balanceType, __METHOD__);
if (in_array($balanceType, [UserAccount::BALANCE_TYPE_FREEZE, UserAccount::BALANCE_TYPE_UNFREEZE, UserAccount::BALANCE_TYPE_FINISH_FREEZE])) {
$freezeCat = true;
$freeze = Freeze::findOne($transId);
if (! $freeze) {
$this->addError('trans', '不存在该交易订单');
return false;
}
}
else {
//根据交易号查找对应的交易
$trans = Trans::findOne($transId);
if (! $trans) {
$this->addError('trans', '不存在该交易订单');
return false;
}
}
Yii::warning('获取用户帐户', __METHOD__);
$userAccount = $this->getUserAccount($uid);
Yii::warning('判断账户变更类型', __METHOD__);
switch ($balanceType) {
case UserAccount::BALANCE_TYPE_PLUS : {
$userAccount->plus($money);
break;
}
case UserAccount::BALANCE_TYPE_MINUS : {
$userAccount->minus($money);
break;
}
case UserAccount::BALANCE_TYPE_FREEZE : {
Yii::warning('进入帐号金额冻结');
$userAccount->freeze($money);
break;
}
case UserAccount::BALANCE_TYPE_UNFREEZE : {
$userAccount->unFreeze($money);
break;
}
case UserAccount::BALANCE_TYPE_FINISH_FREEZE : {
$userAccount->finishFreeze($money);
break;
}
default: {
$this->addError(__METHOD__, '不支持提交的账户操作类型');
return false;
}
}
if ($userAccount->save()) {
//记录账单
Yii::warning('产生用户账单', __METHOD__);
$bill = new Bill();
$bill->uid = $userAccount->uid;
if (! $freezeCat) {
$bill->trans_id = $trans->id;
$bill->trans_type_id = $trans->trans_type_id;
$bill->trans_type_name = $trans->transType->name;
}
else {
$bill->trans_id = $freeze->id;
}
$bill->money = $money;
$bill->balance_type = $balanceType;
$bill->currency = $currency;
$bill->description = $description;
if (! $bill->save()) {
$this->addErrors($bill->getErrors());
return false;
}
Yii::warning('产生账户快照', __METHOD__);
//账户快照产生
$accountLog = new UserAccountLog();
$accountLog->uid = $userAccount->uid;
$accountLog->account_type = $userAccount->type;
$accountLog->currency = $currency;
if (! $freezeCat) {
$accountLog->trans_id = $trans->id;
}
else {
$accountLog->trans_id = $freeze->id;
}
$accountLog->balance = $userAccount->balance;
$accountLog->deposit = $userAccount->deposit;
$accountLog->frozen_money = $userAccount->frozen_money;
$accountLog->balance_type = $balanceType;
$accountLog->trans_money = $money;
$accountLog->trans_desc = $description;
Yii::warning('保存账户信息', __METHOD__);
if (! $accountLog->save()) {
//$this->addErrors($accountLog->getErrors());
Yii::warning('保存账户快照失败', __METHOD__);
Yii::warning($accountLog->getErrors(), __METHOD__);
return false;
}
else {
Yii::warning('保存账户快照成功', __METHOD__);
return true;
}
}
else {
return false;
}
}
}