欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信支付与支付宝支付整合 PHP实现

程序员文章站 2022-06-17 12:07:10
...
利用PHP的反射把两种支付接口统一起来。

使用前需要配置目录下的Config文件即WxpayConfig.php与AlipayConfig.php

使用支付宝支付
 $this->generateUrl('pay_return', array('name' => 'alipay'), true),    'notifyUrl' => $this->generateUrl('pay_notify', array('name' => 'alipay'), true),    'showUrl' => $this->generateUrl('show_goods', array('id' => $goods['id']), true),);$paymentRequest = createPaymentRequest($order, $requestParams);function createPaymentRequest($order, $requestParams){    $requestParams = array_merge($requestParams, array(            'orderSn' => $order['sn'],            'title' => $order['title'],            'summary' => '',            'amount' => $order['amount'],    ));    return Payment::createRequest('alipay', $requestParams);}$htmlForm = $request->form();$inputHtml = '';foreach ($htmlForm['params'] as $key => $value) {    $inputHtml .= "";}$html = Jumping to alipay gateway...
{$inputHtml}
EOF;echo $html;die;
使用微信支付

微信支付依赖composer的把url转换为二维码text的Endroid\QrCode\QrCode库。

 $this->generateUrl('pay_return', array('name' => 'wxpay'), true),    'notifyUrl' => $this->generateUrl('pay_notify', array('name' => 'wxpay'), true),    'showUrl' => $this->generateUrl('show_goods', array('id' => $goods['id']), true),);$paymentRequest = createPaymentRequest($order, $requestParams);function createPaymentRequest($order, $requestParams){    $requestParams = array_merge($requestParams, array(            'orderSn' => $order['sn'],            'title' => $order['title'],            'summary' => '',            'amount' => $order['amount'],    ));    return Payment::createRequest('wxpay', $requestParams);}$returnXml = $paymentRequest->unifiedOrder();$returnArray = $paymentRequest->fromXml($returnXml);if ($returnArray['return_code'] == 'SUCCESS') {    $url = $returnArray['code_url'];    $html = 使用微信二维码支付微信支付与支付宝支付整合 PHP实现EOF;    echo $html;    die;}

 回调数据处理  
if (支付宝) {    $response = Payment::createResponse('alipay', $_POST);} elseif (微信) {    $returnXml = $GLOBALS['HTTP_RAW_POST_DATA'];    $response = Payment::createResponse('wxpay', fromXml($returnXml));}$payData = $response->getPayData();if ($payData['status'] == "success") {    //干支付成功该干的事情}function fromXml($xml){    $array = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);    return $array;}

项目主页:http://www.open-open.com/lib/view/home/1441781914992