PurchaseClearController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizHd\purchase\classes\PurchaseClearClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use common\components\dict;
  7. use Yii;
  8. use common\components\util;
  9. class PurchaseClearController extends BaseController
  10. {
  11. public $guestAccess = ['wx-pay'];
  12. //生成结帐订单 ssh 2021.1.26
  13. public function actionCreateOrder()
  14. {
  15. $post = Yii::$app->request->post();
  16. $current = time();
  17. $ghsId = $post['ghsId'] ?? 0;
  18. $ghs = GhsClass::getById($ghsId, true);
  19. GhsClass::valid($ghs, $this->shopId);
  20. //查找有没结帐订单还没有过期
  21. $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
  22. if (!empty($list)) {
  23. foreach ($list as $item) {
  24. $deadline = $item->deadline;
  25. $deadTime = strtotime($deadline);
  26. if ($current > $deadTime) {
  27. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  28. $item->save();
  29. } else {
  30. util::success(['hasUnComplete' => 1], '还有待完成结账单');
  31. }
  32. }
  33. }
  34. $post['ghsShopId'] = $ghs->shopId ?? 0;
  35. $post['sjId'] = $this->sjId;
  36. $post['shopId'] = $this->shopId;
  37. $post['clearStyle'] = dict::getDict('clearStyle', 'hd2Gys');
  38. $post['customShopAdminId'] = $this->shopAdminId ?? 0;
  39. $post['customShopId'] = $this->shopId ?? 0;
  40. $shopAdmin = $this->shopAdmin;
  41. $shopAdminName = $shopAdmin['name'] ?? '';
  42. $post['customShopAdminName'] = $shopAdminName;
  43. $respond = PurchaseClearClass::addOrder($post, $ghs);
  44. //前端显示的付款倒计时
  45. $hasTime = dict::getDict('order_pay_has_time');
  46. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  47. $remainSecond = $hasTime - $aheadTime;
  48. $respond['remainSecond'] = $remainSecond;
  49. util::success($respond);
  50. }
  51. public function actionWxPay()
  52. {
  53. ini_set('date.timezone', 'Asia/Shanghai');
  54. $post = Yii::$app->request->post();
  55. $couponId = $post['couponId'] ?? 0;
  56. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  57. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  58. if (empty($order)) {
  59. util::fail('账单号无效');
  60. }
  61. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  62. util::fail('账单已结清了');
  63. }
  64. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  65. util::fail('账单已取消了');
  66. }
  67. $current = time();
  68. $id = $order->id;
  69. $deadline = $order->deadline;
  70. if ($current > strtotime($deadline)) {
  71. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  72. $order->save();
  73. util::fail('订单已经失效,请重新发起结算');
  74. }
  75. $name = '采购单结算';
  76. $totalFee = $order->actPrice;
  77. //强制使用小程序的miniOpenId
  78. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  79. if (empty($openId)) {
  80. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  81. $openId = $post['currentMiniOpenId'];
  82. }
  83. }
  84. if (empty($openId)) {
  85. util::fail('没有openId');
  86. }
  87. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  88. //小程序支付
  89. $wxPayType = 1;
  90. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  91. //订单30分钟后过期
  92. $expireTime = $current + 1800;
  93. $wx = Yii::getAlias("@vendor/weixin");
  94. require_once($wx . '/lib/WxPay.Api.php');
  95. require_once($wx . '/example/WxPay.JsApiPay.php');
  96. $input = new \WxPayUnifiedOrder();
  97. $input->SetBody($name);
  98. $input->SetOut_trade_no($orderSn);
  99. $input->SetTotal_fee($totalFee * 100);
  100. $input->SetTime_start(date("YmdHis", $current));
  101. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  102. $input->SetTime_expire(date("YmdHis", $expireTime));
  103. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  104. $input->SetTrade_type("JSAPI");
  105. //花卉宝代为申请的微信支付
  106. $merchantExtend = WxOpenClass::getWxInfo();
  107. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  108. $input->SetSub_openid($openId);
  109. } else {
  110. $input->SetOpenid($openId);
  111. }
  112. //强制使用小程序的miniAppId
  113. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  114. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  115. $tools = new \JsApiPay();
  116. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  117. $newParams = json_decode($jsApiParameters, true);
  118. $newParams['id'] = $id;
  119. $newParams['orderSn'] = $orderSn;
  120. util::success($newParams);
  121. }
  122. }