Code Monkey home page Code Monkey logo

weixin_alipay_php's Introduction

利用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;
}

weixin_alipay_php's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.