教你用PHP开发微信公众号文章付费阅读功能

 4454

教你用PHP开发微信公众号文章付费阅读功能


php开发微信公众号文章付费阅读功能!


教你用PHP开发微信公众号文章付费阅读功能


如上图,一看就懂,就是一片公众号文章,点进去显示标题,作者,时间,公众号名称和部分内容,要想阅读所有内容,那就支付0.01元就可以阅读所有的内容,这就是付费阅读!!!当然金额可以自定义....

其实这个开发原理很简单,无非就是在文章页面加一个微信支付的按钮,点击支付后把隐藏的部分给显示出来或者直接跳转到全文地址。

当然了,这个文章界面也是模仿官方来写的,你可以查看官方的样式,然后模仿就可以了,这个没啥难度。主要是加个按钮在这个页面,下面是这个页面的代码:


readpay.php

  1. <?php
  2. header('Content-type:text/html; Charset=utf-8');
  3. $mchid = '微信支付商户号';          //微信支付商户号
  4. $appid = '微信支付申请对应的公众号的APPID';  //微信支付申请对应的公众号的APPID
  5. $appKey = '微信支付申请对应的公众号的APPSECRET';   //微信支付申请对应的公众号的APPSECRET
  6. $apiKey = 'API密钥';   //帐户设置-安全设置-API安全-API密钥-设置API密钥
  7. //①、获取用户openid
  8. $wxPay = new WxpayService($mchid,$appid,$appKey,$apiKey);
  9. $openId = $wxPay->GetOpenid();      //获取openid
  10. if(!$openId) exit('获取openid失败');
  11. //②、统一下单
  12. $outTradeNo = uniqid();     //你自己的商品订单号
  13. $payAmount = 0.01;          //付款金额,单位:元
  14. $orderName = 'test';    //订单标题
  15. $notifyUrl = 'nofity.php';     //付款成功后的回调地址,一般放在本页面的同级目录即可
  16. $payTime = time();      //付款时间
  17. $jsApiParameters = $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);
  18. $jsApiParameters = json_encode($jsApiParameters);
  19. ?> 
  20. <!--下面就是木方官方微信文章页面-->
  21. <html>
  22. <head>
  23.     <meta charset="utf-8">
  24.     <meta name="viewport" content="width=device-width, initial-scale=1"/>
  25.     <title>新版微信支持双开,王利芬庆祝“茅侃侃去世”文阅读10w+被批人血馒头</title>
  26.     <script type="text/javascript">
  27.         //调用微信JS api 支付
  28.         function jsApiCall()
  29.         {
  30.             WeixinJSBridge.invoke(
  31.                 'getBrandWCPayRequest',
  32.                 <?php echo $jsApiParameters; ?>,
  33.                 function(res){
  34.                     WeixinJSBridge.log(res.err_msg);
  35.                     //alert(res.err_code+res.err_desc+res.err_msg);
  36.                     if(res.err_msg == "get_brand_wcpay_request:ok"){
  37.                         //支付成功跳转页面
  38.                         window.location.href="这里填写支付成功后要跳转的查看全文文章地址";
  39.                     }else{
  40.                         //支付失败/或取消支付跳转页面,自己自定义开发页面
  41.                         window.location.href="false.html";
  42.                     }
  43.                 }
  44.             );
  45.         }
  46.         function callpay()
  47.         {
  48.             if (typeof WeixinJSBridge == "undefined"){
  49.                 if( document.addEventListener ){
  50.                     document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  51.                 }else if (document.attachEvent){
  52.                     document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  53.                     document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  54.                 }
  55.             }else{
  56.                 jsApiCall();
  57.             }
  58.         }
  59.     </script>
  60.     <!--下面就是木方官方微信文章页面的CSS-->
  61.     <style>
  62.         #title{
  63.             margin-bottom:10px;
  64.             line-height:1.4;
  65.             font-weight:400;
  66.             font-size:24px;
  67.             width: 97%;
  68.             margin:0 auto;
  69.         }             
  70.         #meta_content .time{
  71.             color: #8C8C8C;
  72.             font-family: "SimHei";
  73.         }
  74.         .author{
  75.             color: #8C8C8C;
  76.             font-family: "SimHei";
  77.         }
  78.         .name{
  79.             color: #607fa6;
  80.         }
  81.         #con{
  82.             width:95%;
  83.             font-size: 15px;
  84.             margin:15px auto 25px;
  85.             color: #595757;
  86.         }
  87.     </style>
  88. </head>
  89. <body>
  90. <br/>
  91. <!--文章标题-->
  92. <h2 id="title">新版微信支持双开,王利芬庆祝“茅侃侃去世”文阅读10w+被批人血馒头</h2>
  93. <div id="meta_content">
  94.     <!--发布时间、作者、公众号昵称-->
  95.     <span class="time">2018-01-27</span>   <span class="author">tanking</span>   <span class="name">里客云</span>
  96. </div>
  97. <!--显示的部分内容,未支付前显示的内容-->
  98. <div id="con">
  99.     这一周,公众号议论最多的有“周冲的影像声色”因洗稿被撕、80后创业者茅侃侃去世、刘亦菲分手......几乎每件事都能在朋友圈刷到相关报道。此外,视频大号“一条”又拿到了新一轮融资,你最关心哪一件事呢?
  100. </div>
  101. <!--支付按钮-->
  102. <div align="center">
  103.     <button style="width:180px; height:35px;background: none; border-radius: 5px;border:1px #1AAD19 solid; cursor: pointer;  color:#1AAD19;  font-size:15px;-webkit-tap-highlight-color:rgba(255,0,0,0);" type="button" οnclick="callpay()" >支付0.01阅读全文</button>
  104. </div>
  105. </body>
  106. </html>
  107. <!--下面是微信支付逻辑了-->
  108. <?php
  109. header("Content-Type:text/html; charset=utf-8");
  110. class WxpayService
  111. {
  112.     protected $mchid;
  113.     protected $appid;
  114.     protected $appKey;
  115.     protected $apiKey;
  116.     public $data = null;
  117.     public function __construct($mchid, $appid, $appKey,$key)
  118.     {
  119.         $this->mchid = $mchid; //https://pay.weixin.qq.com 产品中心-开发配置-商户号
  120.         $this->appid = $appid; //微信支付申请对应的公众号的APPID
  121.         $this->appKey = $appKey; //微信支付申请对应的公众号的APP Key
  122.         $this->apiKey = $key;   //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
  123.     }
  124.     /**
  125.      * 通过跳转获取用户的openid,跳转流程如下:
  126.      * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize
  127.      * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code
  128.      * @return 用户的openid
  129.      */
  130.     public function GetOpenid()
  131.     {
  132.         //通过code获得openid
  133.         if (!isset($_GET['code'])){
  134.             //触发微信返回code码
  135.             $scheme = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://';
  136.             $baseUrl = urlencode($scheme.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']);
  137.             $url = $this->__CreateOauthUrlForCode($baseUrl);
  138.             Header("Location: $url");
  139.             exit();
  140.         } else {
  141.             //获取code码,以获取openid
  142.             $code = $_GET['code'];
  143.             $openid = $this->getOpenidFromMp($code);
  144.             return $openid;
  145.         }
  146.     }
  147.     /**
  148.      * 通过code从工作平台获取openid机器access_token
  149.      * @param string $code 微信跳转回来带上的code
  150.      * @return openid
  151.      */
  152.     public function GetOpenidFromMp($code)
  153.     {
  154.         $url = $this->__CreateOauthUrlForOpenid($code);
  155.         $res = self::curlGet($url);
  156.         //取出openid
  157.         $data = json_decode($res,true);
  158.         $this->data = $data;
  159.         $openid = $data['openid'];
  160.         return $openid;
  161.     }
  162.     /**
  163.      * 构造获取open和access_toke的url地址
  164.      * @param string $code,微信跳转带回的code
  165.      * @return 请求的url
  166.      */
  167.     private function __CreateOauthUrlForOpenid($code)
  168.     {
  169.         $urlObj["appid"] = $this->appid;
  170.         $urlObj["secret"] = $this->appKey;
  171.         $urlObj["code"] = $code;
  172.         $urlObj["grant_type"] = "authorization_code";
  173.         $bizString = $this->ToUrlParams($urlObj);
  174.         return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  175.     }
  176.     /**
  177.      * 构造获取code的url连接
  178.      * @param string $redirectUrl 微信服务器回跳的url,需要url编码
  179.      * @return 返回构造好的url
  180.      */
  181.     private function __CreateOauthUrlForCode($redirectUrl)
  182.     {
  183.         $urlObj["appid"] = $this->appid;
  184.         $urlObj["redirect_uri"] = "$redirectUrl";
  185.         $urlObj["response_type"] = "code";
  186.         $urlObj["scope"] = "snsapi_base";
  187.         $urlObj["state"] = "STATE"."#wechat_redirect";
  188.         $bizString = $this->ToUrlParams($urlObj);
  189.         return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  190.     }
  191.     /**
  192.      * 拼接签名字符串
  193.      * @param array $urlObj
  194.      * @return 返回已经拼接好的字符串
  195.      */
  196.     private function ToUrlParams($urlObj)
  197.     {
  198.         $buff = "";
  199.         foreach ($urlObj as $k => $v)
  200.         {
  201.             if($k != "sign") $buff .= $k . "=" . $v . "&";
  202.         }
  203.         $buff = trim($buff, "&");
  204.         return $buff;
  205.     }
  206.     /**
  207.      * 统一下单
  208.      * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
  209.      * @param float $totalFee 收款总费用 单位元
  210.      * @param string $outTradeNo 唯一的订单号
  211.      * @param string $orderName 订单名称
  212.      * @param string $notifyUrl 支付结果通知url 不要有问号
  213.      * @param string $timestamp 支付时间
  214.      * @return string
  215.      */
  216.     public function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp)
  217.     {
  218.         $config = array(
  219.             'mch_id' => $this->mchid,
  220.             'appid' => $this->appid,
  221.             'key' => $this->apiKey,
  222.         );
  223.         $orderName = iconv('GBK','UTF-8',$orderName);
  224.         $unified = array(
  225.             'appid' => $config['appid'],
  226.             'attach' => 'pay',             //商家数据包,原样返回,如果填写中文,请注意转换为utf-8
  227.             'body' => $orderName,
  228.             'mch_id' => $config['mch_id'],
  229.             'nonce_str' => self::createNonceStr(),
  230.             'notify_url' => $notifyUrl,
  231.             'openid' => $openid,            //rade_type=JSAPI,此参数必传
  232.             'out_trade_no' => $outTradeNo,
  233.             'spbill_create_ip' => '127.0.0.1',
  234.             'total_fee' => intval($totalFee * 100),       //单位 转为分
  235.             'trade_type' => 'JSAPI',
  236.         );
  237.         $unified['sign'] = self::getSign($unified, $config['key']);
  238.         $responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));
  239.         $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  240.         if ($unifiedOrder === false) {
  241.             die('parse xml error');
  242.         }
  243.         if ($unifiedOrder->return_code != 'SUCCESS') {
  244.             die($unifiedOrder->return_msg);
  245.         }
  246.         if ($unifiedOrder->result_code != 'SUCCESS') {
  247.             die($unifiedOrder->err_code);
  248.         }
  249.         $arr = array(
  250.             "appId" => $config['appid'],
  251.             "timeStamp" => "$timestamp",        //这里是字符串的时间戳,不是int,所以需加引号
  252.             "nonceStr" => self::createNonceStr(),
  253.             "package" => "prepay_id=" . $unifiedOrder->prepay_id,
  254.             "signType" => 'MD5',
  255.         );
  256.         $arr['paySign'] = self::getSign($arr, $config['key']);
  257.         return $arr;
  258.     }
  259.     public static function curlGet($url = '', $options = array())
  260.     {
  261.         $ch = curl_init($url);
  262.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  263.         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  264.         if (!empty($options)) {
  265.             curl_setopt_array($ch, $options);
  266.         }
  267.         //https请求 不验证证书和host
  268.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  269.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  270.         $data = curl_exec($ch);
  271.         curl_close($ch);
  272.         return $data;
  273.     }
  274.     public static function curlPost($url = '', $postData = '', $options = array())
  275.     {
  276.         if (is_array($postData)) {
  277.             $postData = http_build_query($postData);
  278.         }
  279.         $ch = curl_init();
  280.         curl_setopt($ch, CURLOPT_URL, $url);
  281.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  282.         curl_setopt($ch, CURLOPT_POST, 1);
  283.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  284.         curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  285.         if (!empty($options)) {
  286.             curl_setopt_array($ch, $options);
  287.         }
  288.         //https请求 不验证证书和host
  289.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  290.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  291.         $data = curl_exec($ch);
  292.         curl_close($ch);
  293.         return $data;
  294.     }
  295.     public static function createNonceStr($length = 16)
  296.     {
  297.         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  298.         $str = '';
  299.         for ($i = 0; $i < $length; $i++) {
  300.             $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  301.         }
  302.         return $str;
  303.     }
  304.     public static function arrayToXml($arr)
  305.     {
  306.         $xml = "<xml>";
  307.         foreach ($arr as $key => $val) {
  308.             if (is_numeric($val)) {
  309.                 $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  310.             } else {
  311.                 $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  312.             }
  313.         }
  314.         $xml .= "</xml>";
  315.         return $xml;
  316.     }
  317.     public static function getSign($params, $key)
  318.     {
  319.         ksort($params, SORT_STRING);
  320.         $unSignParaString = self::formatQueryParaMap($params, false);
  321.         $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  322.         return $signStr;
  323.     }
  324.     protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  325.     {
  326.         $buff = "";
  327.         ksort($paraMap);
  328.         foreach ($paraMap as $k => $v) {
  329.             if (null != $v && "null" != $v) {
  330.                 if ($urlEncode) {
  331.                     $v = urlencode($v);
  332.                 }
  333.                 $buff .= $k . "=" . $v . "&";
  334.             }
  335.         }
  336.         $reqPar = '';
  337.         if (strlen($buff) > 0) {
  338.             $reqPar = substr($buff, 0, strlen($buff) - 1);
  339.         }
  340.         return $reqPar;
  341.     }
  342. }
  343. ?>


