PurchaseClearController.php 4.9 KB

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