PurchaseClearController.php 3.7 KB

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