Pay.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Yansongda\Pay;
  3. use Exception;
  4. use Yansongda\Pay\Contracts\GatewayApplicationInterface;
  5. use Yansongda\Pay\Exceptions\InvalidGatewayException;
  6. use Yansongda\Pay\Gateways\Alipay;
  7. use Yansongda\Pay\Gateways\Wechat;
  8. use Yansongda\Pay\Listeners\KernelLogSubscriber;
  9. use Yansongda\Supports\Config;
  10. use Yansongda\Supports\Log;
  11. use Yansongda\Supports\Logger;
  12. use Yansongda\Supports\Str;
  13. /**
  14. * @method static Alipay alipay(array $config) 支付宝
  15. * @method static Wechat wechat(array $config) 微信
  16. */
  17. class Pay
  18. {
  19. /**
  20. * Config.
  21. *
  22. * @var Config
  23. */
  24. protected $config;
  25. /**
  26. * Bootstrap.
  27. *
  28. * @author yansongda <me@yansongda.cn>
  29. *
  30. * @throws Exception
  31. */
  32. public function __construct(array $config)
  33. {
  34. $this->config = new Config($config);
  35. $this->registerLogService();
  36. $this->registerEventService();
  37. }
  38. /**
  39. * Magic static call.
  40. *
  41. * @author yansongda <me@yansongda.cn>
  42. *
  43. * @param string $method
  44. * @param array $params
  45. *
  46. * @throws InvalidGatewayException
  47. * @throws Exception
  48. */
  49. public static function __callStatic($method, $params): GatewayApplicationInterface
  50. {
  51. $app = new self(...$params);
  52. return $app->create($method);
  53. }
  54. /**
  55. * Create a instance.
  56. *
  57. * @author yansongda <me@yansongda.cn>
  58. *
  59. * @param string $method
  60. *
  61. * @throws InvalidGatewayException
  62. */
  63. protected function create($method): GatewayApplicationInterface
  64. {
  65. $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);
  66. if (class_exists($gateway)) {
  67. return self::make($gateway);
  68. }
  69. throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
  70. }
  71. /**
  72. * Make a gateway.
  73. *
  74. * @author yansongda <me@yansonga.cn>
  75. *
  76. * @param string $gateway
  77. *
  78. * @throws InvalidGatewayException
  79. */
  80. protected function make($gateway): GatewayApplicationInterface
  81. {
  82. $app = new $gateway($this->config);
  83. if ($app instanceof GatewayApplicationInterface) {
  84. return $app;
  85. }
  86. throw new InvalidGatewayException("Gateway [{$gateway}] Must Be An Instance Of GatewayApplicationInterface");
  87. }
  88. /**
  89. * Register log service.
  90. *
  91. * @author yansongda <me@yansongda.cn>
  92. *
  93. * @throws Exception
  94. */
  95. protected function registerLogService()
  96. {
  97. $config = $this->config->get('log');
  98. $config['identify'] = 'yansongda.pay';
  99. $logger = new Logger();
  100. $logger->setConfig($config);
  101. Log::setInstance($logger);
  102. }
  103. /**
  104. * Register event service.
  105. *
  106. * @author yansongda <me@yansongda.cn>
  107. */
  108. protected function registerEventService()
  109. {
  110. Events::setDispatcher(Events::createDispatcher());
  111. Events::addSubscriber(new KernelLogSubscriber());
  112. }
  113. }