nofity.php

  1. <?php
  2. /**
  3.  * 原生支付(扫码支付)及公众号支付的异步回调通知
  4.  * 说明:需要在native.php或者jsapi.php中的填写回调地址。例如:http://www.xxx.com/wx/notify.php
  5.  * 付款成功后,微信服务器会将付款结果通知到该页面
  6.  */
  7. header('Content-type:text/html; Charset=utf-8');
  8. $mchid = '微信支付商户号';
  9. $appid = '公众号APPID'; 
  10. $apiKey = 'API密钥'; 
  11. $wxPay = new WxpayService($mchid,$appid,$apiKey);
  12. $result = $wxPay->notify();
  13. if($result){
  14.     //完成你的逻辑
  15.     //例如连接数据库,获取付款金额$result['cash_fee'],获取订单号$result['out_trade_no'],修改数据库中的订单状态等;
  16. }else{
  17.     echo 'pay error';
  18. }
  19. class WxpayService
  20. {
  21.     protected $mchid;
  22.     protected $appid;
  23.     protected $apiKey;
  24.     public function __construct($mchid, $appid, $key)
  25.     {
  26.         $this->mchid = $mchid;
  27.         $this->appid = $appid;
  28.         $this->apiKey = $key;
  29.     }
  30.     public function notify()
  31.     {
  32.         $config = array(
  33.             'mch_id' => $this->mchid,
  34.             'appid' => $this->appid,
  35.             'key' => $this->apiKey,
  36.         );
  37.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  38.         $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  39.         if ($postObj === false) {
  40.             die('parse xml error');
  41.         }
  42.         if ($postObj->return_code != 'SUCCESS') {
  43.             die($postObj->return_msg);
  44.         }
  45.         if ($postObj->result_code != 'SUCCESS') {
  46.             die($postObj->err_code);
  47.         }
  48.         $arr = (array)$postObj;
  49.         unset($arr['sign']);
  50.         if (self::getSign($arr, $config['key']) == $postObj->sign) {
  51.             echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  52.             return $arr;
  53.         }
  54.     }
  55.     /**
  56.      * 获取签名
  57.      */
  58.     public static function getSign($params, $key)
  59.     {
  60.         ksort($params, SORT_STRING);
  61.         $unSignParaString = self::formatQueryParaMap($params, false);
  62.         $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  63.         return $signStr;
  64.     }
  65.     protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  66.     {
  67.         $buff = "";
  68.         ksort($paraMap);
  69.         foreach ($paraMap as $k => $v) {
  70.             if (null != $v && "null" != $v) {
  71.                 if ($urlEncode) {
  72.                     $v = urlencode($v);
  73.                 }
  74.                 $buff .= $k . "=" . $v . "&";
  75.             }
  76.         }
  77.         $reqPar = '';
  78.         if (strlen($buff) > 0) {
  79.             $reqPar = substr($buff, 0, strlen($buff) - 1);
  80.         }
  81.         return $reqPar;
  82.     }
  83. }


简单说明:

1、新建readpay.php,拷贝上面的代码,修改商户号、appid、appsecret、密钥即可,还有下面的回调地址的路径(nofity.php),至于支付成功和支付失败跳转的页面按自己的需求修改即可。

支付金额、文章标题】时间、作者、公账号名称等根据自己的需求修改。

要注意,订单标题不能为中文,否则会提示body参数错误什么的。

2、新建nofity.php

把上面的代码拷贝上去,修改商户号、appid、密钥即可。

上传到支付授权目录,然后访问readpay.php页面测试。

3、新建false.html,这个是支付失败的页面,至于支付失败要显示什么或者输出什么,这个根据你的业务来自定义开发。




本文网址:https://www.zztuku.com/detail-7928.html
站长图库 - 教你用PHP开发微信公众号文章付费阅读功能
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