{"id":24167,"date":"2024-04-25T13:09:31","date_gmt":"2024-04-25T05:09:31","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=24167"},"modified":"2024-04-25T13:09:31","modified_gmt":"2024-04-25T05:09:31","slug":"%e8%8b%b9%e6%9e%9c%e5%85%8d%e7%ad%bephp%e6%ba%90%e7%a0%81%e4%bd%9c%e7%94%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2024\/04\/25\/%e8%8b%b9%e6%9e%9c%e5%85%8d%e7%ad%bephp%e6%ba%90%e7%a0%81%e4%bd%9c%e7%94%a8%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"\u82f9\u679c\u514d\u7b7ephp\u6e90\u7801\u4f5c\u7528\u4ecb\u7ecd"},"content":{"rendered":"

\u82f9\u679c\u514d\u7b7e\u652f\u4ed8\u662f\u4e00\u4e2a\u7ed5\u8fc7\u82f9\u679c\u5b98\u65b9\u652f\u4ed8\u7cfb\u7edf\uff0c\u4f7f\u7528\u4e2a\u4eba\u5f00\u53d1\u8005\u8d26\u53f7\u5b9e\u73b0\u7684\u4e00\u79cd\u652f\u4ed8\u6a21\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u6a21\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8d2d\u4e70\u865a\u62df\u5546\u54c1\u6216\u670d\u52a1,\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u82f9\u679c\u7684\u652f\u4ed8\u7cfb\u7edf\uff0c\u4ece\u800c\u907f\u5f00\u82f9\u679c\u7684\u5ba1\u67e5\u548c\u62bd\u6210\u3002\u7136\u800c,\u8fd9\u79cd\u65b9\u5f0f\u8fdd\u53cd\u4e86\u82f9\u679c\u7684\u653f\u7b56\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5b58\u5728\u4e00\u5b9a\u7684\u98ce\u9669\u3002<\/p>\n

\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u82f9\u679c\u514d\u7b7e\u652f\u4ed8\u7684PHP\u5b9e\u73b0\u539f\u7406\u3002\u8bf7\u6ce8\u610f,\u4f7f\u7528\u8fd9\u4e9b\u4ee3\u7801\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u5e94\u7528\u88ab\u4e0b\u67b6\uff0c\u6211\u4eec\u4e0d\u63a8\u8350\u8fd9\u6837\u505a\u3002\u8fd9\u4e2a\u6559\u7a0b\u4ec5\u4f5c\u4e3a\u6280\u672f\u4ea4\u6d41\uff0c\u5e2e\u52a9\u60a8\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n

1. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684PHP\u6587\u4ef6\uff0c\u547d\u540d\u4e3a “apple_pay.php”\u3002<\/p>\n

2. \u5728PHP\u6587\u4ef6\u4e2d,\u6211\u4eec\u9700\u8981\u9996\u5148\u8bbe\u7f6e\u4e00\u4e9b\u5e38\u91cf\u548c\u914d\u7f6e\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n

“`php<\/p>\n<\/p>\n

\/\/ \u4f60\u7684RSA\u79c1\u94a5\u548c\u516c\u94a5<\/p>\n

define(“PRIVATE_KEY”, “Your private key”);<\/p>\n

define(“PUBLIC_KEY”, “Your public key”);<\/p>\n

\/\/ \u652f\u4ed8\u5b9d\u540c\u6b65\u901a\u77e5\u56de\u8c03\u94fe\u63a5<\/p>\n

define(“ALIPAY_NOT\u82f9\u679cios\u6c38\u4e45\u514d\u7b7e\u540d<\/a>IFY_URL”, “https:\/\/example.com\/alipay_notify.php”);<\/p>\n

\/\/ \u5546\u54c1\u4fe1\u606f\u914d\u7f6e\uff0c\u5546\u54c1\u540d\u79f0\u4e0e\u4ef7\u683c<\/p>\n

$products = array(<\/p>\n

‘product_01’ => array(‘name’ => ‘\u865a\u62df\u5546\u54c11’, ‘price’ => 6.00),<\/p>\n

‘product_02’ => array(‘name’ => ‘\u865a\u62df\u5546\u54c12’, ‘price’ => 15.00),<\/p>\n

\/\/ \u66f4\u591a\u5546\u54c1…<\/p>\n

);<\/p>\n

“`<\/p>\n

3. \u63a5\u4e0b\u6765,\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u52a0\u5bc6\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u8bf7\u6c42\u53c2\u6570\u8fdb\u884c\u7b7e\u540d\u3002<\/p>\n

“`php<\/p>\n

function sign($dataStr) {<\/p>\n

$privateKeyId = openssl_get_privatekey(PRIVATE_KEY);<\/p>\n

$result = openssl_sign($dataStr, $sign, $privateKeyId, OPENSSL_ALGO_SHA1);<\/p>\n

openssl_free_key($privateKeyId);<\/p>\n<\/p>\n

$sign = base64_encode($sign);<\/p>\n

return $sign;<\/p>\n

}<\/p>\n

“`<\/p>\n

4. \u73b0\u5728\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u652f\u4ed8\u8bf7\u6c42\u7684\u6838\u5fc3\u51fd\u6570\u3002<\/p>\n

