easywechat

<?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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值