PurchaseClearController.php 4.4 KB

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