{"id":23658,"date":"2024-11-21T15:15:06","date_gmt":"2024-11-21T07:15:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23658\/"},"modified":"2024-11-21T15:15:06","modified_gmt":"2024-11-21T07:15:06","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8thinkphp6%e5%ae%9e%e7%8e%b0%e6%94%af%e4%bb%98%e5%ae%9d%e6%94%af%e4%bb%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23658\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8"},"content":{"rendered":"<p>\u968f\u7740\u79fb\u52a8\u652f\u4ed8\u7684\u666e\u53ca\uff0c\u6210\u4e3a\u4e86\u8d8a\u6765\u8d8a\u591a\u4eba\u7684\u9996\u9009\u652f\u4ed8\u65b9\u5f0f\u3002\u800c\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u3001\u5b89\u5168\u7a33\u5b9a\u7684\u5f00\u6e90\u6846\u67b6\uff0c6\u4e5f\u88ab\u4f17\u591a\u5f00\u53d1\u4eba\u5458\u6240\u9752\u7750\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728thinkphp6\u4e2d\u5feb\u901f\u7b80\u4fbf\u5730\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8\u5462\uff1f<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\u4e0a\u7533\u8bf7\u7b7e\u7ea6\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\uff0c\u83b7\u53d6\u76f8\u5e94\u7684\u5e94\u7528\u4fe1\u606f\u3002\u5177\u4f53\u6d41\u7a0b\u53ef\u53c2\u8003\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\u7684\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u5728ThinkPHP6\u4e2d\u5b89\u88c5\u652f\u4ed8\u5b9dSDK\u3002\u53ef\u5728ThinkPHP6\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require \"alipay\/easysdk:dev-master\" -vvv<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u5728app\u76ee\u5f55\u4e0b\u65b0\u5efaAliPay\u6587\u4ef6\u5939\uff0c\u7136\u540e\u5728\u8be5\u6587\u4ef6\u5939\u4e0b\u521b\u5efaService\u76ee\u5f55\uff0c\u518d\u5728\u8be5\u76ee\u5f55\u4e0b\u521b\u5efaPayService.php\uff0c\u7528\u4e8e\u5c01\u88c5\u652f\u4ed8\u5b9d\u652f\u4ed8\u63a5\u53e3\u8c03\u7528\uff1a<\/p>\n<pre>liPayservice;\n\nuse Exception;\nuse thinkacadeConfig;\nuse thinkacadeLog;\nuse AlipayEasySDKKernelFactory;\nuse AlipayEasySDKPaymentCommonModelsAlipayTradeAppPayModel;\n\nclass PayService\n{\n    \/**\n     * app\u652f\u4ed8\n     *\n     * @param integer $orderId \u8ba2\u5355ID\n     * @param string $subject \u8ba2\u5355\u6807\u9898\n     * @param string $body \u8ba2\u5355\u5185\u5bb9\n     * @param float $amount \u8ba2\u5355\u91d1\u989d\uff08\u5143\uff09\n     * @param bool $isDebug \u662f\u5426\u5f00\u542f\u8c03\u8bd5\n     * \n     * @return array|string\n     *\/\n    public function appPay($orderId, $subject, $body, $amount, $isDebug = false)\n    {\n        try {\n            \/\/ \u83b7\u53d6\u652f\u4ed8\u5b9d\u914d\u7f6e\u4fe1\u606f\n            $config = Config::get('alipay.');\n\n            \/\/ \u52a0\u8f7dSDK\u914d\u7f6e\n            Factory::setOptions($config);\n\n            \/\/ \u521b\u5efa\u652f\u4ed8\u8bf7\u6c42\u5bf9\u8c61\n            $request = Factory::payment()-&amp;gt;appPay(new AlipayTradeAppPayModel([\n                'outTradeNo' =&amp;gt; $orderId, \/\/ \u8ba2\u5355ID\n                'subject' =&amp;gt; $subject, \/\/ \u8ba2\u5355\u6807\u9898\n                'totalAmount' =&amp;gt; strval($amount), \/\/ \u8ba2\u5355\u91d1\u989d\uff08\u5143\uff09\n                'body' =&amp;gt; $body, \/\/ \u8ba2\u5355\u5185\u5bb9\n            ]));\n\n            \/\/ \u8fd4\u56de\u652f\u4ed8\u53c2\u6570\n            return $request;\n        } catch (Exception $e) {\n            \/\/ \u5f02\u5e38\u5904\u7406\uff0c\u81ea\u884c\u6839\u636e\u4e1a\u52a1\u903b\u8f91\u5b9e\u73b0\n            Log::error($e-&amp;gt;getMessage());\n            return '\u652f\u4ed8\u8bf7\u6c42\u5931\u8d25\uff0c\u8bf7\u91cd\u65b0\u5c1d\u8bd5';\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u652f\u4ed8\u65b9\u6cd5\u4ec5\u4f9b\u53c2\u8003\uff0c\u5177\u4f53\u7684\u53c2\u6570\u914d\u7f6e\u53ef\u6839\u636e\u81ea\u8eab\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u6700\u540e\uff0c\u5728controller\u4e2d\u8c03\u7528PayService\u5373\u53ef\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8\u529f\u80fd\uff1a<\/p>\n<pre>acadeRequest;\nuse appliPayservicePayService;\n\nclass Index\n{\n    public function index()\n    {\n        \/\/ \u751f\u6210\u8ba2\u5355\n        $orderId = 1;\n        $subject = '\u6d4b\u8bd5\u8ba2\u5355';\n        $body = '\u6d4b\u8bd5\u8ba2\u5355\u5185\u5bb9';\n        $amount = 0.01;\n\n        $payService = new PayService();\n        $payParams = $payService-&amp;gt;appPay($orderId, $subject, $body, $amount, true);\n\n        \/\/ \u8fd4\u56de\u652f\u4ed8\u53c2\u6570\u7ed9\u5ba2\u6237\u7aef\n        return json($payParams);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4fbf\u662f\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u5f53\u7136\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u8fd8\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u903b\u8f91\u5904\u7406\u548c\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u6ee1\u8db3\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u501f\u52a9\u652f\u4ed8\u5b9dSDK\u548cThinkPHP6\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8\u5df2\u7ecf\u53d8\u5f97\u5f02\u5e38\u7b80\u5355\uff0c\u800c\u652f\u4ed8\u5b9d\u652f\u4ed8\u7684\u5e7f\u6cdb\u5e94\u7528\u4e5f\u4e3a\u6211\u4eec\u5f00\u53d1\u66f4\u52a0\u65b9\u4fbf\u5feb\u6377\u7684\u79fb\u52a8\u652f\u4ed8\u5e94\u7528\u63d0\u4f9b\u4e86\u91cd\u8981\u7684\u652f\u6301\u548c\u4fdd\u969c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528ThinkPHP6\u5b9e\u73b0\u652f\u4ed8\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>\u968f\u7740\u79fb\u52a8\u652f\u4ed8\u7684\u666e\u53ca\uff0c\u6210\u4e3a\u4e86\u8d8a\u6765\u8d8a\u591a\u4eba\u7684\u9996\u9009\u652f\u4ed8\u65b9\u5f0f\u3002\u800c\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u3001\u5b89\u5168\u7a33\u5b9a\u7684\u5f00\u6e90\u6846\u67b6\uff0c6\u4e5f\u88ab\u4f17\u591a\u5f00\u53d1\u4eba\u5458\u6240\u9752\u7750\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728thinkphp6\u4e2d\u5feb\u901f\u7b80\u4fbf\u5730\u5b9e\u73b0\u652f\u4ed8\u5b9d\u652f\u4ed8\u5462\uff1f \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\u4e0a\u7533\u8bf7\u7b7e\u7ea6\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\uff0c\u83b7\u53d6\u76f8\u5e94\u7684\u5e94\u7528\u4fe1\u606f\u3002\u5177\u4f53\u6d41\u7a0b\u53ef\u53c2\u8003\u652f\u4ed8\u5b9d\u5f00\u653e\u5e73\u53f0\u7684\u5b98\u65b9\u6587\u6863\u3002 \u63a5\u7740\uff0c\u5728ThinkPHP6\u4e2d\u5b89\u88c5\u652f\u4ed8\u5b9dSDK\u3002\u53ef\u5728ThinkPHP6\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require &#8220;alipay\/easysdk:dev-master&#8221; -vvv \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u6210\u529f\u540e\uff0c\u5728app\u76ee\u5f55\u4e0b\u65b0\u5efaAliPay\u6587\u4ef6\u5939\uff0c\u7136\u540e\u5728\u8be5\u6587\u4ef6\u5939\u4e0b\u521b\u5efaService\u76ee\u5f55\uff0c\u518d\u5728\u8be5\u76ee\u5f55\u4e0b\u521b\u5efaPayService.php\uff0c\u7528\u4e8e\u5c01\u88c5\u652f\u4ed8\u5b9d\u652f\u4ed8\u63a5\u53e3\u8c03\u7528\uff1a liPayservice; use Exception; use thinkacadeConfig; use thinkacadeLog; use AlipayEasySDKKernelFactory; use AlipayEasySDKPaymentCommonModelsAlipayTradeAppPayModel; class PayService { \/** * app\u652f\u4ed8 * * @param integer $orderId \u8ba2\u5355ID * @param string $subject \u8ba2\u5355\u6807\u9898 * @param string $body \u8ba2\u5355\u5185\u5bb9 * @param float $amount \u8ba2\u5355\u91d1\u989d\uff08\u5143\uff09 * @param bool $isDebug \u662f\u5426\u5f00\u542f\u8c03\u8bd5 * * @return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23658","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23658","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=23658"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23658\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}