php實現微信支付
微信支付文檔地址:https://pay.weixin.qq.com/wiki/doc/api/index.html
在php下實現微信支付,這里我使用了EasyWeChat
這里我是在Yii框架實現的,安裝EasyWeChat插件
composer require jianyan74/yii2-easy-wechat
一:配置EasyWeChat
1:在config/main.php 的 component中添加EasyWeChat的SDK
'components' => [
// ...
'wechat' => [
'class' => 'jianyan\easywechat\Wechat',
'userOptions' => [], // 用戶身份類參數
'sessionParam' => 'wechatUser', // 微信用戶信息將存儲在會話在這個密鑰
'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存儲在會話中
'rebinds' => [ // 自定義服務模塊
// 'cache' => 'common\components\Cache',
]
],
// ...
]
2:在config/params.php中設置基礎配置信息和微信支付信息
// 微信配置 具體可參考EasyWechat
'wechatConfig' => [],
// 微信支付配置 具體可參考EasyWechat
'wechatPaymentConfig' => [],
// 微信小程序配置 具體可參考EasyWechat
'wechatMiniProgramConfig' => [],
// 微信開放平臺第三方平臺配置 具體可參考EasyWechat
'wechatOpenPlatformConfig' => [],
// 微信企業微信配置 具體可參考EasyWechat
'wechatWorkConfig' => [],
// 微信企業微信開放平臺 具體可參考EasyWechat
'wechatOpenWorkConfig' => [],
// 微信小微商戶 具體可參考EasyWechat
'wechatMicroMerchantConfig' => [],
具體配置方法可以參考GitHub的說明:https://github.com/jianyan74/yii2-easy-wechat
二:實現微信支付
1:微信支付api
$data = [
'body' => '',//支付描述
'out_trade_no' => '',//訂單號
'total_fee' => '',//支付金額
'notify_url' => '', // 支付結果通知網址,如果不設置則會使用配置里的默認地址
'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通過上面的獲取實例來獲取
'config' => $config
]);
2:在wxpay.php文件中發起支付
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">
//數組內為jssdk授權可用的方法,按需添加,詳細查看微信jssdk的方法
wx.config(?php echo $jssdk->buildConfig(array('chooseWXPay'), true) ?>);
function onBridgeReady(){
// 發起支付
wx.chooseWXPay({
timestamp: ?= $config['timestamp'] ?>,
nonceStr: '?= $config['nonceStr'] ?>',
package: '?= $config['package'] ?>',
signType: '?= $config['signType'] ?>',
paySign: '?= $config['paySign'] ?>', // 支付簽名
success: function (res) {
// 支付成功后的回調函數
},
cancel: function(r) {
//支付取消后的回調函數
},
});
}
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>
在異步回調地址中獲取微信支付回調只需要使用如下方法即可:
$payment = Yii::$app->wechat->payment;
$response = $payment->handlePaidNotify(function($message, $fail) {
//支付結果邏輯,只有在函數里 return true; 才代表處理完成
});
$response->send();
根據如上步驟就可以實現微信支付
php實現支付寶支付
支付寶支付文檔地址:https://opendocs.alipay.com/open/00y8k9
一:在php中安裝支付寶插件
composer require alipaysdk/easysdk
alipaysdk/easysdk的GitHub地址:https://github.com/alipay/alipay-easysdk/tree/master/php
二:php實現支付寶支付
1:配置支付寶
/**
* 支付寶配置
*/
public static function getOptions()
{
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
$options->appId = '-- 請填寫您的AppId,例如:2019022663440152 -->';
// 為避免私鑰隨源碼泄露,推薦從文件中讀取私鑰字符串而不是寫入源碼中
$options->merchantPrivateKey = '-- 請填寫您的應用私鑰,例如:MIIEvQIBADANB ... ... -->';
$options->alipayCertPath = '-- 請填寫您的支付寶公鑰證書文件路徑,例如:/foo/alipayCertPublicKey\_RSA2.crt -->';
$options->alipayRootCertPath = '-- 請填寫您的支付寶根證書文件路徑,例如:/foo/alipayRootCert.crt" -->';
$options->merchantCertPath = '-- 請填寫您的應用公鑰證書文件路徑,例如:/foo/appCertPublicKey\_2019051064521003.crt -->';
//注:如果采用非證書模式,則無需賦值上面的三個證書路徑,改為賦值如下的支付寶公鑰字符串即可
// $options->alipayPublicKey = '-- 請填寫您的支付寶公鑰,例如:MIIBIjANBg... -->';
//可設置異步通知接收服務地址(可選)
$options->notifyUrl = "-- 請填寫您的支付類接口異步通知接收服務地址,例如:https://www.test.com/callback -->";
//可設置AES密鑰,調用AES加解密相關接口時需要(可選)
//$options->encryptKey = "-- 請填寫您的AES密鑰,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
return $options;
}
2:實現支付寶支付
//加載支付寶配置
Factory::setOptions(self::getOptions());
try {
//發起API調用
$result = Factory::payment()->wap()->pay('訂單標題', '商戶訂單號', '訂單總金額', '用戶付款中途退出返回商戶網站的地址', '支付回調地址');
$responseChecker = new ResponseChecker();
//處理響應或異常
if ($responseChecker->success($result)) {
//調用成功
return $result->body;
} else {
//調用失敗
$errorMsg = $result->msg . $result->subMsg;
throw new yii\\base\\ErrorException($errorMsg);
}
} catch (\\Exception $e) {
throw new yii\\base\\ErrorException($e->getMessage());
}
根據如上就可以實現支付寶支付
到此這篇關于php實現微信和支付寶支付的示例代碼的文章就介紹到這了,更多相關php實現微信和支付寶支付內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- PHP后臺微信支付和支付寶支付開發
- PHP實現QQ、微信和支付寶三合一收款碼實例代碼
- PHP實現一個二維碼同時支持支付寶和微信支付的示例