“`php<\/p>\n

function processPayment($orderNumber, $productID, $userID) {<\/p>\n

global $products;<\/p>\n

\/\/ \u83b7\u53d6\u5546\u54c1\u4fe1\u606f<\/p>\n

$product = $products[$productID];<\/p>\n<\/p>\n

\/\/ \u751f\u6210\u5f85\u7b7e\u540d\u5b57\u7b26\u4e32<\/p>\n

$dataStr = “order_nu\u514d\u7b7eios\u5c01\u88c5app<\/a>mber={$orderNumber}&product_id={$produ<\/p>\n

<\/figure>\n<\/p>\n

ctID}&user_id={$userID}”;<\/p>\n

\/\/ \u5bf9\u5f85\u7b7e\u540d\u5b57\u7b26\u4e32\u8fdb\u884c\u7b7e\u540d<\/p>\n

$signedData = sign($dataStr);<\/p>\n

\/\/ \u8bf7\u6c42\u652f\u4ed8\u5b9d\u652f\u4ed8\u63a5\u53e3<\/p>\n

$alipayUrl = “https:\/\/openapi.alipay.com\/gateway.do?”.$dataStr.”&sign=”.$signedData.”&notify_url=”.urlencode(ALIPAY_NOTIFY_URL);<\/p>\n

\/\/ \u91cd\u5b9a\u5411\u5230\u652f\u4ed8\u5b9d\u652f\u4ed8\u9875\u9762<\/p>\n

header(“Location: “.$alipayUrl);<\/p>\n

}<\/p>\n

“`<\/p>\n

5. \u6700\u540e,\u6211\u4eec\u9700\u8981\u5904\u7406\u7528\u6237\u63d0\u4ea4\u7684\u652f\u4ed8\u8bf7\u6c42\u3002<\/p>\n

“`php<\/p>\n

if (isset($_POST[‘product_id’]) && isset($_POST[‘user_id’])) {<\/p>\n

\/\/ \u9a8c\u8bc1\u5546\u54c1ID\u548c\u7528\u6237ID<\/p>\n

$productID = $_POST[‘product_id’];<\/p>\n

$userID = $_POST[‘user_id’];<\/p>\n

\/\/ \u5982\u679c\u5546\u54c1ID\u548c\u7528\u6237ID\u6709\u6548\uff0c\u8fdb\u884c\u652f\u4ed8\u6d41\u7a0b<\/p>\n

if (isset($products[$productID]) && !empty($userID)) {<\/p>\n

\/\/ \u751f\u6210\u8ba2\u5355\u53f7<\/p>\n

$orderNumber = time().”_”.rand(100000, 999999);<\/p>\n

\/\/ \u5904\u7406\u652f\u4ed8<\/p>\n

processPayment($orderNumber, $productID, $userID);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u6574\u4e2a\u6e90\u7801\u4ec5\u4f5c\u4e3a\u6f14\u793a\u5982\u4f55\u5b9e\u73b0\u82f9\u679c\u514d\u7b7ePHP\u652f\u4ed8\u6d41\u7a0b\u3002\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6d89\u53ca\u652f\u4ed8\u6d41\u7a0b\u53ca\u4f9d\u8d56\u7b2c\u4e09\u65b9\u652f\u4ed8\u5e73\u53f0\uff0c\u8bf7\u4ed4\u7ec6\u5ba1\u67e5\u4ee3\u7801\uff0c\u5e76\u8c28\u614e\u4f7f\u7528\u3002\u6211\u4eec\u5efa\u8bae\u60a8\u91c7\u7528\u5408\u89c4\u7684\u652f\u4ed8\u65b9\u5f0f\uff0c\u9075\u5b88\u82f9\u679c\u5546\u5e97\u7684\u76f8\u5173\u653f\u7b56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u82f9\u679c\u514d\u7b7e\u652f\u4ed8\u662f\u4e00\u4e2a\u7ed5\u8fc7\u82f9\u679c\u5b98\u65b9\u652f\u4ed8\u7cfb\u7edf\uff0c\u4f7f\u7528\u4e2a\u4eba\u5f00\u53d1\u8005\u8d26\u53f7\u5b9e\u73b0\u7684\u4e00\u79cd\u652f\u4ed8\u6a21\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u6a21\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8d2d\u4e70\u865a\u62df\u5546\u54c1\u6216\u670d\u52a1,\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u82f9\u679c\u7684\u652f\u4ed8\u7cfb\u7edf\uff0c\u4ece\u800c\u907f\u5f00\u82f9\u679c\u7684\u5ba1\u67e5\u548c\u62bd\u6210\u3002\u7136\u800c,\u8fd9\u79cd\u65b9\u5f0f\u8fdd\u53cd\u4e86<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[1545,849,205,2532,22593],"topic":[],"class_list":["post-24167","post","type-post","status-publish","format-standard","hentry","category-iosmainqian","tag-1545","tag-849","tag-205","tag-2532","tag-22593"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/24167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=24167"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/24167\/revisions"}],"predecessor-version":[{"id":24302,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/24167\/revisions\/24302"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=24167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=24167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=24167"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=24167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}