weixin_public.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * 微信支付服务器端下单
  4. * 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
  5. * 使用示例
  6. * 构造方法参数
  7. * 'appid' => //填写微信分配的公众账号ID
  8. * 'mch_id' => //填写微信支付分配的商户号
  9. * 'notify_url'=> //填写微信支付结果回调地址
  10. * 'key' => //填写微信商户支付密钥
  11. * );
  12. * 统一下单方法
  13. * $WechatAppPay = new wechatAppPay($options);
  14. * $params['body'] = '商品描述'; //商品描述
  15. * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复
  16. * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
  17. * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
  18. * $wechatAppPay->unifiedOrder( $params );
  19. */
  20. class weixin_public
  21. {
  22. private $appid;
  23. private $seceret;
  24. private $uri;
  25. public function __construct($appid, $seceret, $uri)
  26. {
  27. $this->appid = $appid;
  28. $this->seceret = $seceret;
  29. $this->uri = $uri;
  30. }
  31. //获取code
  32. public function redirectWithCode()
  33. {
  34. // $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  35. // ."&redirect_uri=".urlencode($this->uri)
  36. // ."&response_type=code&scope=snsapi_userinfo&state=".time()."#wechat_redirect";
  37. $url = 'test';
  38. //header('Location: '.$url);
  39. return $url;
  40. }
  41. private function getAcessTokenAndOpenId($code)
  42. {
  43. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  44. ."&secret=".$this->seceret."&code=".$code."&grant_type=authorization_code";
  45. return file_get_contents($url);
  46. //return json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  47. }
  48. public function getAllInfo($code)
  49. {
  50. $info = self::getAcessTokenAndOpenId($code);
  51. return $info;
  52. if (empty($info['access_token']) || empty($info['openid'])) {
  53. return $info;
  54. }
  55. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$info['access_token']."&openid=".$info['openid']."&lang=zh_CN";
  56. $jsonInfo = file_get_contents($url);
  57. //$jsonInfo = iconv('ISO-8859-1', 'UTF-8', $jsonInfo);
  58. $ret = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  59. return $ret;
  60. }
  61. }