{"id":100,"date":"2021-07-30T17:00:38","date_gmt":"2021-07-30T09:00:38","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=100"},"modified":"2024-07-08T12:54:20","modified_gmt":"2024-07-08T04:54:20","slug":"%e3%80%90php%e3%80%91php%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e7%94%b3%e8%af%b7%e9%80%80%e6%ac%be%e6%b5%81%e7%a8%8b%e5%ae%9e%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/07\/30\/%e3%80%90php%e3%80%91php%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e7%94%b3%e8%af%b7%e9%80%80%e6%ac%be%e6%b5%81%e7%a8%8b%e5%ae%9e%e4%be%8b%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u3010PHP\u3011PHP\u5b9e\u73b0\u5fae\u4fe1\u7533\u8bf7\u9000\u6b3e\u6d41\u7a0b\u5b9e\u4f8b\u4ee3\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u524d\u9762\u8bb2\u4e86\u600e\u4e48\u5b9e\u73b0\u5fae\u4fe1\u652f\u4ed8\u3002\u8fd8\u9700\u8981\u6709\u5fae\u4fe1\u9000\u6b3e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u524d\u671f\u51c6\u5907\uff1a<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\u662f\u641e\u5b9a\u4e86\u5fae\u4fe1\u652f\u4ed8\uff0c\u4e0d\u7136\u600e\u4e48\u9000\u6b3e\uff0c\u8fd9\u6b21\u8fd8\u662f\u4f7f\u7528\u5b98\u65b9\u7684demo\u3002\u5f53\u7136\u7f51\u4e0a\u53ef\u80fd\u4e5f\u6709\u5f88\u591a\u5927\u795e\u81ea\u5df1\u91cd\u5199\u548c\u5c01\u88c5\u4e86demo\uff0c\u6216\u8bb8\u66f4\u52a0\u597d\u7528\u7b80\u6d01\uff0c\u4f46\u662f\u6211\u8fd8\u662f\u4e0d\u63d0\u5021\u7528\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u53ef\u80fd\u529f\u80fd\u4e0d\u5168\uff0c\u6216\u8bb8\u4ed6\u53ea\u662f\u5b9e\u73b0\u4e86\u5fae\u4fe1\u652f\u4ed8\uff0c\u4f46\u662f\u8fd8\u6709\u7533\u8bf7\u9000\u6b3e\u3001\u67e5\u8be2\u9000\u6b3e\u3001\u8ba2\u5355\u67e5\u8be2\u3001\u64a4\u9500\u8ba2\u5355\u7b49\u4e1a\u52a1\u529f\u80fd\u53ef\u80fd\u662f\u4f60\u540e\u7eed\u9700\u8981\u7684\uff0c\u5982\u679c\u4f60\u4f9d\u8d56\u4e8e\u5927\u795e\u7684SDK\u7684\u4fbf\u6377\uff0c\u5982\u679c\u6709\u65b0\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4f60\u5c31\u61f5\u903c\u4e86\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u5b89\u5168\u8003\u8651\uff0c\u6d89\u53ca\u5230\u652f\u4ed8\u6d89\u53ca\u5230\u91d1\u94b1\uff0c\u5fc5\u987b\u8981\u975e\u5e38\u5b89\u5168\u3002\u5b98\u65b9SDK\u867d\u7136\u6211\u4e5f\u5410\u69fd\uff0c\u4f46\u81f3\u5c11\u4f1a\u76f8\u5bf9\u6bd4\u8f83\u5b89\u5168\uff0c\u518d\u6b21\u91cd\u5199\uff0c\u867d\u7136\u6682\u65f6\u6ca1\u770b\u51fa\u95ee\u9898\uff0c\u4f46\u662f\u4e07\u4e00\u6709\u6f0f\u6d1e\u5c31\u4e0d\u597d\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u7bc7\u8fd8\u662f\u4f7f\u7528\u5230\u5b98\u65b9\u63d0\u4f9b\u7684SDK\u4e2d\u7684\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u7c7b\u6587\u4ef6WxPay.Api.php\u4e2d\u63d0\u4f9b\u7684refund()\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u6b64\u65b9\u6cd5\u5728WxPay.Api.php\u6587\u4ef6\u7684\u7b2c141\u884c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/** \n * \n * \u7533\u8bf7\u9000\u6b3e\uff0cWxPayRefund\u4e2dout_trade_no\u3001transaction_id\u81f3\u5c11\u586b\u4e00\u4e2a\u4e14 \n * out_refund_no\u3001total_fee\u3001refund_fee\u3001op_user_id\u4e3a\u5fc5\u586b\u53c2\u6570 \n * appid\u3001mchid\u3001spbill_create_ip\u3001nonce_str\u4e0d\u9700\u8981\u586b\u5165 \n * @param WxPayRefund $inputObj \n * @param int $timeOut \n * @throws WxPayException \n * @return \u6210\u529f\u65f6\u8fd4\u56de\uff0c\u5176\u4ed6\u629b\u5f02\u5e38 \n *\/ \npublic static function refund($inputObj, $timeOut = 6){ \n $url = \"https:\/\/api.mch.weixin.qq.com\/secapi\/pay\/refund\"; \n \/\/\u68c0\u6d4b\u5fc5\u586b\u53c2\u6570 \n if(!$inputObj->IsOut_trade_noSet() &amp;&amp; !$inputObj->IsTransaction_idSet()) { \n  throw new WxPayException(\"\u9000\u6b3e\u7533\u8bf7\u63a5\u53e3\u4e2d\uff0cout_trade_no\u3001transaction_id\u81f3\u5c11\u586b\u4e00\u4e2a\uff01\"); \n }else if(!$inputObj->IsOut_refund_noSet()){ \n  throw new WxPayException(\"\u9000\u6b3e\u7533\u8bf7\u63a5\u53e3\u4e2d\uff0c\u7f3a\u5c11\u5fc5\u586b\u53c2\u6570out_refund_no\uff01\"); \n }else if(!$inputObj->IsTotal_feeSet()){ \n  throw new WxPayException(\"\u9000\u6b3e\u7533\u8bf7\u63a5\u53e3\u4e2d\uff0c\u7f3a\u5c11\u5fc5\u586b\u53c2\u6570total_fee\uff01\"); \n }else if(!$inputObj->IsRefund_feeSet()){ \n  throw new WxPayException(\"\u9000\u6b3e\u7533\u8bf7\u63a5\u53e3\u4e2d\uff0c\u7f3a\u5c11\u5fc5\u586b\u53c2\u6570refund_fee\uff01\"); \n }else if(!$inputObj->IsOp_user_idSet()){ \n  throw new WxPayException(\"\u9000\u6b3e\u7533\u8bf7\u63a5\u53e3\u4e2d\uff0c\u7f3a\u5c11\u5fc5\u586b\u53c2\u6570op_user_id\uff01\"); \n } \n $inputObj->SetAppid(WxPayConfig::APPID);\/\/\u516c\u4f17\u8d26\u53f7ID \n $inputObj->SetMch_id(WxPayConfig::MCHID);\/\/\u5546\u6237\u53f7 \n $inputObj->SetNonce_str(self::getNonceStr());\/\/\u968f\u673a\u5b57\u7b26\u4e32 \n $inputObj->SetSign();\/\/\u7b7e\u540d \n $xml = $inputObj->ToXml(); \n $startTimeStamp = self::getMillisecond();\/\/\u8bf7\u6c42\u5f00\u59cb\u65f6\u95f4 \n $response = self::postXmlCurl($xml, $url, true, $timeOut); \n $result = WxPayResults::Init($response); \n self::reportCostTime($url, $startTimeStamp, $result);\/\/\u4e0a\u62a5\u8bf7\u6c42\u82b1\u8d39\u65f6\u95f4 \n return $result; \n} <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u7684\u65b9\u6cd5\uff0c\u5199\u7684\u5f88\u6e05\u695a\u9700\u8981\u54ea\u4e9b\u53c2\u6570\uff0c\u8fd8\u6709\u4e00\u4e9b\u5fc5\u987b\u53c2\u6570SDK\u5df2\u7ecf\u5e2e\u6211\u4eec\u8865\u9f50\u4e86\uff0c\u6211\u5c06\u8fd9\u4e2a\u65b9\u6cd5\u91cd\u65b0\u5c01\u88c5\u4e00\u4e0b\uff0c\u4fbf\u4e8e\u5728\u9879\u76ee\u4e2d\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/** \n * \u5fae\u4fe1\u9000\u6b3e \n * @param string $order_id \u8ba2\u5355ID \n * @return \u6210\u529f\u65f6\u8fd4\u56de(array\u7c7b\u578b)\uff0c\u5176\u4ed6\u629b\u5f02\u5e38 \n *\/ \nfunction wxRefund($order_id){ \n \/\/\u6211\u7684SDK\u653e\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684Api\u76ee\u5f55\u4e0b \n require_once APP_ROOT.\"\/Api\/wxpay\/lib\/WxPay.Api.php\"; \n \/\/\u67e5\u8be2\u8ba2\u5355,\u6839\u636e\u8ba2\u5355\u91cc\u8fb9\u7684\u6570\u636e\u8fdb\u884c\u9000\u6b3e \n $order = M('order')->where(array('id'=>$order_id,'is_refund'=>2,'order_status'=>1))->find(); \n $merchid = WxPayConfig::MCHID; \n if(!$order) return false; \n $input = new WxPayRefund(); \n $input->SetOut_trade_no($order['order_sn']);   \/\/\u81ea\u5df1\u7684\u8ba2\u5355\u53f7 \n $input->SetTransaction_id($order['transaction_id']);  \/\/\u5fae\u4fe1\u5b98\u65b9\u751f\u6210\u7684\u8ba2\u5355\u6d41\u6c34\u53f7\uff0c\u5728\u652f\u4ed8\u6210\u529f\u4e2d\u6709\u8fd4\u56de \n $input->SetOut_refund_no(getrand_num(true));   \/\/\u9000\u6b3e\u5355\u53f7 \n $input->SetTotal_fee($order['total_price']);   \/\/\u8ba2\u5355\u6807\u4ef7\u91d1\u989d\uff0c\u5355\u4f4d\u4e3a\u5206 \n $input->SetRefund_fee($order['total_price']);   \/\/\u9000\u6b3e\u603b\u91d1\u989d\uff0c\u8ba2\u5355\u603b\u91d1\u989d\uff0c\u5355\u4f4d\u4e3a\u5206\uff0c\u53ea\u80fd\u4e3a\u6574\u6570 \n $input->SetOp_user_id($merchid); \n $result = WxPayApi::refund($input); \/\/\u9000\u6b3e\u64cd\u4f5c \n \/\/ \u8fd9\u53e5file_put_contents\u662f\u7528\u6765\u67e5\u770b\u670d\u52a1\u5668\u8fd4\u56de\u7684\u9000\u6b3e\u7ed3\u679c \u6d4b\u8bd5\u5b8c\u53ef\u4ee5\u5220\u9664\u4e86 \n \/\/file_put_contents(APP_ROOT.'\/Api\/wxpay\/logs\/log3.txt',arrayToXml($result),FILE_APPEND); \n return $result; \n} <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u9700\u8981\u5410\u69fd\u4e00\u4e0b\uff0c\u7adf\u7136\u4e0d\u8bf4\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u5728\u652f\u4ed8\u7684\u65f6\u5019\u8fd4\u56de\u7684\u662fXML\u6570\u636e\uff0c\u8fd9\u91cc\u7adf\u7136\u8fd4\u56de\u7684\u662f\u6570\u7ec4\uff0c\u8ba9\u6211\u63aa\u624b\u4e0d\u53ca\uff0c\u54c8\u54c8\u4e0d\u8fc7\u8fd8\u662f\u8fd4\u56de\u6570\u7ec4\u6bd4\u8f83\u597d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u8c03\u7528\u5c31\u66f4\u52a0\u7b80\u5355\u4e86:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5fae\u4fe1\u9000\u6b3e \n$result = wxRefund($order_id); \n\/\/ \u8fd9\u53e5file_put_contents\u662f\u7528\u6765\u67e5\u770b\u670d\u52a1\u5668\u8fd4\u56de\u7684\u9000\u6b3e\u7ed3\u679c \u6d4b\u8bd5\u5b8c\u53ef\u4ee5\u5220\u9664\u4e86 \n\/\/file_put_contents(APP_ROOT.'\/Api\/wxpay\/logs\/log4.txt',arrayToXml($result),FILE_APPEND); \nif(($result['return_code']=='SUCCESS') &amp;&amp; ($result['result_code']=='SUCCESS')){ \n \/\/\u9000\u6b3e\u6210\u529f \n}else if(($result['return_code']=='FAIL') || ($result['result_code']=='FAIL')){ \n \/\/\u9000\u6b3e\u5931\u8d25 \n \/\/\u539f\u56e0 \n $reason = (empty($result['err_code_des'])?$result['return_msg']:$result['err_code_des']); \n}else{ \n \/\/\u5931\u8d25 \n} <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9000\u6b3e\u6210\u529f\u8fd4\u56de\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/replace\/210b13b9f2196aee98cb548b5357c788.png\" alt=\"\u3010PHP\u3011PHP\u5b9e\u73b0\u5fae\u4fe1\u7533\u8bf7\u9000\u6b3e\u6d41\u7a0b\u5b9e\u4f8b\u4ee3\u7801\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eb2\u6d4b\u65e0\u8bef:\u8fd9\u662f\u96c6\u6210\u4e86\u5b98\u65b9\u7684SDK\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/7de6e11da7a6699d703976251765ab8c.png\" alt=\"\u3010PHP\u3011PHP\u5b9e\u73b0\u5fae\u4fe1\u7533\u8bf7\u9000\u6b3e\u6d41\u7a0b\u5b9e\u4f8b\u4ee3\u7801\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u8bb2\u4e86\u600e\u4e48\u5b9e\u73b0\u5fae\u4fe1\u652f\u4ed8\u3002\u8fd8\u9700\u8981\u6709\u5fae\u4fe1\u9000\u6b3e\u3002 \u524d\u671f\u51c6\u5907\uff1a \u5f53&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1217,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":1218,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions\/1218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media\/1217"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}