qrCharge.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * The file is part of the payment lib.
  4. *
  5. * (c) Leo <dayugog@gmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. require_once __DIR__ . '/../../vendor/autoload.php';
  11. date_default_timezone_set('Asia/Shanghai');
  12. $aliConfig = require_once __DIR__ . '/../aliconfig.php';
  13. // 交易信息
  14. $tradeNo = time() . rand(1000, 9999);
  15. $payData = [
  16. 'body' => 'ali qr pay',
  17. 'subject' => '测试支付宝扫码支付',
  18. 'trade_no' => $tradeNo,
  19. 'time_expire' => time() + 1000, // 表示必须 1000s 内付款
  20. 'amount' => '0.01', // 单位为元 ,最小为0.01
  21. 'return_param' => '123123',
  22. // 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',// 客户地址
  23. 'goods_type' => '1', // 0—虚拟类商品,1—实物类商品
  24. 'store_id' => '',
  25. 'operator_id' => '',
  26. 'terminal_id' => '', // 终端设备号(门店号或收银设备ID) 默认值 web
  27. ];
  28. // 使用
  29. try {
  30. $client = new \Payment\Client(\Payment\Client::ALIPAY, $aliConfig);
  31. $res = $client->pay(\Payment\Client::ALI_CHANNEL_QR, $payData);
  32. } catch (InvalidArgumentException $e) {
  33. echo $e->getMessage();
  34. exit;
  35. } catch (\Payment\Exceptions\GatewayException $e) {
  36. echo $e->getMessage();
  37. var_dump($e->getRaw());
  38. exit;
  39. } catch (\Payment\Exceptions\ClassNotFoundException $e) {
  40. echo $e->getMessage();
  41. exit;
  42. } catch (Exception $e) {
  43. echo $e->getMessage();
  44. exit;
  45. }
  46. echo \Payment\Helpers\StrUtil::toQRImg($res['qr_code']);