php实现微信支付
在php下实现微信支付,这里我使用了EasyWeChat
这里我是在Yii框架实现的,安装EasyWeChat插件composerrequirejianyan74/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'=>[],
二:实现微信支付
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文件中发起支付
//数组内为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();
}
在异步回调地址中获取微信支付回调只需要使用如下方法即可:$payment = Yii::$app->wechat->payment;
$response = $payment->handlePaidNotify(function($message, $fail) {
//支付结果逻辑,只有在函数里 return true; 才代表处理完成
});
$response->send();
根据如上步骤就可以实现微信支付
php实现支付宝支付
一:在php中安装支付宝插件composer require alipaysdk/easysdk
二:php实现支付宝支付
1:配置支付宝/**
* 支付宝配置
*/
public static function getOptions()
{
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = '';
$options->signType = 'RSA2';
$options->appId = '';
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
$options->merchantPrivateKey = '';
$options->alipayCertPath = '';
$options->alipayRootCertPath = '';
$options->merchantCertPath = '';
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
// $options->alipayPublicKey = '';
//可设置异步通知接收服务地址(可选)
$options->notifyUrl = "";
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
//$options->encryptKey = "";
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());
}
根据如上就可以实现支付宝支付