Pay.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Yansongda\Pay;
  3. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  4. use Yansongda\Pay\Support\Config;
  5. class Pay
  6. {
  7. /**
  8. * @var \Yansongda\Pay\Support\Config
  9. */
  10. private $config;
  11. /**
  12. * @var string
  13. */
  14. private $drivers;
  15. /**
  16. * @var \Yansongda\Pay\Contracts\GatewayInterface
  17. */
  18. private $gateways;
  19. /**
  20. * construct method.
  21. *
  22. * @author JasonYan <me@yansongda.cn>
  23. *
  24. * @param array $config
  25. */
  26. public function __construct(array $config = [])
  27. {
  28. $this->config = new Config($config);
  29. }
  30. /**
  31. * set pay's driver.
  32. *
  33. * @author JasonYan <me@yansongda.cn>
  34. *
  35. * @param string $driver
  36. *
  37. * @return Pay
  38. */
  39. public function driver($driver)
  40. {
  41. if (is_null($this->config->get($driver))) {
  42. throw new InvalidArgumentException("Driver [$driver]'s Config is not defined.");
  43. }
  44. $this->drivers = $driver;
  45. return $this;
  46. }
  47. /**
  48. * set pay's gateway.
  49. *
  50. * @author yansongda <me@yansongda.cn>
  51. *
  52. * @param string $gateway
  53. *
  54. * @return \Yansongda\Pay\Contracts\GatewayInterface
  55. */
  56. public function gateway($gateway = 'web')
  57. {
  58. if (!isset($this->drivers)) {
  59. throw new InvalidArgumentException('Driver is not defined.');
  60. }
  61. $this->gateways = $this->createGateway($gateway);
  62. return $this->gateways;
  63. }
  64. /**
  65. * create pay's gateway.
  66. *
  67. * @author yansongda <me@yansongda.cn>
  68. *
  69. * @param string $gateway
  70. *
  71. * @return \Yansongda\Pay\Contracts\GatewayInterface
  72. */
  73. protected function createGateway($gateway)
  74. {
  75. if (!file_exists(__DIR__ . '/Gateways/' . ucfirst($this->drivers) . '/' . ucfirst($gateway) . 'Gateway.php')) {
  76. throw new InvalidArgumentException("Gateway [$gateway] is not supported.");
  77. }
  78. $gateway = __NAMESPACE__ . '\\Gateways\\' . ucfirst($this->drivers) . '\\' . ucfirst($gateway) . 'Gateway';
  79. return $this->build($gateway);
  80. }
  81. /**
  82. * build pay's gateway.
  83. *
  84. * @author JasonYan <me@yansongda.cn>
  85. *
  86. * @param string $gateway
  87. *
  88. * @return \Yansongda\Pay\Contracts\GatewayInterface
  89. */
  90. protected function build($gateway)
  91. {
  92. return new $gateway($this->config->get($this->drivers));
  93. }
  94. public function verify()
  95. {
  96. if ($this->drivers == 'wechat') {
  97. return $this->gateway()->verify(file_get_contents("php://input"));
  98. } else {
  99. $request = request();
  100. $data = $request->get('app_id') && $request->get('out_trade_no') ? $request->get('', null, 'trim') : $request->post('', null, 'trim');
  101. return $this->gateway()->verify($data);
  102. }
  103. }
  104. public function success()
  105. {
  106. if ($this->drivers == 'wechat') {
  107. echo '<xml>
  108. <return_code><![CDATA[SUCCESS]]></return_code>
  109. <return_msg><![CDATA[OK]]></return_msg>
  110. </xml>';
  111. } else {
  112. echo 'success';
  113. }
  114. return;
  115. }
  116. }