123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // Copyright 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 DLocalGoGateway extends AbstractGateway
- {
- public function purchase(
- array $bodyParams,
- string $origin = 'wallet'
- ): array {
- $bodyParams = $this->purchaseAddDefaultParams($bodyParams, $origin);
- $response = $this->request('POST', '/v1/payments', $bodyParams);
- $responseBody = $this->purchaseProcessResponse($response);
- $paymentUuid = $this->purchaseInsertPayment(
- $bodyParams,
- $responseBody,
- $origin
- );
- return [
- 'uuid' => $paymentUuid,
- 'redirect_url' => $responseBody->redirect_url
- ];
- }
- private function purchaseAddDefaultParams(
- array $bodyParams,
- string $origin
- ): array {
- $uuid = (string) Str::uuid();
- $returnUrl = HttpSupport::appendQueryParams(
- config("payment.origins.$origin.return_url"),
- ['uuid' => $uuid]
- );
- return array_merge($bodyParams, [
- 'order_id' => $uuid,
- 'payer' => ['email' =>
- User::findByEmailOrCreate($bodyParams['payer']['email'])->email],
- 'success_url' => $returnUrl,
- 'back_url' => $returnUrl,
- 'notification_url' => url(route(
- 'gateways.notify',
- ['gateway' => 'dlocalgo']
- )),
- ]);
- }
- /**
- * 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, 'id')) {
- 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['order_id'],
- 'user_uid' => $bodyParams['payer']['email'],
- 'gateway' => 'dlocalgo',
- 'currency' => $bodyParams['currency'] ?? 'USD',
- 'amount' => $bodyParams['amount'],
- 'description' => $bodyParams['description'],
- 'detail' => [
- 'id' => $responseBody->id,
- 'country' => $responseBody->country,
- ],
- 'status' => Payment::STATUS_PENDING,
- 'origin' => $origin,
- ]))->insert();
- }
- public function acceptNotification(array $bodyParams)
- {
- $response = $this->request(
- 'GET',
- '/v1/payments/' . $bodyParams['payment_id']
- );
- $responseBody = HttpSupport::getJsonBody($response);
- $payment = Payment::find($responseBody->order_id);
- if (
- Money::of($responseBody->amount, $responseBody->currency)
- ->isAmountAndCurrencyEqualTo($payment->amountAsMoney)
- ) {
- if ($payment->status === Payment::STATUS_COMPLETED) {
- return response('Already processed', 400);
- } else {
- // REJECTED CANCELLED or EXPIRED set Payment::STATUS_ABORTED.
- $payment->validateAndUpdateStatus(
- $responseBody->status,
- ['PAID'],
- ['PENDING']
- );
- return response('All right DLocalGo!', 200);
- }
- }
- return response('Incorrect amount', 400);
- }
- }
|