php實(shí)現(xiàn)微信和支付寶支付的示例代碼
php實(shí)現(xiàn)微信支付
微信支付文檔地址:https://pay.weixin.qq.com/wiki/doc/api/index.html
在php下實(shí)現(xiàn)微信支付,這里我使用了EasyWeChat
這里我是在Yii框架實(shí)現(xiàn)的,安裝EasyWeChat插件
composer require jianyan74/yii2-easy-wechat
一:配置EasyWeChat
1:在config/main.php 的 component中添加EasyWeChat的SDK
'components' => [
// ...
'wechat' => [
'class' => 'jianyan\easywechat\Wechat',
'userOptions' => [], // 用戶身份類參數(shù)
'sessionParam' => 'wechatUser', // 微信用戶信息將存儲在會話在這個(gè)密鑰
'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存儲在會話中
'rebinds' => [ // 自定義服務(wù)模塊
// 'cache' => 'common\components\Cache',
]
],
// ...
]
2:在config/params.php中設(shè)置基礎(chǔ)配置信息和微信支付信息
// 微信配置 具體可參考EasyWechat 'wechatConfig' => [], // 微信支付配置 具體可參考EasyWechat 'wechatPaymentConfig' => [], // 微信小程序配置 具體可參考EasyWechat 'wechatMiniProgramConfig' => [], // 微信開放平臺第三方平臺配置 具體可參考EasyWechat 'wechatOpenPlatformConfig' => [], // 微信企業(yè)微信配置 具體可參考EasyWechat 'wechatWorkConfig' => [], // 微信企業(yè)微信開放平臺 具體可參考EasyWechat 'wechatOpenWorkConfig' => [], // 微信小微商戶 具體可參考EasyWechat 'wechatMicroMerchantConfig' => [],
具體配置方法可以參考GitHub的說明:https://github.com/jianyan74/yii2-easy-wechat
二:實(shí)現(xiàn)微信支付
1:微信支付api
$data = [
'body' => '',//支付描述
'out_trade_no' => '',//訂單號
'total_fee' => '',//支付金額
'notify_url' => '', // 支付結(jié)果通知網(wǎng)址,如果不設(shè)置則會使用配置里的默認(rèn)地址
'trade_type' => 'JSAPI',//支付方式
'openid' => '',//用戶openid
];
// 生成支付配置
$payment = Yii::$app->wechat->payment;
$result = $payment->order->unify($data);
if ($result['return_code'] == 'SUCCESS') {
$prepayId = $result['prepay_id'];
$config = $payment->jssdk->sdkConfig($prepayId);
} else {
throw new yii\base\ErrorException('微信支付異常, 請稍后再試');
}
return $this->render('wxpay', [
'jssdk' => $payment->jssdk, // $app通過上面的獲取實(shí)例來獲取
'config' => $config
]);
2:在wxpay.php文件中發(fā)起支付
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
//數(shù)組內(nèi)為jssdk授權(quán)可用的方法,按需添加,詳細(xì)查看微信jssdk的方法
wx.config(<?php echo $jssdk->buildConfig(array('chooseWXPay'), true) ?>);
function onBridgeReady(){
// 發(fā)起支付
wx.chooseWXPay({
timestamp: <?= $config['timestamp'] ?>,
nonceStr: '<?= $config['nonceStr'] ?>',
package: '<?= $config['package'] ?>',
signType: '<?= $config['signType'] ?>',
paySign: '<?= $config['paySign'] ?>', // 支付簽名
success: function (res) {
// 支付成功后的回調(diào)函數(shù)
},
cancel: function(r) {
//支付取消后的回調(diào)函數(shù)
},
});
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
</script>
在異步回調(diào)地址中獲取微信支付回調(diào)只需要使用如下方法即可:
$payment = Yii::$app->wechat->payment;
$response = $payment->handlePaidNotify(function($message, $fail) {
//支付結(jié)果邏輯,只有在函數(shù)里 return true; 才代表處理完成
});
$response->send();
根據(jù)如上步驟就可以實(shí)現(xiàn)微信支付
php實(shí)現(xiàn)支付寶支付
支付寶支付文檔地址:https://opendocs.alipay.com/open/00y8k9
一:在php中安裝支付寶插件
composer require alipaysdk/easysdk
alipaysdk/easysdk的GitHub地址:https://github.com/alipay/alipay-easysdk/tree/master/php
二:php實(shí)現(xiàn)支付寶支付
1:配置支付寶
/**
* 支付寶配置
*/
public static function getOptions()
{
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
$options->appId = '<-- 請?zhí)顚懩腁ppId,例如:2019022663440152 -->';
// 為避免私鑰隨源碼泄露,推薦從文件中讀取私鑰字符串而不是寫入源碼中
$options->merchantPrivateKey = '<-- 請?zhí)顚懩膽?yīng)用私鑰,例如:MIIEvQIBADANB ... ... -->';
$options->alipayCertPath = '<-- 請?zhí)顚懩闹Ц秾毠€證書文件路徑,例如:/foo/alipayCertPublicKey\_RSA2.crt -->';
$options->alipayRootCertPath = '<-- 請?zhí)顚懩闹Ц秾毟C書文件路徑,例如:/foo/alipayRootCert.crt" -->';
$options->merchantCertPath = '<-- 請?zhí)顚懩膽?yīng)用公鑰證書文件路徑,例如:/foo/appCertPublicKey\_2019051064521003.crt -->';
//注:如果采用非證書模式,則無需賦值上面的三個(gè)證書路徑,改為賦值如下的支付寶公鑰字符串即可
// $options->alipayPublicKey = '<-- 請?zhí)顚懩闹Ц秾毠€,例如:MIIBIjANBg... -->';
//可設(shè)置異步通知接收服務(wù)地址(可選)
$options->notifyUrl = "<-- 請?zhí)顚懩闹Ц额惤涌诋惒酵ㄖ邮辗?wù)地址,例如:https://www.test.com/callback -->";
//可設(shè)置AES密鑰,調(diào)用AES加解密相關(guān)接口時(shí)需要(可選)
//$options->encryptKey = "<-- 請?zhí)顚懩腁ES密鑰,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
return $options;
}
2:實(shí)現(xiàn)支付寶支付
//加載支付寶配置
Factory::setOptions(self::getOptions());
try {
//發(fā)起API調(diào)用
$result = Factory::payment()->wap()->pay('訂單標(biāo)題', '商戶訂單號', '訂單總金額', '用戶付款中途退出返回商戶網(wǎng)站的地址', '支付回調(diào)地址');
$responseChecker = new ResponseChecker();
//處理響應(yīng)或異常
if ($responseChecker->success($result)) {
//調(diào)用成功
return $result->body;
} else {
//調(diào)用失敗
$errorMsg = $result->msg . $result->subMsg;
throw new yii\\base\\ErrorException($errorMsg);
}
} catch (\\Exception $e) {
throw new yii\\base\\ErrorException($e->getMessage());
}
根據(jù)如上就可以實(shí)現(xiàn)支付寶支付
到此這篇關(guān)于php實(shí)現(xiàn)微信和支付寶支付的示例代碼的文章就介紹到這了,更多相關(guān)php實(shí)現(xiàn)微信和支付寶支付內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Zend Framework教程之分發(fā)器Zend_Controller_Dispatcher用法詳解
這篇文章主要介紹了Zend Framework教程之分發(fā)器Zend_Controller_Dispatcher用法,結(jié)合實(shí)例形式詳細(xì)分析了分發(fā)器Zend_Controller_Dispatcher的結(jié)構(gòu),功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
PHP中使用substr()截取字符串出現(xiàn)中文亂碼問題該怎么辦
本文給大家介紹使用php substr()截取字符串出現(xiàn)亂碼問題該怎么辦,涉及到php substr()方法的一些知識點(diǎn),感興趣的朋友一起學(xué)習(xí)下吧2015-10-10
ThinkPHP實(shí)現(xiàn)的rsa非對稱加密類示例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)的rsa非對稱加密類,結(jié)合實(shí)例形式分析了thinkPHP引入密鑰文件實(shí)現(xiàn)rsa加密解密的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
在Laravel框架里實(shí)現(xiàn)發(fā)送郵件實(shí)例(郵箱驗(yàn)證)
這篇文章主要介紹了在Laravel框架里實(shí)現(xiàn)發(fā)送郵件(郵箱驗(yàn)證)的相關(guān)資料,需要的朋友可以參考下2016-05-05
Session的工作機(jī)制詳解和安全性問題(PHP實(shí)例講解)
有一點(diǎn)我們必須承認(rèn),大多數(shù)web應(yīng)用程序都離不開session的使用。這篇文章將會結(jié)合php以及http協(xié)議來分析如何建立一個(gè)安全的會話管理機(jī)制2014-04-04
制作安全性高的PHP網(wǎng)站的幾個(gè)實(shí)用要點(diǎn)
這篇文章主要介紹了制作安全性高的PHP網(wǎng)站的幾個(gè)實(shí)用要點(diǎn),需要的朋友可以參考下2014-12-12

