AbstractGateway.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // Copyright 2019-2022 Hackware SpA <human@hackware.cl>
  3. // This file is part of "Hackware Web Services Payment" and licensed under
  4. // the terms of the GNU Affero General Public License version 3, or (at your
  5. // option) a later version. You should have received a copy of this license
  6. // along with the software. If not, see <https://www.gnu.org/licenses/>.
  7. namespace Hawese\Payment\Gateways;
  8. use Buzz\Client\FileGetContents as HttpClient;
  9. use Nyholm\Psr7\Factory\Psr17Factory as Psr17ResponseFactory;
  10. use Psr\Http\Message\ResponseInterface;
  11. abstract class AbstractGateway
  12. {
  13. protected $psr17Factory;
  14. protected $httpClient;
  15. public function __construct()
  16. {
  17. $this->psr17Factory = new Psr17ResponseFactory();
  18. $this->httpClient = new HttpClient($this->psr17Factory);
  19. }
  20. public function request(
  21. string $method,
  22. string $uri,
  23. $bodyParams = null,
  24. array $headers = []
  25. ): ResponseInterface {
  26. $builderName = static::requestBuilderName();
  27. $requestBuilder = new $builderName(
  28. $method,
  29. $uri,
  30. $bodyParams,
  31. $headers
  32. );
  33. return $this->httpClient->sendRequest($requestBuilder->build());
  34. }
  35. protected static function requestBuilderName(): string
  36. {
  37. return static::name() . 'RequestBuilder';
  38. }
  39. protected static function name(): string
  40. {
  41. return substr(static::class, 0, -7);
  42. }
  43. public static function schema(string $schema, array $defaults = []): array
  44. {
  45. $name = strtolower(
  46. substr(static::name(), strrpos(static::name(), '\\') + 1)
  47. );
  48. $schema = array_merge([
  49. '$schema' => 'http://json-schema.org/schema#',
  50. '$id' => route('gateways.schema', [
  51. 'gateway' => $name,
  52. 'schema' => $schema,
  53. ]),
  54. 'type' => 'object',
  55. ], config("gateways.$name.schemas.$schema"));
  56. foreach ($schema['standard_map'] as $prop_input => $prop_schema) {
  57. $property = &static::schemaProperty(
  58. $schema,
  59. $prop_schema
  60. );
  61. if (array_key_exists($prop_input, $defaults)) {
  62. $property['default'] = static::schemaTypeCast(
  63. $property['type'],
  64. $defaults[$prop_input]
  65. );
  66. if ($prop_input == 'email') {
  67. $property['readOnly'] = true;
  68. }
  69. }
  70. }
  71. return $schema;
  72. }
  73. protected static function schemaTypeCast(string $type, $value)
  74. {
  75. switch ($type) {
  76. case 'number':
  77. case 'integer':
  78. return $value * 1;
  79. break;
  80. default:
  81. return $value;
  82. }
  83. }
  84. protected static function &schemaProperty(array &$schema, $property)
  85. {
  86. if (is_array($property)) {
  87. $key = array_key_first($property);
  88. return static::schemaProperty(
  89. $schema['properties'][$key],
  90. $property[$key]
  91. );
  92. }
  93. return $schema['properties'][$property];
  94. }
  95. }