php实现微信和支付宝支付的示例代码

2020-09-01 10:24:48

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' => 'jianyaneasywechatWechat',     'userOptions' => [], // 用户身份类参数     'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥     'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中     'rebinds' => [ // 自定义服务模块        // 'cache' => 'commoncomponentsCache',     ]   ],   // ... ] 

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 yiibaseErrorException('微信支付异常, 请稍后再试'); }  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 yiibaseErrorException($errorMsg);   } } catch (Exception $e) {   throw new yiibaseErrorException($e->getMessage()); }

根据如上就可以实现支付宝支付

相关文章 大家在看