PurchaseClearController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 common\components\httpUtil;
  8. use Yii;
  9. use common\components\util;
  10. class PurchaseClearController extends BaseController
  11. {
  12. //生成结帐订单 ssh 2021.1.26
  13. public function actionCreateOrder()
  14. {
  15. $post = Yii::$app->request->post();
  16. $current = time();
  17. $ghsId = $post['ghsId'] ?? 0;
  18. $ghs = GhsClass::getById($ghsId, true);
  19. GhsClass::valid($ghs, $this->shopId);
  20. //查找有没结帐订单还没有过期
  21. $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
  22. if (!empty($list)) {
  23. foreach ($list as $item) {
  24. $deadline = $item->deadline;
  25. $deadTime = strtotime($deadline);
  26. if ($current > $deadTime) {
  27. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  28. $item->save();
  29. } else {
  30. util::fail('请勿频繁提交,2分钟后再操作');
  31. }
  32. }
  33. }
  34. $post['sjId'] = $this->sjId;
  35. $post['shopId'] = $this->shopId;
  36. $post['type'] = 1;
  37. $post['customShopAdminId'] = $this->shopAdminId;
  38. $shopAdmin = $this->shopAdmin;
  39. $shopAdminName = $shopAdmin['name'] ?? '';
  40. $post['customShopAdminName'] = $shopAdminName;
  41. $respond = PurchaseClearClass::addOrder($post, $ghs);
  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. $current = time();
  55. $id = $order->id;
  56. $deadline = $order->deadline;
  57. if ($current > strtotime($deadline)) {
  58. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  59. $order->save();
  60. util::fail('订单已经失效,请重新发起结算');
  61. }
  62. $name = '采购单结算';
  63. $totalFee = $order->actPrice;
  64. //强制使用小程序的miniOpenId
  65. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  66. if (empty($openId)) {
  67. util::fail('没有openId');
  68. }
  69. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  70. //小程序支付
  71. $wxPayType = 1;
  72. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  73. //订单30分钟后过期
  74. $expireTime = $current + 1800;
  75. $wx = Yii::getAlias("@vendor/weixin");
  76. require_once($wx . '/lib/WxPay.Api.php');
  77. require_once($wx . '/example/WxPay.JsApiPay.php');
  78. $input = new \WxPayUnifiedOrder();
  79. $input->SetBody($name);
  80. $input->SetOut_trade_no($orderSn);
  81. $input->SetTotal_fee($totalFee * 100);
  82. $input->SetTime_start(date("YmdHis", $current));
  83. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  84. $input->SetTime_expire(date("YmdHis", $expireTime));
  85. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  86. $input->SetTrade_type("JSAPI");
  87. //花卉宝代为申请的微信支付
  88. $merchantExtend = WxOpenClass::getWxInfo();
  89. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  90. $input->SetSub_openid($openId);
  91. } else {
  92. $input->SetOpenid($openId);
  93. }
  94. //强制使用小程序的miniAppId
  95. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  96. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  97. $tools = new \JsApiPay();
  98. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  99. $newParams = json_decode($jsApiParameters, true);
  100. $newParams['id'] = $id;
  101. $newParams['orderSn'] = $orderSn;
  102. util::success($newParams);
  103. }
  104. }