RechargeController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\sj\classes\MerchantClass;
  4. use biz\recharge\classes\RechargeClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use common\components\configDict;
  7. use common\components\httpUtil;
  8. use common\components\util;
  9. use Yii;
  10. class RechargeController extends BaseController
  11. {
  12. //充值记录 shish 2021.2.21
  13. public function actionList()
  14. {
  15. $where = ['shopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  16. $list = RechargeClass::getRechargeList($where);
  17. util::success($list);
  18. }
  19. //充值余额 shish 2021.2.21
  20. public function actionBalance()
  21. {
  22. ini_set('date.timezone', 'Asia/Shanghai');
  23. $get = Yii::$app->request->get();
  24. $amount = $get['amount'] ?? 500;
  25. $wxPay = configDict::getConfig('payWay', 'weixinPay');
  26. $data = [
  27. 'amount' => $amount,
  28. 'payWay' => $wxPay,
  29. 'sjId' => $this->sjId,
  30. 'sjStyle' => MerchantClass::STYLE_RETAIL,
  31. 'shopId' => $this->shopId,
  32. 'modPrice' => 0,
  33. 'remark' => '',
  34. ];
  35. $order = RechargeClass::addOrder($data);
  36. $orderSn = $order['orderSn'];
  37. $name = $order['orderName'] ?? '充值';
  38. $totalFee = $order['amount'];
  39. //强制使用小程序的miniOpenId
  40. $openId = $this->admin['miniOpenId'];
  41. $capitalTypeData = configDict::getConfig('capitalType');
  42. $capitalType = $capitalTypeData['xhRecharge']['id'];
  43. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  44. $wxPayType = 0;
  45. if (httpUtil::isMiniProgram()) {
  46. $wxPayType = 1;
  47. }
  48. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  49. //订单30分钟后过期
  50. $now = time();
  51. $expireTime = $now + 1800;
  52. $wx = Yii::getAlias("@vendor/weixin");
  53. require_once($wx . '/lib/WxPay.Api.php');
  54. require_once($wx . '/example/WxPay.JsApiPay.php');
  55. $input = new \WxPayUnifiedOrder();
  56. $input->SetBody($name);
  57. $input->SetOut_trade_no($orderSn);
  58. $input->SetTotal_fee($totalFee * 100);
  59. $input->SetTime_start(date("YmdHis", $now));
  60. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  61. $input->SetTime_expire(date("YmdHis", $expireTime));
  62. $input->SetNotify_url(Yii::$app->params['shopHost'] . '/notice/wx-callback/');
  63. $input->SetTrade_type("JSAPI");
  64. $merchantExtend = WxOpenClass::getGhsWxInfo();
  65. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  66. $input->SetSub_openid($openId);
  67. } else {
  68. $input->SetOpenid($openId);
  69. }
  70. //强制使用小程序的AppId
  71. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  72. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  73. $tools = new \JsApiPay();
  74. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  75. $newParams = json_decode($jsApiParameters, true);
  76. util::success($newParams);
  77. }
  78. }