RechargeController.php 3.2 KB

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