PurchaseClearController.php 4.1 KB

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