| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace addons\YunfastPay\common\services;
- use addons\Store\common\services\MerchantService;
- use addons\YunfastPay\common\models\AgentOrder;
- use addons\YunfastPay\common\models\SplitbillItem;
- use addons\YunfastPay\common\services\trans\nocash\AgentTrans;
- use Exception;
- /**
- * StoreService class
- * @package addons\YunfastPay\common\services
- */
- class StoreService extends BaseService
- {
- /**
- * 通过分账ID进行代付
- *
- * @param integer $id
- * @return void
- */
- public function agentBySplitBillLogId(int $id)
- {
- $service = new YunPayService(['mall_id' => $this->mall_id]);
- $log = SplitbillItem::getLogById($id);
- if (empty($log)) throw new Exception('记录%s不存在');
- if ($log->hasAgentOrder()) return false;
- $service->setStoreId($log->store_id);
- list($name, $bank_account, $bank_account_type) = $this->getStoreBankInfo($log->store_id);
- $trans = new AgentTrans(
- 'SW' . date('YmdHis') . mt_rand(1000, 9999),
- $log->amount,
- $bank_account,
- $name,
- '门店分账快速到账'
- );
- // 如果设置账户类型
- if (!empty($bank_account_type)) {
- $trans->setAcctType($bank_account_type);
- }
- // 执行代付
- $res = $service->sendPayRq($trans);
- // 添加代付记录
- $reqData = $service->getReqData();
- /**
- * @var AgentOrder
- */
- $model = AgentOrder::addLog([
- 'type' => 2,
- 'mall_id' => $this->mall_id,
- 'user_id' => $log->store_id,
- 'withdraw_id' => $log->id,
- 'trans_amount' => $reqData['transAmt'],
- 'order_no' => $reqData['outOrderId'],
- 'acct_number' => $reqData['settleAcct'],
- 'acct_name' => $reqData['settleAcctNm'],
- 'acct_type' => $trans->getAcctType(),
- 'req_data' => $reqData,
- 'res_data' => $res,
- 'status' => $res['transStatus'] ?? '',
- ]);
- // 申请失败
- if ($res['respCode'] != '0000') {
- $msg = $res['respMsg'];
- $model->failByStore();
- return $this->error($msg);
- } else {
- // 打款失败
- if ($res['transStatus'] == '102') {
- $msg = $res['respMsg'];
- $model->failByStore();
- return $this->error($msg);
- }
- // 打款成功
- if ($res['transStatus'] == '100') {
- $model->successByStore();
- return $log->id;
- }
- }
- }
- /**
- * 获取门店银行卡信息
- *
- * @param integer $store_id
- * @return array
- * @throws Exception
- */
- public function getStoreBankInfo(int $store_id)
- {
- $merchant = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $store_id]);
- $bank_info = $merchant->getBankInfo();
- if (empty($bank_info['bank_account'])) throw new Exception(sprintf('门店%s未设置银行卡信息', $store_id));
- return [
- $bank_info['name'],
- $bank_info['bank_account'],
- $bank_info['bank_account_type']
- ];
- }
- }
|