微信支付與支付寶支付整合 PHP實現
利用PHP的反射把兩種支付接口統一起來。
使用前需要配置目錄下的Config文件即WxpayConfig.php與AlipayConfig.php
使用支付寶支付
<?php namespace WatcherHangzhouPayment\Payment; $payRequestParams = array( 'returnUrl' => $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 .= "<input type=\"hidden\" name=\"{$key}\" value=\"{$value}\">"; } $html = <<<EOF <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Jumping to alipay gateway...</title> <body> <form action="{$htmlForm['action']}" method="{$htmlForm['method']}" name="form"> {$inputHtml} </form> <script> document.all.form.submit(); </script> </body> </html> EOF; echo $html; die;
使用微信支付
微信支付依賴composer的把url轉換為二維碼text的Endroid\QrCode\QrCode庫。
<?php namespace WatcherHangzhouPayment\Payment; <?php namespace WatcherHangzhouPayment\Payment; $payRequestParams = array( 'returnUrl' => $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 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用微信二維碼支付</title> <body> <img src="{{ path('generate_qrcode_image',{url:url}) }}" > <div class="text-qrcode hidden-xs"> 請使用微信掃一掃<br>掃描二維碼支付 </div> </body> </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; }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!