PurchaseClearController.php 3.6 KB

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