Gateway.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Wechat;
  3. use Yansongda\Pay\Contracts\GatewayInterface;
  4. use Yansongda\Pay\Events;
  5. use Yansongda\Pay\Exceptions\GatewayException;
  6. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  7. use Yansongda\Pay\Exceptions\InvalidSignException;
  8. use Yansongda\Supports\Collection;
  9. abstract class Gateway implements GatewayInterface
  10. {
  11. /**
  12. * Mode.
  13. *
  14. * @var string
  15. */
  16. protected $mode;
  17. /**
  18. * Bootstrap.
  19. *
  20. * @author yansongda <me@yansongda.cn>
  21. *
  22. * @throws InvalidArgumentException
  23. */
  24. public function __construct()
  25. {
  26. $this->mode = Support::getInstance()->mode;
  27. }
  28. /**
  29. * Pay an order.
  30. *
  31. * @author yansongda <me@yansongda.cn>
  32. *
  33. * @param string $endpoint
  34. *
  35. * @return Collection
  36. */
  37. abstract public function pay($endpoint, array $payload);
  38. /**
  39. * Find.
  40. *
  41. * @author yansongda <me@yansongda.cn>
  42. *
  43. * @param string|array $order
  44. */
  45. public function find($order): array
  46. {
  47. return [
  48. 'endpoint' => 'pay/orderquery',
  49. 'order' => is_array($order) ? $order : ['out_trade_no' => $order],
  50. 'cert' => false,
  51. ];
  52. }
  53. /**
  54. * Get trade type config.
  55. *
  56. * @author yansongda <me@yansongda.cn>
  57. *
  58. * @return string
  59. */
  60. abstract protected function getTradeType();
  61. /**
  62. * Schedule an order.
  63. *
  64. * @author yansongda <me@yansongda.cn>
  65. *
  66. * @param array $payload
  67. *
  68. * @throws GatewayException
  69. * @throws InvalidArgumentException
  70. * @throws InvalidSignException
  71. */
  72. protected function preOrder($payload): Collection
  73. {
  74. $payload['sign'] = Support::generateSign($payload);
  75. Events::dispatch(new Events\MethodCalled('Wechat', 'PreOrder', '', $payload));
  76. return Support::requestApi('pay/unifiedorder', $payload);
  77. }
  78. }