RechargeController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\saas\services\RegionService;
  4. use common\components\util;
  5. class RechargeController extends BaseController
  6. {
  7. //充值记录 shish 2021.2.21
  8. public function actionList()
  9. {
  10. $where = ['shopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  11. $list = RechargeClass::getRechargeList($where);
  12. util::success($list);
  13. }
  14. //充值余额 shish 2021.2.21
  15. public function actionBalance()
  16. {
  17. ini_set('date.timezone', 'Asia/Shanghai');
  18. $get = Yii::$app->request->get();
  19. $amount = $get['amount'] ?? 500;
  20. $wxPay = configDict::getConfig('payWay', 'weixinPay');
  21. $data = [
  22. 'amount' => $amount,
  23. 'payWay' => $wxPay,
  24. 'sjId' => $this->sjId,
  25. 'sjStyle' => MerchantClass::STYLE_RETAIL,
  26. 'shopId' => $this->shopId,
  27. 'modPrice' => 0,
  28. 'remark' => '',
  29. ];
  30. $order = RechargeClass::addOrder($data);
  31. $orderSn = $order['orderSn'];
  32. $name = $order['orderName'] ?? '充值';
  33. $totalFee = $order['amount'];
  34. //强制使用小程序的miniOpenId
  35. $openId = $this->admin['miniOpenId'];
  36. $capitalTypeData = configDict::getConfig('capitalType');
  37. $capitalType = $capitalTypeData['xhRecharge']['id'];
  38. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  39. $wxPayType = 0;
  40. if (httpUtil::isMiniProgram()) {
  41. $wxPayType = 1;
  42. }
  43. $attach = "couponId=0&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['shopHost'] . '/notice/wx-callback/');
  58. $input->SetTrade_type("JSAPI");
  59. $merchantExtend = WxOpenClass::getWxInfo();
  60. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  61. $input->SetSub_openid($openId);
  62. } else {
  63. $input->SetOpenid($openId);
  64. }
  65. //强制使用小程序的AppId
  66. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  67. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  68. $tools = new \JsApiPay();
  69. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  70. $newParams = json_decode($jsApiParameters, true);
  71. util::success($newParams);
  72. }
  73. }