pubCharge.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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' => '3.01', // 微信沙箱模式,需要金额固定为3.01
  21. 'return_param' => '123',
  22. 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', // 客户地址
  23. 'openid' => 'ottkCuO1PW1Dnh6PWFffNk-2MPbY',
  24. 'product_id' => '123',
  25. // 如果是服务商,请提供以下参数
  26. 'sub_appid' => '', //微信分配的子商户公众账号ID
  27. 'sub_mch_id' => '', // 微信支付分配的子商户号
  28. 'sub_openid' => '', // 用户在子商户appid下的唯一标识
  29. ];
  30. // 使用
  31. try {
  32. $client = new \Payment\Client(\Payment\Client::WECHAT, $wxConfig);
  33. $res = $client->pay(\Payment\Client::WX_CHANNEL_PUB, $payData);
  34. } catch (InvalidArgumentException $e) {
  35. echo $e->getMessage();
  36. exit;
  37. } catch (\Payment\Exceptions\GatewayException $e) {
  38. echo $e->getMessage();
  39. exit;
  40. } catch (\Payment\Exceptions\ClassNotFoundException $e) {
  41. echo $e->getMessage();
  42. exit;
  43. }
  44. var_dump($res);