StoreService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace addons\YunfastPay\common\services;
  3. use addons\Store\common\services\MerchantService;
  4. use addons\YunfastPay\common\models\AgentOrder;
  5. use addons\YunfastPay\common\models\SplitbillItem;
  6. use addons\YunfastPay\common\services\trans\nocash\AgentTrans;
  7. use Exception;
  8. /**
  9. * StoreService class
  10. * @package addons\YunfastPay\common\services
  11. */
  12. class StoreService extends BaseService
  13. {
  14. /**
  15. * 通过分账ID进行代付
  16. *
  17. * @param integer $id
  18. * @return void
  19. */
  20. public function agentBySplitBillLogId(int $id)
  21. {
  22. $service = new YunPayService(['mall_id' => $this->mall_id]);
  23. $log = SplitbillItem::getLogById($id);
  24. if (empty($log)) throw new Exception('记录%s不存在');
  25. if ($log->hasAgentOrder()) return false;
  26. $service->setStoreId($log->store_id);
  27. list($name, $bank_account, $bank_account_type) = $this->getStoreBankInfo($log->store_id);
  28. $trans = new AgentTrans(
  29. 'SW' . date('YmdHis') . mt_rand(1000, 9999),
  30. $log->amount,
  31. $bank_account,
  32. $name,
  33. '门店分账快速到账'
  34. );
  35. // 如果设置账户类型
  36. if (!empty($bank_account_type)) {
  37. $trans->setAcctType($bank_account_type);
  38. }
  39. // 执行代付
  40. $res = $service->sendPayRq($trans);
  41. // 添加代付记录
  42. $reqData = $service->getReqData();
  43. /**
  44. * @var AgentOrder
  45. */
  46. $model = AgentOrder::addLog([
  47. 'type' => 2,
  48. 'mall_id' => $this->mall_id,
  49. 'user_id' => $log->store_id,
  50. 'withdraw_id' => $log->id,
  51. 'trans_amount' => $reqData['transAmt'],
  52. 'order_no' => $reqData['outOrderId'],
  53. 'acct_number' => $reqData['settleAcct'],
  54. 'acct_name' => $reqData['settleAcctNm'],
  55. 'acct_type' => $trans->getAcctType(),
  56. 'req_data' => $reqData,
  57. 'res_data' => $res,
  58. 'status' => $res['transStatus'] ?? '',
  59. ]);
  60. // 申请失败
  61. if ($res['respCode'] != '0000') {
  62. $msg = $res['respMsg'];
  63. $model->failByStore();
  64. return $this->error($msg);
  65. } else {
  66. // 打款失败
  67. if ($res['transStatus'] == '102') {
  68. $msg = $res['respMsg'];
  69. $model->failByStore();
  70. return $this->error($msg);
  71. }
  72. // 打款成功
  73. if ($res['transStatus'] == '100') {
  74. $model->successByStore();
  75. return $log->id;
  76. }
  77. }
  78. }
  79. /**
  80. * 获取门店银行卡信息
  81. *
  82. * @param integer $store_id
  83. * @return array
  84. * @throws Exception
  85. */
  86. public function getStoreBankInfo(int $store_id)
  87. {
  88. $merchant = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $store_id]);
  89. $bank_info = $merchant->getBankInfo();
  90. if (empty($bank_info['bank_account'])) throw new Exception(sprintf('门店%s未设置银行卡信息', $store_id));
  91. return [
  92. $bank_info['name'],
  93. $bank_info['bank_account'],
  94. $bank_info['bank_account_type']
  95. ];
  96. }
  97. }