<?php
namespace App\Http\Classes;
use EasyWeChat\Factory;
class Wechat
{
/**
* @var \EasyWeChat\OfficialAccount\Application
*/
public $app;
/**
* @var \Overtrue\Socialite\Providers\WeChatProvider
*/
public $oauth;
/**
* payment
* @var Factory
*/
public $payment;
public function __construct()
{
$this->app = Factory::officialAccount(config('config.easy_wechat'));
$this->oauth = $this->app->oauth;
$this->payment = Factory::payment(config('config.easy_wechat'));
}
/**
* 获取用户信息
* @return $this|\Overtrue\Socialite\User
*/
public function userInfo()
{
// 获取 OAuth 授权结果用户信息
$user = $this->oauth->user();
return $user;
}
/**
* 根据openid获取用户信息
* @param $openId
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string|mixed
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function getUser($openId)
{
if (!empty($this->app->user)) {
return $this->app->user->get($openId);
}
return null;
}
/**
* Name:发送模板消息
*
* @@param string $wechat_openid 接收用户openid
* @param string $templete_id 模板id
* @param string $url 链接url
* @param array $data 模板键值对数据
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function templeteMsg($wechat_openid, $templete_id, $url, $data)
{
$res = $this->app->template_message->send([
'touser' => $wechat_openid,
'template_id' => $templete_id,
'url' => $url,
'data' => $data
]);
return $res;
}
/**
* Name:jsapi支付
*
* @param $data
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function jsApiPay($data)
{
$result = $this->payment->order->unify(array_merge([
'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
], $data));
if (isset($result['return_code']) && $result['return_code'] == 'FAIL') {
return ['status' => false, 'msg' => $result['return_msg'], 'data' => ''];
} else {
if ($result['result_code'] == 'FAIL') {
return ['status' => false, 'msg' => $result['err_code_des'], 'data' => ''];
} else {
$arr = $this->getBridgeConfig($result['prepay_id']);
return ['status' => true, 'msg' => 'success', 'data' => $arr];
}
}
}
/**
* 获取支付信息
* @param $prepayId
* @return mixed
*/
public function getBridgeConfig($prepayId)
{
return $this->payment->jssdk->bridgeConfig($prepayId);
}
}