RechargeController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\user\services\UserService;
  4. use common\components\httpUtil;
  5. use common\components\stringUtil;
  6. use common\components\util;
  7. use common\services\xhRechargeService;
  8. use Yii;
  9. class RechargeController extends BaseController
  10. {
  11. //发起充值 shish 2019.12.19
  12. public function actionLaunch()
  13. {
  14. $rechargeAmount = Yii::$app->request->get('amount', 1);
  15. $account = $this->sjId;
  16. $sj = $this->sj;
  17. $extend = $this->sjExtend;
  18. $user = $this->user;
  19. ini_set('date.timezone', 'Asia/Shanghai');
  20. if ($rechargeAmount <= 0) {
  21. util::fail('请填写充值金额');
  22. }
  23. $rechargeAmount = round($rechargeAmount, 2);//四舍五入,保留二位小数
  24. $userId = $user['id'];
  25. $rechargeData['userId'] = $userId;
  26. $rechargeData['actPrice'] = $rechargeAmount;
  27. $rechargeData['payStyle'] = 0;
  28. $rechargeData['merchantId'] = $sj['id'];
  29. $now = time();
  30. $expireTime = $now + 300;//订单5分钟后过期
  31. $rechargeData['createTime'] = date("Y-m-d H:i:s", $now);
  32. $rechargeData['addTime'] = time();
  33. $rechargeData['deadline'] = $expireTime;
  34. $rechargeData['id'] = stringUtil::generateOrderNo($account, $userId);
  35. $rechargeData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
  36. $payment = xhRechargeService::add($rechargeData);
  37. $orderId = $payment['id'];
  38. $orderSn = $payment['orderSn'];
  39. UserService::updateVisitTime($sjId, $userId);
  40. $totalFee = $rechargeAmount;
  41. $name = '充值' . $totalFee . '元';
  42. //小程序使用miniOpenId
  43. if (httpUtil::isMiniProgram()) {
  44. $openId = $user['miniOpenId'];
  45. } else {
  46. $openId = $user['openId'];
  47. }
  48. if (empty($openId)) {
  49. util::fail('没有找到用户信息 wx');
  50. }
  51. $typeList = dict::getConfig('capitalType');
  52. $capitalType = $typeList['xhRecharge']['id'];
  53. $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
  54. $wx = Yii::getAlias("@vendor/weixin");
  55. require_once($wx . '/lib/WxPay.Api.php');
  56. require_once($wx . '/example/WxPay.JsApiPay.php');
  57. $input = new \WxPayUnifiedOrder();
  58. $input->SetBody($name);
  59. $input->SetOut_trade_no($orderSn);
  60. $input->SetTotal_fee($totalFee * 100);
  61. $input->SetTime_start(date("YmdHis", $now));
  62. $input->SetAttach($attach);
  63. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  64. $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/weixin-recharge-callback/');
  65. $input->SetTrade_type("JSAPI");
  66. //服务商 代设置微信支付,要添加的参数设置
  67. if ($extend['wxPayApply'] == 1) {
  68. //$input->SetOpenid($openId);
  69. $input->SetSub_openid($openId);
  70. //自设置 微信支付
  71. } else {
  72. $input->SetOpenid($openId);
  73. }
  74. //小程序使用miniAppId
  75. if(httpUtil::isMiniProgram()){
  76. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  77. }
  78. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
  79. $tools = new \JsApiPay();
  80. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
  81. $newParams = json_decode($jsApiParameters, true);
  82. $newParams['orderSn'] = $orderSn;
  83. util::success($newParams);
  84. }
  85. }