当前位置:Gxlcms > PHP教程 > 微信支付与支付宝支付整合PHP实现

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

时间:2021-07-01 10:21:17 帮助过:31人阅读

利用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...    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 = <<    使用微信二维码支付                        请使用微信扫一扫
扫描二维码支付 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

人气教程排行