RechargeController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = ['mainId' => $this->mainId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  17. $list = RechargeClass::getRechargeList($where);
  18. util::success($list);
  19. }
  20. //客户充值记录 ssh 20211005
  21. public function actionCustomList()
  22. {
  23. $where = ['ghsShopId' => $this->shopId, 'payStatus' => RechargeClass::PAY_STATUS_HAS_PAY];
  24. $list = RechargeClass::getRechargeList($where);
  25. util::success($list);
  26. }
  27. //充值余额 ssh 2021.2.21
  28. public function actionBalance()
  29. {
  30. ini_set('date.timezone', 'Asia/Shanghai');
  31. $get = Yii::$app->request->get();
  32. $amount = $get['amount'] ?? 500;
  33. $wxPay = dict::getDict('payWay', 'wxPay');
  34. $shopAdmin = $this->shopAdmin->attributes;
  35. $shopAdminName = $shopAdmin['name'] ?? '';
  36. $data = [
  37. 'amount' => $amount,
  38. 'payWay' => $wxPay,
  39. 'sjId' => $this->sjId,
  40. 'sjStyle' => SjClass::STYLE_RETAIL,
  41. 'shopId' => $this->shopId,
  42. 'mainId' => $this->mainId,
  43. 'modPrice' => 0,
  44. 'remark' => '',
  45. 'shopAdminId' => $this->shopAdminId,
  46. 'shopAdminName' => $shopAdminName,
  47. ];
  48. $order = RechargeClass::addOrder($data, true);
  49. $orderSn = $order->orderSn;
  50. $name = $order->orderName ?? '充值';
  51. $totalFee = $order->amount;
  52. //强制使用小程序的miniOpenId
  53. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  54. if (empty($openId)) {
  55. util::fail('没有找到openId');
  56. }
  57. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  58. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  59. $wxPayType = httpUtil::isMiniProgram() ? 1 : 0;
  60. $attach = "couponId=0&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  61. //订单30分钟后过期
  62. $now = time();
  63. $expireTime = $now + 1800;
  64. $wx = Yii::getAlias("@vendor/weixin");
  65. require_once($wx . '/lib/WxPay.Api.php');
  66. require_once($wx . '/example/WxPay.JsApiPay.php');
  67. $input = new \WxPayUnifiedOrder();
  68. $input->SetBody($name);
  69. $input->SetOut_trade_no($orderSn);
  70. $input->SetTotal_fee($totalFee * 100);
  71. $input->SetTime_start(date("YmdHis", $now));
  72. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  73. $input->SetTime_expire(date("YmdHis", $expireTime));
  74. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  75. $input->SetTrade_type("JSAPI");
  76. $merchantExtend = WxOpenClass::getGhsWxInfo();
  77. $input->SetOpenid($openId);
  78. //强制使用小程序的AppId
  79. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  80. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  81. $tools = new \JsApiPay();
  82. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  83. $newParams = json_decode($jsApiParameters, true);
  84. util::success($newParams);
  85. }
  86. }