PurchaseClearController.php 4.3 KB

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