WechatLogic.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\mobile\logic;
  3. //关于微信相关操作的工具类
  4. use think\Exception;
  5. use think\exception\ValidateException;
  6. use think\facade\Config;
  7. use think\facade\Validate;
  8. class WechatLogic
  9. {
  10. /**
  11. * //获取微信网页授权URL
  12. * //$param $callback string 微信回跳地址(接口已经默认url_encode处理,授权成功会有$_GET['code']值,可用于下个步骤)
  13. * //$param $state string 重定向后会带上state参数(开发者可以填写a-zA-Z0-9的参数值,最多128字节)
  14. * //$param $scope string 应用授权作用域(snsapi_base | snsapi_userinfo)
  15. * public static function getOauthRedirect(string $callback = '', string $state = '', string $scope = 'snsapi_base'): string
  16. * {
  17. *
  18. * $val = Validate::rule([
  19. * 'callback|微信回跳地址' => 'require|url',
  20. * 'state|携带参数' => 'require|length:1,255',
  21. * 'scope|应用授权作用域' => 'require|in:snsapi_base,snsapi_userinfo'
  22. * ]);
  23. *
  24. * if (!$val->check(['callback' => $callback, 'state' => $state, 'scope' => $scope])) throw new ValidateException($val->getError());
  25. *
  26. * $oauth = &load_wechat('Oauth');
  27. *
  28. * $rs = $oauth->getOauthRedirect($callback, $state, $scope);
  29. *
  30. * halt('调用成功', $rs);
  31. *
  32. * }
  33. *
  34. *
  35. * //通过code换取网页授权access_token(和openId)
  36. * public static function getOauthAccessToken()
  37. * {
  38. *
  39. * $oauth = &load_wechat('Oauth');
  40. *
  41. * $rs = $oauth->getOauthAccessToken();
  42. *
  43. * halt('最终结果:', $rs);
  44. * }
  45. *
  46. *
  47. * //获取预支付信息
  48. * public static function getPrepayId(string $openid = '', string $body = '', string $out_trade_no = '', float $total_fee = 0, string $notify_url = '', string $trade_type = "JSAPI")
  49. * {
  50. *
  51. * if ($notify_url == '') $notify_url = env('notify_url');
  52. *
  53. * $pay = &load_wechat('Pay');
  54. *
  55. * $rs = $pay->getPrepayId($openid, $body, $out_trade_no, bcmul($total_fee, '100'), $notify_url, $trade_type);
  56. *
  57. * halt('预支付结果:', $rs);
  58. * }
  59. *
  60. *
  61. * //支付通知回调
  62. * public static function Notify()
  63. * {
  64. *
  65. * //实例化支付接口
  66. * $pay = &load_wechat('Pay');
  67. *
  68. * //获取支付通知
  69. * $notifyInfo = $pay->getNotify();
  70. *
  71. * if ($notifyInfo === false) throw new Exception('获取微信支付通知数据失败,' . $pay->errMsg);
  72. * elseif ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') return $notifyInfo;// 支付状态完全成功,可以更新订单的支付状态了
  73. * else throw new Exception('获取微信支付通知数据失败,' . json_encode($notifyInfo, JSON_UNESCAPED_UNICODE));
  74. *
  75. * }
  76. *
  77. *
  78. * //@todo 校验订单状态,确认支付结果
  79. * public static function checkPayResult(string $out_trade_no = '')
  80. * {
  81. * $pay = &load_wechat('Pay');
  82. *
  83. * $rs = $pay->queryOrder($out_trade_no);
  84. *
  85. * halt('订单详情:', $rs);
  86. *
  87. * }
  88. **/
  89. }