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