GatewayController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // Copyright 2019 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\Http\Controllers;
  8. use Hawese\Core\Http\Controllers\Controller;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Response;
  12. class GatewayController extends Controller
  13. {
  14. public function index()
  15. {
  16. $gateways = config('gateways');
  17. $schemas = [];
  18. foreach (array_keys($gateways) as $gatewayKey) {
  19. $schemasKeys = array_keys($gateways[$gatewayKey]['schemas']);
  20. foreach ($schemasKeys as $schemaKey) {
  21. $schemas[$gatewayKey][$schemaKey] = route('gateways.schema', [
  22. 'gateway' => $gatewayKey,
  23. 'schema' => $schemaKey,
  24. ]);
  25. }
  26. }
  27. return ['schemas' => $schemas];
  28. }
  29. public function paymentMethods(Request $request, $schema)
  30. {
  31. $gateways = config('gateways');
  32. $paymentMethods = [];
  33. foreach (array_keys($gateways) as $gatewayKey) {
  34. $property = Arr::get(
  35. $gateways,
  36. "$gatewayKey.payment_methods.$schema"
  37. );
  38. $gatewayPaymentMethods = Arr::get(
  39. $gateways,
  40. "$gatewayKey.schemas.$schema.properties.$property.oneOf"
  41. );
  42. array_walk(
  43. $gatewayPaymentMethods,
  44. function (&$paymentMethod) use ($gatewayKey, $property) {
  45. $paymentMethod['gateway'] = $gatewayKey;
  46. $paymentMethod['property'] = $property;
  47. }
  48. );
  49. $paymentMethods = array_merge(
  50. $paymentMethods,
  51. $gatewayPaymentMethods
  52. );
  53. }
  54. return $paymentMethods;
  55. }
  56. public function schema(Request $request, $gateway, $schema)
  57. {
  58. return array_merge([
  59. '$schema' => 'http://json-schema.org/schema#',
  60. '$id' => route('gateways.schema', [
  61. 'gateway' => $gateway,
  62. 'schema' => $schema,
  63. ]),
  64. 'type' => 'object',
  65. ], config("gateways.$gateway.schemas.$schema"));
  66. }
  67. /**
  68. * Receive notification from payment gateway, process and notify to other
  69. * custom endpoint.
  70. * @return mixed generally void.
  71. */
  72. public function notify(Request $request, $gateway)
  73. {
  74. $gatewayClass = config("gateways.$gateway.class");
  75. $gateway = new $gatewayClass();
  76. // This most probably will not return a response because
  77. // it's a method returned to IPN requests, returning status
  78. // OK is enough for most gateways
  79. return $gateway->acceptNotification($request->all());
  80. }
  81. /**
  82. * @return ['redirect_url' => $url] that's OK for current use cases,
  83. * but probably will change in the future.
  84. */
  85. public function purchase(Request $request, $gateway): array
  86. {
  87. $gatewayClass = config("gateways.$gateway.class");
  88. $gateway = new $gatewayClass();
  89. return $gateway->purchase($request->all());
  90. }
  91. }