YunstAutoCashJob.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\jobs;
  3. use app\constants\OptionSetting;
  4. use app\models\Option;
  5. use app\models\Order;
  6. use app\models\SaasUser;
  7. use app\models\SaasUserPriceLog;
  8. use app\models\SharingReceiver;
  9. use app\models\Store;
  10. use app\models\StoreCash;
  11. use app\models\User;
  12. use app\utils\OrderNo;
  13. use app\utils\yunst\src\OrderYunst;
  14. use yii\base\BaseObject;
  15. use yii\queue\JobInterface;
  16. //通联支付提现失败
  17. class YunstAutoCashJob extends BaseObject implements JobInterface
  18. {
  19. public $retry;
  20. public function execute($queue)
  21. {
  22. if ($this->retry > 0) {
  23. $queue_id = \queue_push(new YunstAutoCashJob(['retry' => $this->retry--]), 900);
  24. }
  25. $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
  26. if ($oldConf){
  27. $oldConf = json_decode($oldConf, true);
  28. if (!empty($oldConf)) {
  29. $customer_number = $oldConf['customer_number'];
  30. //查询平台余额
  31. $orderYunst = new OrderYunst();
  32. $res = $orderYunst->queryAcctInfo($customer_number);
  33. debug_log($res);
  34. $store_amount = 0;
  35. if ($res['subCode'] == 'SUCCESS'){
  36. $store_amount = $res['data']['amount'];//真的
  37. }
  38. if ($store_amount) {
  39. $cash = new StoreCash();
  40. $cash->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  41. $cash->is_delete = 0;
  42. $cash->status = 0;
  43. $cash->price = $store_amount/100;
  44. $cash->created_at = time();
  45. $cash->store_id = -1;
  46. $cash->type =6;//通联提现
  47. $cash->name = '平台提现';
  48. $cash->mobile = '';
  49. $cash->pay_time = 0;
  50. $cash->service_charge = 0;
  51. $cash->cash_type = 0;
  52. if ($cash->save()) {
  53. //调用通联自动提现接口
  54. $orderYunst = new OrderYunst();
  55. $data['bizOrderNo'] = $cash->order_no;
  56. $data['bizUserId'] = $customer_number;//客户号
  57. $data['amount'] = ceil(bcmul($cash->price, 100));
  58. $data['notifyUrl'] = "";
  59. $data['remark'] = "";
  60. $data['authWay'] = "3";
  61. debug_log($data);
  62. $res_cash = $orderYunst->servicerWithdraw($data);
  63. debug_log($res_cash);
  64. if ($res_cash['subCode'] != 'SUCCESS'){
  65. debug_log('平台提现失败');
  66. debug_log($res_cash['subMsg']);
  67. $cash_info2 = StoreCash::findOne($cash->id);
  68. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  69. $cash_info2->order_no = $order_no;
  70. $cash_info2->save();
  71. }else{
  72. $cash_info = StoreCash::findOne($cash->id);
  73. $cash_info->status = 2;
  74. $cash_info->pay_time = time();
  75. $cash_info->save();
  76. debug_log('平台提现成功');
  77. }
  78. }else{
  79. debug_log('提现保存失败');
  80. }
  81. }else{
  82. debug_log('平台提现金额不足');
  83. }
  84. }
  85. }
  86. // 商城列表
  87. $store_list = Store::find()->where(['is_delete' => 0])->andWhere(['!=','cusid',''])->asArray()->all();
  88. foreach($store_list as $store) {
  89. try {
  90. $orderYunst = new OrderYunst();
  91. $res2 = $orderYunst->queryAcctInfo($store['store_number']);
  92. $mch_amount = 0;
  93. if ($res2['subCode'] == 'SUCCESS'){
  94. $mch_amount = $res2['data']['amount'];//真的
  95. }
  96. // print_r($mch_amount/100);die;
  97. if ($mch_amount) {
  98. $cash_mch = new StoreCash();
  99. $cash_mch->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  100. $cash_mch->is_delete = 0;
  101. $cash_mch->status = 0;
  102. $cash_mch->price = $mch_amount/100;
  103. $cash_mch->created_at = time();
  104. $cash_mch->store_id = $store['id'];
  105. $cash_mch->type =6;//通联提现
  106. $cash_mch->name = '商户提现';
  107. $cash_mch->mobile = '';
  108. $cash_mch->pay_time = 0;
  109. $cash_mch->service_charge = 0;
  110. $cash_mch->cash_type = 0;
  111. if ($cash_mch->save()) {
  112. $orderYunst = new OrderYunst();
  113. $data2['bizOrderNo'] = $cash_mch->order_no;
  114. $data2['bizUserId'] = $store['store_number'];
  115. $data2['amount'] = ceil(bcmul($cash_mch->price, 100));
  116. $data2['notifyUrl'] = "";
  117. $data2['remark'] = "";
  118. $data2['authWay'] = "3";
  119. debug_log($data2);
  120. $res_cash2 = $orderYunst->servicerWithdraw($data2);
  121. debug_log($res_cash2);
  122. if ($res_cash2['subCode'] != 'SUCCESS'){
  123. debug_log('商户提现失败');
  124. debug_log($res_cash2['subMsg']);
  125. $cash_info2 = StoreCash::findOne($cash_mch->id);
  126. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  127. $cash_info2->order_no = $order_no;
  128. $cash_info2->save();
  129. }else{
  130. $cash_info2 = StoreCash::findOne($cash_mch->id);
  131. $cash_info2->status = 2;
  132. $cash_info2->pay_time = time();
  133. $cash_info2->save();
  134. debug_log('商户提现成功');
  135. }
  136. }else{
  137. debug_log('提现保存失败2');
  138. }
  139. }else{
  140. debug_log('商户可用余额不足');
  141. }
  142. } catch(\Exception $e) {
  143. \Yii::error('--------auto-execute-cash-error---- '.$store->id.' ----auto-execute-cash-error--------' . $e->getMessage() . $e->getFile() . $e->getLine());
  144. }
  145. }
  146. if (isset($queue_id) && !\Yii::$app->queue->isDone($queue_id)) {
  147. \Yii::$app->queue->remove($queue_id);
  148. }
  149. return [
  150. 'code' => 0,
  151. 'msg' => '执行完毕'
  152. ];
  153. }
  154. }