PurchaseClearController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $ids = $post['id'];
  16. $data['merchantId'] = $this->sjId;
  17. $data['id'] = $ids;
  18. $respond = PurchaseClearClass::addClear($data);
  19. util::success($respond);
  20. }
  21. public function actionWxPay()
  22. {
  23. ini_set('date.timezone', 'Asia/Shanghai');
  24. $post = Yii::$app->request->post();
  25. $couponId = $post['couponId'] ?? 0;
  26. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  27. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn]);
  28. if (empty($order)) {
  29. util::fail('订单号无效');
  30. }
  31. $id = $order['id'];
  32. $name = '1123';
  33. $totalFee = $order['actPrice'];
  34. //强制使用小程序的miniOpenId
  35. $openId = $this->admin['miniOpenId'];
  36. $typeList = dict::getConfig('capitalType');
  37. $capitalType = $typeList['xhPurchase']['id'];
  38. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  39. $wxPayType = 0;
  40. if (httpUtil::isMiniProgram()) {
  41. $wxPayType = 1;
  42. }
  43. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  44. //订单30分钟后过期
  45. $now = time();
  46. $expireTime = $now + 1800;
  47. $wx = Yii::getAlias("@vendor/weixin");
  48. require_once($wx . '/lib/WxPay.Api.php');
  49. require_once($wx . '/example/WxPay.JsApiPay.php');
  50. $input = new \WxPayUnifiedOrder();
  51. $input->SetBody($name);
  52. $input->SetOut_trade_no($orderSn);
  53. $input->SetTotal_fee($totalFee * 100);
  54. $input->SetTime_start(date("YmdHis", $now));
  55. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  56. $input->SetTime_expire(date("YmdHis", $expireTime));
  57. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  58. $input->SetTrade_type("JSAPI");
  59. //花卉宝代为申请的微信支付
  60. $merchantExtend = WxOpenClass::getWxInfo();
  61. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  62. $input->SetSub_openid($openId);
  63. } else {
  64. $input->SetOpenid($openId);
  65. }
  66. //强制使用小程序的miniAppId
  67. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  68. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  69. $tools = new \JsApiPay();
  70. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  71. $newParams = json_decode($jsApiParameters, true);
  72. $current = date("Y-m-d H:i:s");
  73. $updateData = ['deadline' => $current, 'changePrice' => 2];
  74. PurchaseClearClass::updateById($id, $updateData);
  75. util::success($newParams);
  76. }
  77. }