RechargeController.php 3.1 KB

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