鐢宠鍟嗘埛鍙?/h2>
鐢宠鍦板潃锛?/strong>https://pay.weixin.qq.com/濡傛灉鎮ㄦ病鏈夊井淇″晢鎴疯处鍙凤紝璇风偣鍑讳笂鏂归摼鎺ョ敵璇枫€傚鏋滄偍宸茬粡鏈夛紝鍙互璺宠繃姝ゆ楠わ細 鐢宠鍟嗘埛璇佷功
棣栧厛鐐瑰嚮璐︽埛涓績 API 瀹夊叏 鐢宠API 璇佷功鐢宠璇︾粏姝ラ锛?/strong>https://kf.qq.com/faq/161222NneAJf161222U7fARv.html 璁剧疆APIv3瀵嗛挜
棣栧厛鐐瑰嚮璐︽埛涓績 API瀹夊叏 璁剧疆APIv3 Key 璁剧疆浼氱湅鍒版湁涓や釜瀵嗛挜锛屽垎鍒槸APIv2 key 鍜孉PIv3 Key锛岀敱浜嶢PIv2 key 宸茬粡閫愭笎搴熷純锛屾墍浠ュ彧闇€瑕佺敵璇稟PIv3 key 鍗冲彲銆傚瘑閽ュ彲浠ユ槸鏁板瓧銆佸ぇ灏忓啓瀛楁瘝鐨勭粍鍚堬紝鍙互杈撳叆浠绘剰32浣嶅瓧绗︺€傚瘑閽ラ渶瑕佷繚瀛樿捣鏉ヤ緵浠ュ悗浣跨敤鍚楋紵php //鐢熸垚32浣岮PIv3闅忔満瀵嗛挜$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; echo substr(str_shuffle($chars), 0, $length);澶嶅埗锛?h2> 涓嬭浇 SDK 寮€鍙戝寘
寰俊瀹樻柟鎻愪緵JAVA銆丳HP銆丟O 涓夌璇█鐗堟湰鐨勫紑鍙戝簱锛岃閫夋嫨JAVA璇█锛?/strong>wechatpay-java 銆堟帹鑽愩€夈€亀echatpay-apache-httpclientPHP璇█锛?/strong>wechatpay-php 銆堟帹鑽愩€夈€亀echatpay- guzzle-middlewareGO璇█锛?/strong>wechatpay-go 銆堟帹鑽愩€夋牴鎹嚜宸辩殑寮€鍙戣瑷€瀹炵幇鏀粯姣旇緝绠€鍗曪紝鎵€浠ユ垜灏辩敤php浣滀负鏀粯鐨勮В閲娿€備竴銆佷娇鐢╟omposer瀹夎sdk#鍒濆鍖栨枃浠跺すcomposer init#寤鸿浣跨敤PHP鍖呯鐞嗗伐鍏稢omposer瀹夎SDKcomposer require wechatpay/wechatpay copy: 涓嬭浇骞冲彴璇佷功
骞冲彴璇佷功涓庝笂闈㈢敵璇风殑涓€鏍峰晢鎴疯瘉涔︿笉鏄悓涓€浠朵簨銆傚悗闈㈢殑璇锋眰涓紝蹇呴』鎼哄甫骞冲彴璇佷功鍜屽晢鎴疯瘉涔︺€傛墽琛屼笂杩板懡浠ゅ悗锛屼細鏈変竴涓獀endor/bin/CertificateDownloader.php鏂囦欢銆傚鏋滄槸绗竴娆$敵璇峰钩鍙拌瘉涔︼紝闇€瑕佹墽琛屽懡浠わ細php CertificateDownloader.php -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}-k锛?/strong>apiv3 绉橀挜锛屼笂闈㈣缃殑32 浣嶆暟瀛楃閽?strong>-m锛?/strong>鏄晢鎴峰彿锛屽井淇″晢鎴峰钩鍙板彲浠ユ煡璇㈠埌-f锛?/strong>寰俊鍟嗘埛API 绉侀挜鏂囦欢鐩綍锛屽嵆绗簩姝ョ敵璇峰晢鎴疯瘉涔︾敓鎴愮殑apiclient_key.pem璺緞銆?strong>-s锛?/strong>璇佷功搴忓垪鍙峰湪璐︽埛涓績API瀹夊叏绠$悊璇佷功涓彲浠ョ湅鍒般€傚鏋滄湁澶氫釜璇佷功锛岃鏌ユ壘鎮ㄦ鍦ㄤ娇鐢ㄧ殑璇佷功鐨勫簭鍒楀彿銆?
}}if(!isWeChat()) { // 闈炲井淇″唴鎵撳紑鐨勪骇鍝侀〉闈? alert('寰俊澶栦笉鏀寔JSAPI鏀粯锛岃鍦ㄥ井淇′腑鎵撳紑椤甸潰'); return false;}// 鐢ㄦ埛鎸戦€夊畬鍟嗗搧鍚庤烦杞紝杩欓噷appid闇€瑕佷笂闈㈣窡鍟嗘埛缁戝畾鐨勫叕浼楀彿appid// 寰俊鎺堟潈鍒嗕负闈欓粯鎺堟潈鍜岄潪闈欓粯鎺堟潈锛屽叾涓潪闈欓粯鎺堟潈锛岄渶瑕佺敤鎴风偣鍑荤‘璁ゆ巿鏉冨悗锛屾墠鍙互鑾峰彇code锛?/ 鍥犱负杩欓噷涓绘墦涓€涓敤鎴锋棤鎰熺煡锛岃€屼笖鎴戜滑鍙渶瑕乷penid鍗冲彲锛屾墍浠ユ垜浠彧闇€瑕佷娇鐢ㄩ潤榛樻巿鏉冨嵆鍙?/ 闈欓粯鎺堟潈鍙互鑾峰彇鐢ㄦ埛鏇村鐨勪俊鎭紝姣斿澶村儚銆佹樀绉扮瓑锛岃€岄潤榛樻巿鏉冨彧鑳借幏鍙杘penid锛岃繖鐐归渶瑕佹敞鎰忥紝鍏蜂綋鎯呭喌閫夋嫨涓嶅悓// 闈為潤榛樻巿鏉?/ $(location).attr('href', `https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxx&redirect_uri=${encodeURIComponent('https://xxxx/b.html')}&response_type=code&scope=snsapi_userinfo#wechat_redirect`)// 闈欓粯鎺堟潈$(location).attr('href', `https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxx&redirect_uri=${encodeURIComponent('https://xxxx/b.html')}&response_type=code&scope=snsapi_base#wechat_redirect`)澶嶅埗// b椤甸潰锛屼粎鍋氶€昏緫婕旂ず锛屾洿鍔犲叿浣撶殑閫昏緫闇€瑕佽嚜宸卞畬鍠刲et openid = '';// 鑾峰彇code, 璇锋眰鎺ュ彛鑾峰彇openidfunction getParamUrl(name, url) { if (!url) url = location.href; if (url.indexOf('?') == -1) return ''; try { var re = new RegExp("" + name + "=([^&?]*)", "ig"); return ((url.match(re)) ? (decodeURIComponent(url.match(re)[0].substr(name.length + 1))) : ''); } catch (_e) { return ''; }}let code = getParamUrl('code');$.getJSON('鍚庣鎺ュ彛鍦板潃/openid?callback=?', function(res) { if(res.code == 200) { openid = res.data; } else { console.error(res.msg); }})// 鐢ㄦ埛纭畾璁㈠崟鍚庯紝鎷夎捣鏀粯let params = { total: 2, // 鍗曚綅锛氬厓 description: 'Image褰㈣薄搴?娣卞湷鑵惧ぇ-QQ鍏粩', // 浜у搧鐨勪粙缁? openid: openid //鐢ㄦ埛鐨刼penid // ....鏇村鍏ュ簱鍙傛暟};$.getJSON('鍚庣鎺ュ彛鍦板潃/jssapi?' + $.param(params) + '&callback=?', function(res) { WeixinJSBridge.invoke('getBrandWCPayRequest', { 'appId': res.data.sign.appId, 'timeStamp': res.data.sign.timeStamp, 'nonceStr': res.data.sign.nonceStr, 'package': res.data.sign.package, 'signType': res.data.sign.signType, 'paySign': res.data.sign.paySign }, function (response) { if (response.err_msg == "get_brand_wcpay_request:ok") { $(location).attr("href", `https://xxxxxx/finish?out_trade_no=${res.data.out_trade_no}`) } else { // 鏈変簺鐢ㄦ埛璋冭捣浜嗘敮浠橈紝浣嗘槸鏈粯娆惧彇娑堢殑澶勭悊鏂瑰紡锛屼綘鍙互缁欎粬绠€鍗曠畝鍗曟彁绀? toast('鏀粯寮傚父鍙栨秷') // 褰撶劧鏈変簺鐢ㄦ埛鏄鎿嶄綔锛屼綘鍙互鎻愰啋浜屾鏀粯 if(confirm('妫€娴嬪埌浣犳搷浣滄湁璇紝鏄惁閲嶆柊鏀粯?')) { WeixinJSBridge.invoke('getBrandWCPayRequest', { 'appId': res.data.sign.appId, 'timeStamp': res.data.sign.timeStamp, 'nonceStr': res.data.sign.nonceStr, 'package': res.data.sign.package, 'signType': res.data.sign.signType, 'paySign': res.data.sign.paySign }, function (response) { if (response.err_msg == "get_brand_wcpay_request:ok") { $(location).attr("href", `https://xxxxxx/finish?out_trade_no=${res.data.out_trade_no}`) } }) } } });});澶嶅埗<?php// 鑾峰彇鐢ㄦ埛鐨刼penid$input = $request->only(['code']);$response = getCurl("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->secret}&code={$input['code']}&grant_type=authorization_code");$openid = json_decode($response, true)['openid'];// 杩斿洖openidreturn jsonp([ 'code' => 200, 'msg' => '鑾峰彇鎴愬姛', 'data' => $openid]);// 灏佽鐨凣ET璇锋眰function getCurl($url, $timeout = 5){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $result = curl_exec($ch); curl_close($ch); return $result;}澶嶅埗<?php// 浠呬粎鐢ㄤ綔灞曠ず锛屼笉鍙洿鎺ュ鍒朵娇鐢╮equire_once('../vendor/autoload.php');use WeChatPay\Builder;use WeChatPay\Formatter;use WeChatPay\Crypto\Rsa;use WeChatPay\Util\PemUtil;// 鎺ュ彈鍙傛暟锛岀浉褰撲簬鍘熺敓鐨?_GET锛岃繖閲屼細姣攈5鏀粯澶氫竴涓猳penid$input = $request->only(['openid', 'name', 'total', 'description', 'phone']);// 鐢熸垚鍟嗘埛璁㈠崟鍙?out_trade_no = getOutTradeNo();// 澶勭悊閲戦// 鐢变簬寰俊浣跨敤鐨勬槸鍒嗕綔涓哄崟浣嶏紝鎵€浠ュ墠绔紶鐨勬槸鍏冪殑璇濓紝闇€瑕佽浆鎹竴涓?total = $input['total'] * 100;// 鍟嗘埛鍙?merchantId = '1xxxxxx1';// 浠庢湰鍦版枃浠朵腑鍔犺浇銆屽晢鎴稟PI绉侀挜銆嶏紝銆屽晢鎴稟PI绉侀挜銆嶄細鐢ㄦ潵鐢熸垚璇锋眰鐨勭鍚?merchantPrivateKeyFilePath = 'file://../cert/merchant/apiclient_key.pem';$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);// 銆屽晢鎴稟PI璇佷功銆嶇殑銆岃瘉涔﹀簭鍒楀彿銆?merchantCertificateSerial = '1xxxxxxxxxxxxxxxxxxxxx91';// 浠庢湰鍦版枃浠朵腑鍔犺浇銆屽井淇℃敮浠樺钩鍙拌瘉涔︺€嶏紝鐢ㄦ潵楠岃瘉寰俊鏀粯搴旂瓟鐨勭鍚?platformCertificateFilePath = 'file://../cert/wechatpay/wechatpay_4xxxxxxxxxxxxxxxxxxx9.pem';$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);// 浠庛€屽井淇℃敮浠樺钩鍙拌瘉涔︺€嶄腑鑾峰彇銆岃瘉涔﹀簭鍒楀彿銆?platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);// 鏋勯€犱竴涓?APIv3 瀹㈡埛绔疄渚?instance = Builder::factory([ 'mchid' => $merchantId, 'serial' => $merchantCertificateSerial, 'privateKey' => $merchantPrivateKeyInstance, 'certs' => [ $platformCertificateSerial => $platformPublicKeyInstance, ],]);try { // 璋冪敤 transactions/jsapi 鎺ュ彛鍚庝細鐢熸垚prepay_id $resp = $this->instance() ->chain('v3/pay/transactions/jsapi') ->post(['json' => [ 'mchid' => $merchantId, // 鍟嗘埛鍙? 'out_trade_no' => $out_trade_no, // 鍟嗘埛璁㈠崟鍙? 'appid' => '********鎹㈡垚璺熷晢鎴峰彿缁戝畾鐨勫叕浼楀彿APPID**********', 'description' => $input['description'], //鍟嗗搧鎻忚堪 'notify_url' => 'https://xxxxx/notify', // 鐢ㄦ埛鏀粯鍚庣殑鍥炶皟鍦板潃锛屽湪杩欓噷淇敼璁㈠崟鐨勭姸鎬? 'amount' => [ 'total' => $total, 'currency' => 'CNY' ], 'payer' => [ 'openid' => $input['openid'] ] ]]); // 闇€瑕佹牴鎹畃repay_id鍘荤敓鎴愬姞瀵嗙殑淇℃伅 $prepay_id = json_decode($resp->getBody(), true)['prepay_id']; $sign = getSign($prepay_id); // 濡傛灉璇锋眰鎴愬姛锛岄渶瑕佸皢涓€浜涘弬鏁拌繘琛屽叆搴擄紝杩欓噷浠呬綔婕旂ず锛岄潪姝e紡鏁版嵁鍏ュ簱 $response = Db::table('order')->insert([ 'openid' => $input['openid'], 'name' => $input['name'], 'description' => $input['description'], 'total' => $input['total'], 'phone' => $input['phone'], 'trade_state' => 'START', ]); // 鍏ュ簱鎴愬姛鍚庯紝灏嗚烦杞摼鎺ュ拰璁㈠崟鍙蜂紶缁欏墠绔紝鍓嶇鎷垮埌璺宠浆鍦板潃璺宠浆鍗冲彲 if($response) { return jsonp([ 'code' => 200, 'msg' => '鎿嶄綔鎴愬姛', 'data' => [ 'out_trade_no' => $out_trade_no, 'sign' => $sign ] ]); } else { return jsonp([ 'code' => 100, 'msg' => '鎿嶄綔澶辫触' ]); }} catch (\Exception $e) { // 杩涜閿欒澶勭悊 if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL; }}// 鑾峰彇鍔犲瘑鍙傛暟function getSign($prepay_id){ $merchantPrivateKeyInstance = Rsa::from($this->merchantPrivateKeyFilePath); $params = [ 'appId' => $this->appid, 'timeStamp' => (string)Formatter::timestamp(), 'nonceStr' => Formatter::nonce(), 'package' => 'prepay_id=' . $prepay_id, ]; $params += ['paySign' => Rsa::sign( Formatter::joinedByLineFeed(...array_values($params)), $merchantPrivateKeyInstance ), 'signType' => 'RSA']; return $params;}澶嶅埗