Pay.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay;
  4. use Closure;
  5. use Psr\Container\ContainerInterface;
  6. use Yansongda\Artful\Artful;
  7. use Yansongda\Artful\Exception\ContainerException;
  8. use Yansongda\Artful\Exception\ServiceNotFoundException;
  9. use Yansongda\Pay\Provider\Alipay;
  10. use Yansongda\Pay\Provider\Douyin;
  11. use Yansongda\Pay\Provider\Jsb;
  12. use Yansongda\Pay\Provider\Unipay;
  13. use Yansongda\Pay\Provider\Wechat;
  14. use Yansongda\Pay\Service\AlipayServiceProvider;
  15. use Yansongda\Pay\Service\DouyinServiceProvider;
  16. use Yansongda\Pay\Service\JsbServiceProvider;
  17. use Yansongda\Pay\Service\UnipayServiceProvider;
  18. use Yansongda\Pay\Service\WechatServiceProvider;
  19. /**
  20. * @method static Alipay alipay(array $config = [], $container = null)
  21. * @method static Wechat wechat(array $config = [], $container = null)
  22. * @method static Unipay unipay(array $config = [], $container = null)
  23. * @method static Jsb jsb(array $config = [], $container = null)
  24. * @method static Douyin douyin(array $config = [], $container = null)
  25. */
  26. class Pay
  27. {
  28. /**
  29. * 正常模式.
  30. */
  31. public const MODE_NORMAL = 0;
  32. /**
  33. * 沙箱模式.
  34. */
  35. public const MODE_SANDBOX = 1;
  36. /**
  37. * 服务商模式.
  38. */
  39. public const MODE_SERVICE = 2;
  40. protected static array $providers = [
  41. AlipayServiceProvider::class,
  42. WechatServiceProvider::class,
  43. UnipayServiceProvider::class,
  44. JsbServiceProvider::class,
  45. DouyinServiceProvider::class,
  46. ];
  47. /**
  48. * @throws ContainerException
  49. * @throws ServiceNotFoundException
  50. */
  51. public static function __callStatic(string $service, array $config = [])
  52. {
  53. if (!empty($config)) {
  54. self::config(...$config);
  55. }
  56. return Artful::get($service);
  57. }
  58. /**
  59. * @throws ContainerException
  60. */
  61. public static function config(array $config = [], null|Closure|ContainerInterface $container = null): bool
  62. {
  63. $result = Artful::config($config, $container);
  64. foreach (self::$providers as $provider) {
  65. Artful::load($provider);
  66. }
  67. return $result;
  68. }
  69. /**
  70. * @throws ContainerException
  71. */
  72. public static function set(string $name, mixed $value): void
  73. {
  74. Artful::set($name, $value);
  75. }
  76. /**
  77. * @throws ContainerException
  78. * @throws ServiceNotFoundException
  79. */
  80. public static function get(string $service): mixed
  81. {
  82. return Artful::get($service);
  83. }
  84. public static function setContainer(null|Closure|ContainerInterface $container): void
  85. {
  86. Artful::setContainer($container);
  87. }
  88. public static function clear(): void
  89. {
  90. Artful::clear();
  91. }
  92. }