PurchaseClearController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\purchase\classes\PurchaseClearClass;
  4. use bizHd\wx\classes\WxOpenClass;
  5. use common\components\dict;
  6. use common\components\httpUtil;
  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. //查找还有没待清算的订单
  17. $list = PurchaseClearClass::getAllByCondition(['shopId' => $this->shopId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
  18. if (!empty($list)) {
  19. foreach ($list as $item) {
  20. $deadline = $item->deadline;
  21. $deadTime = strtotime($deadline);
  22. if ($current > $deadTime) {
  23. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  24. $item->save();
  25. } else {
  26. util::fail('您还有结算订单没有付款');
  27. }
  28. }
  29. }
  30. $post['sjId'] = $this->sjId;
  31. $post['shopId'] = $this->shopId;
  32. $post['type'] = 1;
  33. $respond = PurchaseClearClass::addOrder($post);
  34. util::success($respond);
  35. }
  36. public function actionWxPay()
  37. {
  38. ini_set('date.timezone', 'Asia/Shanghai');
  39. $post = Yii::$app->request->post();
  40. $couponId = $post['couponId'] ?? 0;
  41. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  42. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  43. if (empty($order)) {
  44. util::fail('订单号无效');
  45. }
  46. $current = time();
  47. $deadline = $order->deadline;
  48. if ($current > strtotime($deadline)) {
  49. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  50. $order->save();
  51. util::fail('订单已经失效,请重新发起结算');
  52. }
  53. $name = '采购单结算';
  54. $totalFee = $order->actPrice;
  55. //强制使用小程序的miniOpenId
  56. $openId = $this->admin['miniOpenId'];
  57. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  58. //小程序支付
  59. $wxPayType = 1;
  60. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  61. //订单30分钟后过期
  62. $expireTime = $current + 1800;
  63. $wx = Yii::getAlias("@vendor/weixin");
  64. require_once($wx . '/lib/WxPay.Api.php');
  65. require_once($wx . '/example/WxPay.JsApiPay.php');
  66. $input = new \WxPayUnifiedOrder();
  67. $input->SetBody($name);
  68. $input->SetOut_trade_no($orderSn);
  69. $input->SetTotal_fee($totalFee * 100);
  70. $input->SetTime_start(date("YmdHis", $current));
  71. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  72. $input->SetTime_expire(date("YmdHis", $expireTime));
  73. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  74. $input->SetTrade_type("JSAPI");
  75. //花卉宝代为申请的微信支付
  76. $merchantExtend = WxOpenClass::getWxInfo();
  77. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  78. $input->SetSub_openid($openId);
  79. } else {
  80. $input->SetOpenid($openId);
  81. }
  82. //强制使用小程序的miniAppId
  83. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  84. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  85. $tools = new \JsApiPay();
  86. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  87. $newParams = json_decode($jsApiParameters, true);
  88. util::success($newParams);
  89. }
  90. }