PurchaseClearController.php 4.6 KB

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