appCharge.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. $wxConfig = require_once __DIR__ . '/../wxconfig.php';
  13. $tradeNo = time() . rand(1000, 9999);
  14. // 订单信息
  15. $payData = [
  16. 'body' => 'test body',
  17. 'subject' => 'test subject',
  18. 'trade_no' => $tradeNo,
  19. 'time_expire' => time() + 600, // 表示必须 600s 内付款
  20. 'amount' => '5.52', // 微信沙箱模式,需要金额固定为3.01
  21. 'return_param' => '123',
  22. 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', // 客户地址
  23. ];
  24. // 使用
  25. try {
  26. $client = new \Payment\Client(\Payment\Client::WECHAT, $wxConfig);
  27. $res = $client->pay(\Payment\Client::WX_CHANNEL_APP, $payData);
  28. } catch (InvalidArgumentException $e) {
  29. echo $e->getMessage();
  30. exit;
  31. } catch (\Payment\Exceptions\GatewayException $e) {
  32. echo $e->getMessage();
  33. var_dump($e->getRaw());
  34. exit;
  35. } catch (\Payment\Exceptions\ClassNotFoundException $e) {
  36. echo $e->getMessage();
  37. exit;
  38. } catch (Exception $e) {
  39. echo $e->getMessage();
  40. exit;
  41. }
  42. var_dump($res);