12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace app\mobile\logic;
- //关于微信相关操作的工具类
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Config;
- use think\facade\Validate;
- class WechatLogic
- {
- /**
- * //获取微信网页授权URL
- * //$param $callback string 微信回跳地址(接口已经默认url_encode处理,授权成功会有$_GET['code']值,可用于下个步骤)
- * //$param $state string 重定向后会带上state参数(开发者可以填写a-zA-Z0-9的参数值,最多128字节)
- * //$param $scope string 应用授权作用域(snsapi_base | snsapi_userinfo)
- * public static function getOauthRedirect(string $callback = '', string $state = '', string $scope = 'snsapi_base'): string
- * {
- *
- * $val = Validate::rule([
- * 'callback|微信回跳地址' => 'require|url',
- * 'state|携带参数' => 'require|length:1,255',
- * 'scope|应用授权作用域' => 'require|in:snsapi_base,snsapi_userinfo'
- * ]);
- *
- * if (!$val->check(['callback' => $callback, 'state' => $state, 'scope' => $scope])) throw new ValidateException($val->getError());
- *
- * $oauth = &load_wechat('Oauth');
- *
- * $rs = $oauth->getOauthRedirect($callback, $state, $scope);
- *
- * halt('调用成功', $rs);
- *
- * }
- *
- *
- * //通过code换取网页授权access_token(和openId)
- * public static function getOauthAccessToken()
- * {
- *
- * $oauth = &load_wechat('Oauth');
- *
- * $rs = $oauth->getOauthAccessToken();
- *
- * halt('最终结果:', $rs);
- * }
- *
- *
- * //获取预支付信息
- * public static function getPrepayId(string $openid = '', string $body = '', string $out_trade_no = '', float $total_fee = 0, string $notify_url = '', string $trade_type = "JSAPI")
- * {
- *
- * if ($notify_url == '') $notify_url = env('notify_url');
- *
- * $pay = &load_wechat('Pay');
- *
- * $rs = $pay->getPrepayId($openid, $body, $out_trade_no, bcmul($total_fee, '100'), $notify_url, $trade_type);
- *
- * halt('预支付结果:', $rs);
- * }
- *
- *
- * //支付通知回调
- * public static function Notify()
- * {
- *
- * //实例化支付接口
- * $pay = &load_wechat('Pay');
- *
- * //获取支付通知
- * $notifyInfo = $pay->getNotify();
- *
- * if ($notifyInfo === false) throw new Exception('获取微信支付通知数据失败,' . $pay->errMsg);
- * elseif ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') return $notifyInfo;// 支付状态完全成功,可以更新订单的支付状态了
- * else throw new Exception('获取微信支付通知数据失败,' . json_encode($notifyInfo, JSON_UNESCAPED_UNICODE));
- *
- * }
- *
- *
- * //@todo 校验订单状态,确认支付结果
- * public static function checkPayResult(string $out_trade_no = '')
- * {
- * $pay = &load_wechat('Pay');
- *
- * $rs = $pay->queryOrder($out_trade_no);
- *
- * halt('订单详情:', $rs);
- *
- * }
- **/
- }
|