{"id":34025,"date":"2024-11-25T09:51:03","date_gmt":"2024-11-25T01:51:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34025\/"},"modified":"2024-11-25T09:51:03","modified_gmt":"2024-11-25T01:51:03","slug":"%e3%80%90%e8%ae%b0%e5%bd%95%e3%80%91php%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f-%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98v3%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34025\/","title":{"rendered":"\u3010\u8bb0\u5f55\u3011PHP\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u5fae\u4fe1\u652f\u4ed8v3\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u5199\u5c0f\u7a0b\u5e8f\u53d1\u73b0\u5fae\u4fe1\u652f\u4ed8\u6709\u4e86v3\u7248\u672c, \u5c31\u60f3\u7740\u63a5\u4e2a\u65b0\u7684\u652f\u4ed8\u770b\u770b, \u7ed3\u679c\u53d1\u73b0,\u6709\u6bd2,\u8bb0\u5f55\u4e00\u4e0b\u5386\u7a0b<\/p>\n<p>\u5148\u662f\u7edf\u4e00\u4e0b\u5355<\/p>\n<pre>\/\/\u7edf\u4e00\u4e0b\u5355\npublic function wechartAddOrder($name,$ordernumber,$money,$openid){\n        $url = \"https:\/\/api.mch.weixin.qq.com\/v3\/pay\/transactions\/jsapi\";\n        $urlarr = parse_url($url);\n        $appid = config('config.appId');\/\/appID\n        $mchid = config('config.mchid');\/\/\u5546\u6237ID\n        $xlid = config('config.apiXL');\/\/API\u5e8f\u5217\u53f7\n        $data = array();\n        $randstr = getRanStr(16,false);\/\/\u968f\u673a\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u8d85\u8fc732\n        $time = time();\n        $data['appid'] = $appid;\n        $data['mchid'] = $mchid;\n        $data['description'] = $name;\/\/\u5546\u54c1\u63cf\u8ff0\n        $data['out_trade_no'] = $ordernumber;\/\/\u8ba2\u5355\u7f16\u53f7\n        $data['notify_url'] = \"https:\/\/www.xffly.cn\/api\/admin\/order\/wechartCallback\";\/\/\u56de\u8c03\u63a5\u53e3\n\/\/      $data['amount']['total'] = $money;\/\/\u91d1\u989d\n        $data['amount']['total'] = 1;\n        $data['payer']['openid'] = $openid;\/\/\u7528\u6237openID\n        $data = json_encode($data); \n        $key = $this-&gt;getSign($data,$urlarr['path'],$randstr,$time);\/\/\u7b7e\u540d\n        $token = sprintf('mchid=\"%s\",serial_no=\"%s\",nonce_str=\"%s\",timestamp=\"%d\",signature=\"%s\"',$mchid,$xlid,$randstr,$time,$key);\/\/\u5934\u90e8\u4fe1\u606f\n        $header  = array(\n            'Content-Type:'.'application\/json; charset=UTF-8',\n            'Accept:application\/json',\n            'User-Agent:*\/*',\n            'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token\n        );  \n        $ret = curl_post_https($url,$data,$header);\n        return $ret;\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8ba1\u7b97\u7b7e\u540d, &nbsp;\u4e5f\u662f\u6309\u7167\u6587\u6863\u5f04\u4e86\u597d\u591a\u904d<\/p>\n<pre>\/\/\u5fae\u4fe1\u652f\u4ed8\u7b7e\u540d\npublic function getSign($data=array(),$url,$randstr,$time){\n        $str = \"POST\".\"\n\".$url.\"\n\".$time.\"\n\".$randstr.\"\n\".$data.\"\n\";\n        $key = file_get_contents('apiclient_key.pem');\/\/\u5728\u5546\u6237\u5e73\u53f0\u4e0b\u8f7d\u7684\u79d8\u94a5\n        $str = getSha256WithRSA($str,$key);\n        return $str;\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\u5f97\u5230prepay_id<br \/>\u5728\u5c0f\u7a0b\u5e8f\u91cc\u9762\u7528\u4ed6\u7684wx.requestPayment\u63a5\u53e3\u8c03\u8d77\u652f\u4ed8,\u6ca1\u5565\u542b\u91cf,\u4e0d\u8d34\u4ee3\u7801\u4e86,\u7167\u7740\u6587\u6863\u5199<br \/>\u5176\u4e2d\u8c03\u8d77\u652f\u4ed8\u8fd8\u9700\u8981\u4e00\u4e2a\u7b7e\u540d,\u540c\u7406<\/p>\n<pre>\/\/\u8c03\u8d77\u652f\u4ed8\u7684\u7b7e\u540d\npublic function getWechartSign($post){\n        $data = array();\n        $data['timeStamp'] = $post['timeStamp'];\n        $data['nonceStr'] = $post['str'];\n        $data['package'] = $post['package'];\n        $str = config('config.appId').\"\n\".$data['timeStamp'].\"\n\".$data['nonceStr'].\"\n\".$data['package'].\"\n\";\n        $key = file_get_contents('apiclient_key.pem');\n        $str = getSha256WithRSA($str,$key);\n        return $str;\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u91cd\u70b9\u662f\u652f\u4ed8\u6210\u529f\u7684\u56de\u8c03,\u7b80\u76f4\u6709\u6bd2<br \/>\u8fd4\u56de\u56de\u6765\u7684json\u4fe1\u606f, &nbsp; json_decode\u89e3\u6790\u5c31\u6210\u4e86\u7a7a, &nbsp;\u590d\u5236\u51fa\u6765\u518d\u89e3\u6790\u662f\u53ef\u4ee5\u89e3\u6790\u7684,\u8bf4\u662f\u4ed6\u6709bom\u4fe1\u606f\u5427, \u5f04\u4e86\u4e5f\u4e0d\u597d\u4f7f, &nbsp; \u7528htmlspecialchars_decode\u8f6c\u4e49\u4e00\u4e0b, &nbsp;\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u6210\u529f, \u53ef\u662f\u771f\u5b9e\u5fae\u4fe1\u652f\u4ed8,\u8fd8\u662f\u4e0d\u884c,\u6700\u540e\u6ca1\u529e\u6cd5, \u5b58\u7684log\u91cc\u9762, \u81ea\u5df1\u518d\u53d6\u4e00\u4e0b,\u5c31\u53ef\u4ee5\u7528\u4e86<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/\u5fae\u4fe1\u56de\u8c03\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u5e76\u8fd4\u56de\n    public function writeWechartLog($post){\n        if(!is_dir(\"upload\/log\")){\n           mkdir(\"upload\/log\",0777,true);\n        }\n        $log = fopen(\"upload\/log\/wechart.txt\", \"a+\");\n        if(is_array($post)){\n            $post = json_encode($post);\n        }\n        fwrite($log, $post.\"\n\");\n        fclose($log);\n        $read = fopen(\"upload\/log\/wechart.txt\", \"r\");\n        fseek($read, -1, SEEK_END);\n        $s = '';\n        while (($c = fgetc($read)) !== false) {\n            if ($c == \"\n\" &amp;&amp; $s) break;\n            $s = $c . $s;\n            fseek($read, -2, SEEK_CUR);\n        }\n        fclose($read);\n        return $s;\/\/\u53d6\u521a\u521a\u5b58\u7684\u6700\u540e\u4e00\u6761\u56de\u8c03\u4fe1\u606f\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56de\u8c03\u4fe1\u606f\u91cc\u9762,\u6709\u4e2a\u52a0\u5bc6\u7684\u4e1c\u897f,\u8fd8\u5f97\u89e3\u5bc6\u4e00\u4e0b,\u8fd9\u4e2a\u4f20\u5165\u7684\u662f\u81ea\u5df1\u53c8\u4ecelog\u91cc\u9762\u53d6\u51fa\u6765\u7684\u6570\u636e<br \/>sodium_crypto_aead_aes256gcm_decrypt &nbsp;\u8fd9\u4e2a\u89e3\u5bc6\u5bc6\u65b9\u6cd5\u9700\u8981php\u6269\u5c55 sodium<\/p>\n<pre>\/\/\u5fae\u4fe1\u56de\u8c03\u89e3\u5bc6\n    public function wechartDecrypt($str) {\n        $str = htmlspecialchars_decode($str,ENT_COMPAT);\n        $post = json_decode($str,true);\n        $key = config(\"config.apiv3Key\");\/\/\u5546\u6237\u5e73\u53f0\u8bbe\u7f6e\u7684api v3 \u5bc6\u7801\n        $text = base64_decode($post['resource']['ciphertext']);\n        $str = sodium_crypto_aead_aes256gcm_decrypt($text,$post['resource']['associated_data'],$post['resource']['nonce'],$key);\n        return json_decode($str,true);\n    }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5c31\u662f\u53d6\u5230\u56de\u8c03\u4fe1\u606f\u540e\u7684\u4e1a\u52a1\u5904\u7406\u4e86<\/p>\n<p>\u53c8\u770b\u5230\u8fd9\u4e2a\u6587\u7ae0\u7684\u670b\u53cb, &nbsp;\u5982\u679c\u77e5\u9053\u4e3a\u4ec0\u4e48 \u5fae\u4fe1\u56de\u8c03\u56de\u6765\u7684 json\u4fe1\u606f, &nbsp;\u6ca1\u529e\u6cd5\u76f4\u63a5\u4f7f\u7528\u7684, &nbsp; \u9ebb\u70e6\u544a\u544a\u6211,\u8c22\u8c22\u5566, &nbsp; \u7814\u7a76\u4e00\u5929\u6ca1\u7814\u7a76\u51fa\u6765, &nbsp;\u53ea\u80fd\u5148\u8fd9\u6837\u5904\u7406\u4e86&#8230;<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u3010\u8bb0\u5f55\u3011PHP\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u5fae\u4fe1\u652f\u4ed8v3\u7684\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u5c0f\u7a0b\u5e8f\u53d1\u73b0\u5fae\u4fe1\u652f\u4ed8\u6709\u4e86v3\u7248\u672c, \u5c31\u60f3\u7740\u63a5\u4e2a\u65b0\u7684\u652f\u4ed8\u770b\u770b, \u7ed3\u679c\u53d1\u73b0,\u6709\u6bd2,\u8bb0\u5f55\u4e00\u4e0b\u5386\u7a0b \u5148\u662f\u7edf\u4e00\u4e0b\u5355 \/\/\u7edf\u4e00\u4e0b\u5355 public function wechartAddOrder($name,$ordernumber,$money,$openid){ $url = &#8220;https:\/\/api.mch.weixin.qq.com\/v3\/pay\/transactions\/jsapi&#8221;; $urlarr = parse_url($url); $appid = config(&#8216;config.appId&#8217;);\/\/appID $mchid = config(&#8216;config.mchid&#8217;);\/\/\u5546\u6237ID $xlid = config(&#8216;config.apiXL&#8217;);\/\/API\u5e8f\u5217\u53f7 $data = array(); $randstr = getRanStr(16,false);\/\/\u968f\u673a\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u8d85\u8fc732 $time = time(); $data[&#8216;appid&#8217;] = $appid; $data[&#8216;mchid&#8217;] = $mchid; $data[&#8216;description&#8217;] = $name;\/\/\u5546\u54c1\u63cf\u8ff0 $data[&#8216;out_trade_no&#8217;] = $ordernumber;\/\/\u8ba2\u5355\u7f16\u53f7 $data[&#8216;notify_url&#8217;] = &#8220;https:\/\/www.xffly.cn\/api\/admin\/order\/wechartCallback&#8221;;\/\/\u56de\u8c03\u63a5\u53e3 \/\/ $data[&#8216;amount&#8217;][&#8216;total&#8217;] = $money;\/\/\u91d1\u989d $data[&#8216;amount&#8217;][&#8216;total&#8217;] = 1; $data[&#8216;payer&#8217;][&#8216;openid&#8217;] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-34025","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=34025"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34025\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}