123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- // Copyright 2019-2022 Hackware SpA <human@hackware.cl>
- // This file is part of "Hackware Web Services Payment" and licensed under
- // the terms of the GNU Affero General Public License version 3, or (at your
- // option) a later version. You should have received a copy of this license
- // along with the software. If not, see <https://www.gnu.org/licenses/>.
- namespace Hawese\Payment\Gateways;
- use Hawese\Payment\Payment;
- use Hawese\Payment\Exceptions\UnexpectedResponseException;
- use Hawese\Payment\Support\Http as HttpSupport;
- use Hawese\Core\User;
- use Brick\Money\Money;
- use Illuminate\Support\Str;
- use Psr\Http\Message\ResponseInterface;
- class FlowGateway extends AbstractGateway
- {
- public function purchase(
- array $bodyParams,
- string $origin = 'wallet'
- ): array {
- $bodyParams = $this->purchaseAddDefaultParams($bodyParams, $origin);
- $response = $this->request('POST', '/payment/create', $bodyParams);
- $responseBody = $this->purchaseProcessResponse($response);
- $payment_uuid = $this->purchaseInsertPayment(
- $bodyParams,
- $responseBody,
- $origin
- );
- $redirectUrl = $responseBody->url . '?token=' . $responseBody->token;
- return ['uuid' => $payment_uuid, 'redirect_url' => $redirectUrl];
- }
- private function purchaseAddDefaultParams(
- array $bodyParams,
- string $origin
- ): array {
- $uuid = (string) Str::uuid();
- return array_merge($bodyParams, [
- 'urlReturn' => HttpSupport::appendQueryParams(
- config("payment.origins.$origin.return_url"),
- ['uuid' => $uuid]
- ),
- 'urlConfirmation' => url(route(
- 'gateways.notify',
- ['gateway' => 'flow']
- )),
- 'commerceOrder' => $uuid,
- ]);
- }
- /**
- * Validate and process $response
- * @returns object parsed body
- */
- private function purchaseProcessResponse(
- ResponseInterface &$response
- ): object {
- if ($response->getStatusCode() == 200) {
- $responseBody = HttpSupport::getJsonBody($response);
- if (property_exists($responseBody, 'token')) {
- return $responseBody;
- }
- }
- throw new UnexpectedResponseException($response);
- }
- /**
- * @returns string Payment primary key value
- */
- private function purchaseInsertPayment(
- array &$bodyParams,
- object &$responseBody,
- string &$origin
- ): string {
- return (new Payment([
- 'uuid' => $bodyParams['commerceOrder'],
- 'user_uid' => User::findByEmailOrCreate($bodyParams['email'])->uid,
- 'gateway' => 'flow',
- 'currency' => $bodyParams['currency'] ?? 'CLP',
- 'amount' => $bodyParams['amount'], // Integers only!!
- 'description' => $bodyParams['subject'],
- 'detail' => [
- 'flowOrder' => $responseBody->flowOrder,
- 'paymentMethod' => $bodyParams['paymentMethod'],
- ],
- 'status' => Payment::STATUS_PENDING,
- 'origin' => $origin,
- ]))->insert();
- }
- public function acceptNotification(array $bodyParams)
- {
- $queryParams = ['token' => $bodyParams['token']];
- $response = $this->request(
- 'GET',
- '/payment/getStatus?' . HttpSupport::buildUriQuery($queryParams)
- );
- $responseBody = HttpSupport::getJsonBody($response);
- $payment = Payment::find($responseBody->commerceOrder);
- if (
- Money::of($responseBody->amount, $responseBody->currency)
- ->isAmountAndCurrencyEqualTo($payment->amountAsMoney)
- ) {
- if ($payment->status === Payment::STATUS_COMPLETED) {
- return response('Already processed', 400);
- } else {
- // 1 pendiente de pago, 2 pagada, 3 rechazada, 4 anulada
- $payment->validateAndUpdateStatus($responseBody->status, [2], [1]);
- return response('OK, Flow', 200);
- }
- }
- return response('Incorrect amount', 400);
- }
- }
|