weixin_public.class.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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=".$this->uri
  36. ."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  37. //header('Location: '.$url);
  38. return $url;
  39. }
  40. private function getAcessTokenAndOpenId($code)
  41. {
  42. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  43. ."&secret=".$this->seceret."&code=".$code."&grant_type=authorization_code";
  44. $jsonInfo = file_get_contents($url);
  45. return json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  46. }
  47. public function getAllInfo($code)
  48. {
  49. $info = self::getAcessTokenAndOpenId($code);
  50. return $info;
  51. // if (empty($info['access_token']) || empty($info['openid'])) {
  52. // return $info;
  53. // }
  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. }