PurchaseClearController.php 4.5 KB

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