Skip to content
nilnice edited this page Mar 2, 2018 · 2 revisions

微信支付中 total_fee 单位为分,也就是说传入参数 1 就是支付 1 分钱

微信 App 支付

<?php

require __DIR__ . '/../vendor/autoload.php';

$config = require __DIR__ . '/config.php';
$config = $config['wechat'];
$action = $_REQUEST['action'] ?? 'pay';

$wechat = new \Nilnice\Payment\Wechat($config);

if ($action === 'pay') {
    $order = [
        'out_trade_no' => time(),
        'total_fee'    => 1,
        'body'         => '微信支付-测试订单',
    ];
    $object = $wechat->app($order);
    dd($object);
} elseif ($action === 'query') {
    $order = 'order_id';
    $info = $wechat->query($order);
    dd($info);
}

微信 Wap - H5 支付

<?php

require __DIR__ . '/../vendor/autoload.php';

$config = require __DIR__ . '/config.php';
$config = $config['wechat'];
$action = $_REQUEST['action'] ?? 'pay';

$wechat = new \Nilnice\Payment\Wechat($config);

if ($action === 'pay') {
    $order = [
        'out_trade_no' => time(),
        'total_fee'    => 1,
        'body'         => '微信支付-测试订单',
    ];

    /** @var \Symfony\Component\HttpFoundation\Response $object */
    $object = $wechat->wap($order);
    $object->send();
} elseif ($action === 'query') {
    $order = 'order_id';
    $info = $wechat->query($order);
    dd($info);
} elseif ($action === 'close') {
    $order = 'order_id';
    $info = $wechat->close($order);
    dd($info);
} elseif ($action === 'cancel') {
    $order = 'order_id';
    dd($wechat->cancel($order));
} elseif ($action === 'refund') {
    $order = 'order_id';
    dd($wechat->refund($order));
}

微信 Scan 支付

<?php

require __DIR__ . '/../vendor/autoload.php';

$config = require __DIR__ . '/config.php';
$config = $config['wechat'];
$action = $_REQUEST['action'] ?? 'pay';

$wechat = new \Nilnice\Payment\Wechat($config);

if ($action === 'pay') {
    $order = [
        'out_trade_no' => time(),
        'total_fee'    => 1,
        'body'         => '微信支付-测试订单',
    ];

    /** @var \Illuminate\Support\Collection $object */
    $object = $wechat->scan($order);

    if ($object->get('return_code') === 'SUCCESS'
        || $object->get('return_msg') === 'OK'
    ) {
        $labelFontPath
            = '/../vendor/endroid/qr-code/assets/fonts/noto_sans.otf';
        $logoPath = '/../vendor/endroid/qr-code/assets/images/symfony.png';

        $qrCode = new \Endroid\QrCode\QrCode($object->get('code_url'));
        $qrCode->setSize(300);

        $qrCode->setWriterByName('png');
        $qrCode->setMargin(10);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(\Endroid\QrCode\ErrorCorrectionLevel::HIGH);
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor([
            'r' => 255,
            'g' => 255,
            'b' => 255,
            'a' => 0,
        ]);
        $qrCode->setLabel(
            '请打开微信扫描二维码',
            16,
            __DIR__ . $labelFontPath,
            \Endroid\QrCode\LabelAlignment::CENTER
        );
        $qrCode->setLogoPath(__DIR__ . $logoPath);
        $qrCode->setLogoWidth(150);
        $qrCode->setRoundBlockSize(true);
        $qrCode->setValidateResult(false);

        $response = new \Endroid\QrCode\Response\QrCodeResponse($qrCode);
        $response->send();
    }
} elseif ($action === 'query') {
    $order = 'order_id';
    $info = $wechat->query($order);
    dd($info);
} elseif ($action === 'close') {
    $order = 'order_id';
    $info = $wechat->close($order);
    dd($info);
} elseif ($action === 'cancel') {
    $order = 'order_id';
    $info = $wechat->cancel($order);
    dd($info);
} elseif ($action === 'refund') {
    $order = 'order_id';
    $info = $wechat->refund($order);
    dd($info);
}

微信 Pub 支付

<?php

require __DIR__ . '/../vendor/autoload.php';

$config = require __DIR__ . '/config.php';
$config = $config['wechat'];
$action = $_REQUEST['action'] ?? 'pay';

$wechat = new \Nilnice\Payment\Wechat($config);

if ($action === 'pay') {
    $order = [
        'out_trade_no' => time(),
        'total_fee'    => 1,
        'body'         => '微信支付-测试订单',
        'openid'       => '',
    ];

    /** @var \Illuminate\Support\Collection $object */
    $object = $wechat->pub($order);
    $jsApiParameter = json_encode($object->all());
} elseif ($action === 'query') {
    $order = 'order_id';
    $info = $wechat->query($order);
    dd($info);
} elseif ($action === 'close') {
    $order = 'order_id';
    $info = $wechat->close($order);
    dd($info);
} elseif ($action === 'cancel') {
    $order = 'order_id';
    dd($wechat->cancel($order));
} elseif ($action === 'refund') {
    $order = [
        'out_trade_no'  => 'order_id',
        'out_refund_no' => time(),
        'total_fee'     => 1,
        'refund_fee'    => 1,
        'refund_desc'   => '测试退款',
    ];
    dd($wechat->refund($order));
}

?>

<?php if ($action === 'pay'): ?>
  <!doctype html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>微信安全支付</title>
    <style>
      .container {
        display: flex;
        width: 100%;
        height: 100%;
        margin-top: 10%;
      }

      .btn {
        display: inline-block;
        font-weight: 400;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        border: 1px solid transparent;
        padding: .375rem .75rem;
        font-size: 1rem;
        line-height: 1.5;
        border-radius: .25rem;
        transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
      }

      .btn-success {
        color: #fff;
        background-color: #28a745;
        border-color: #28a745;
      }

      .btn:not(:disabled):not(.disabled) {
        cursor: pointer;
      }

      .btn-location {
        margin: auto;
      }
    </style>
  </head>
  <body>
  <div class="container">
    <button class="btn btn-success btn-location" type="button"
            onclick="goPayment()">
      来点击试试
    </button>
  </div>
  </body>
  <script type="text/javascript">
    function jsApiPayment() {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
          <?php echo $jsApiParameter; ?>,
        function (response) {
          var msg = response.err_msg;

          if (typeof msg === 'undefined') {
            alert('支付异常,请稍候再试');
            return false;
          }
          switch (msg.trim()) {
            case 'get_brand_wcpay_request:ok':
              alert('支付成功');
              break;
            case 'get_brand_wcpay_request:fail':
              alert('支付失败 『' + response.err_desc + '』' + JSON.stringify(response));
              break;
            case 'get_brand_wcpay_request:cancel':
              alert('用户取消支付');
              break;
            default:
              alert(JSON.stringify(response));
              break;
          }
        }
      );
    }

    function goPayment() {
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', jsApiPayment, false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', jsApiPayment);
          document.attachEvent('onWeixinJSBridgeReady', jsApiPayment);
        }
      } else {
        jsApiPayment();
      }
    }
  </script>
  </html>
<?php endif; ?>
Clone this wiki locally