FlowGateway.php 3.5 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\Gateways;
  8. use Hawese\Payment\Payment;
  9. use Hawese\Payment\Exceptions\UnexpectedResponseException;
  10. use Hawese\Payment\Support\Http as HttpSupport;
  11. use Illuminate\Support\Str;
  12. use Psr\Http\Message\ResponseInterface;
  13. class FlowGateway extends AbstractGateway
  14. {
  15. public function purchase(array $bodyParams): array
  16. {
  17. $bodyParams = $this->purchaseAddDefaultParams($bodyParams);
  18. $response = $this->request('POST', '/payment/create', $bodyParams);
  19. $responseBody = $this->purchaseProcessResponse($response);
  20. $this->purchaseInsertPayment($bodyParams, $responseBody);
  21. $redirectUrl = $responseBody->url . '?token=' . $responseBody->token;
  22. return ['redirect_url' => $redirectUrl];
  23. }
  24. private function purchaseAddDefaultParams(array $bodyParams): array
  25. {
  26. $uuid = (string) Str::uuid();
  27. return array_merge($bodyParams, [
  28. 'urlReturn' => HttpSupport::appendQueryParams(
  29. config('payment.return_url'),
  30. ['uuid' => $uuid]
  31. ),
  32. 'urlConfirmation' => url(route(
  33. 'gateways.notify',
  34. ['gateway' => 'flow']
  35. )),
  36. 'commerceOrder' => $uuid,
  37. ]);
  38. }
  39. /**
  40. * Validate and process $response
  41. * @returns object parsed body
  42. */
  43. private function purchaseProcessResponse(
  44. ResponseInterface &$response
  45. ): object {
  46. if ($response->getStatusCode() == 200) {
  47. $responseBody = HttpSupport::getJsonBody($response);
  48. if (property_exists($responseBody, 'token')) {
  49. return $responseBody;
  50. }
  51. }
  52. throw new UnexpectedResponseException($response);
  53. }
  54. /**
  55. * @returns string Payment primary key value
  56. */
  57. private function purchaseInsertPayment(
  58. array &$bodyParams,
  59. object &$responseBody
  60. ): string {
  61. return (new Payment([
  62. 'uuid' => $bodyParams['commerceOrder'],
  63. 'user_uid' => $bodyParams['email'],
  64. 'gateway' => 'flow',
  65. 'currency' => $bodyParams['currency'] ?? 'CLP',
  66. 'amount' => $bodyParams['amount'], // Integers only!!
  67. 'description' => $bodyParams['subject'],
  68. 'detail' => json_encode([
  69. 'flowOrder' => $responseBody->flowOrder,
  70. 'paymentMethod' => $bodyParams['paymentMethod'],
  71. ]),
  72. 'status' => Payment::STATUS_PENDING,
  73. ]))->insert();
  74. }
  75. public function acceptNotification(array $bodyParams)
  76. {
  77. $queryParams = ['token' => $bodyParams['token']];
  78. $response = $this->request(
  79. 'GET',
  80. '/payment/getStatus?' . HttpSupport::buildUriQuery($queryParams)
  81. );
  82. $responseBody = HttpSupport::getJsonBody($response);
  83. $payment = Payment::find($responseBody->commerceOrder);
  84. if ($payment->status === Payment::STATUS_COMPLETED) {
  85. return response('Already processed', 400);
  86. } else {
  87. // 1 pendiente de pago, 2 pagada, 3 rechazada, 4 anulada
  88. $payment->validateAndUpdateStatus($responseBody->status, [2], [1]);
  89. }
  90. }
  91. }