paymentservice/app/Http/Controllers/YoomeeController.php

169 lines
6.3 KiB
PHP
Raw Normal View History

2022-08-11 09:46:51 +00:00
<?php
namespace App\Http\Controllers;
use App\Enums\PaymentTransactionState;
use App\Models\PaymentTransaction;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;
2022-10-08 21:12:51 +00:00
use Illuminate\Support\Str;
2022-08-11 09:46:51 +00:00
class YoomeeController extends Controller
{
private $client;
private $timeout = 60; //In seconds
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
// Create a client with a base URI
$this->client = new Client([
'base_uri' => config('variables.yoomee_api_url')
]);
}
/**
* @OA\Get(
2022-10-08 21:12:51 +00:00
* path="/yoomee/methods",
* summary="Afficher la liste des methodes de paiements de Yoomee",
2022-08-11 09:46:51 +00:00
* tags={"Yoomee"},
* security={{"api_key":{}}},
* @OA\Response(
* response=200,
* description="OK",
* @OA\JsonContent(
* ref="#/components/schemas/ApiResponse",
* example = {
* "status" : 200,
2022-11-21 22:29:50 +00:00
* "response" : {"hasWebview": false, "methods": {"Yoomee": "Yoomee", "MTN": "MTN" , "Orange": "Orange", "EU" : "EU"}},
2022-08-11 09:46:51 +00:00
* "error":null
* }
* )
* )
* )
*/
2022-10-08 21:12:51 +00:00
public function getMethods()
2022-08-11 09:46:51 +00:00
{
2022-11-21 22:29:50 +00:00
// $response = $this->client->get('operators');
// $providers = json_decode($response->getBody()->getContents());
$providers = ["Yoomee","MTN","Orange","EU"];
$methods = [];
foreach ($providers as $provider){
$key = 'providers.'.$provider;
$methods[$provider] = Lang::has($key) ? __($key) : $provider;
}
return $this->successResponse([
'hasWebview' => false,
'methods' => $methods
]
);
2022-08-11 09:46:51 +00:00
}
public function pay(Request $request)
{
$this->validate($request, [
'aggregator_id' => 'required|integer|exists:payment_aggregators,id',
'amount' => 'required|numeric|min:5',
'currency' => 'required|string|size:3',
'payment_method' => 'required|string',
'customer_id' => 'required|integer',
'customer_email' => 'required|email',
'customer_name' => 'required|string',
'customer_surname' => 'required|string',
'customer_phone_number' => 'required|string',
'customer_address' => 'required|string',
'customer_country' => 'required|string|size:2',
'reason' => 'required|string',
'otp' => 'nullable|string'
]);
$transaction_id = $this->getTransactionID();
$payment_method = $request->input('payment_method');
$otp = $request->input('otp');
// Create passport payment
2022-10-08 21:12:51 +00:00
$createResponse = $this->client->post('passport', [
2022-08-11 09:46:51 +00:00
'json' => [
2022-10-08 21:12:51 +00:00
"currency" => $request->input('currency'),
"customerEmail" => $request->input('customer_email'),
"customerName" => $request->input('customer_name'),
"customerPhone" => $request->input('customer_phone_number'),
"merchantCode" => config('variables.yoomee_merchant_code'),
"orderNumber" => $transaction_id,
"transactionAmount" => $request->input('amount')
2022-08-11 09:46:51 +00:00
],
2022-10-08 21:12:51 +00:00
'timeout' => $this->timeout
2022-08-11 09:46:51 +00:00
]);
2022-10-08 21:12:51 +00:00
if ($createResponse->getStatusCode() == 201) {
2022-08-11 09:46:51 +00:00
$createResponse = json_decode($createResponse->getBody()->getContents());
$transaction = PaymentTransaction::create([
'aggregator_id' => $request->input('aggregator_id'),
"currency" => $request->input('currency'),
"transaction_id" => $transaction_id,
"amount" => $createResponse->transactionAmount,
"payment_method" => $payment_method,
2022-10-08 21:12:51 +00:00
'payment_token' => Str::random(32),
2022-08-11 09:46:51 +00:00
'state' => $createResponse->paymentStatus,
"reason" => $request->input('reason'),
"customer_id" => $request->input('customer_id'),
"customer_name" => $request->input('customer_name'),
"customer_surname" => $request->input('customer_surname'),
"customer_email" => $request->input('customer_email'),
"customer_phone_number" => $request->input('customer_phone_number'),
"customer_address" => $request->input('customer_address'),
"customer_city" => $request->input('customer_city'),
"customer_country" => $request->input('customer_country'),
"customer_state" => $request->input('customer_state'),
"customer_zip_code" => $request->input('customer_zip_code'),
]);
// Pay passport payment
$paymentData = [
"customerName" => $createResponse->customerName,
"customerPhone" => $createResponse->customerPhone,
"merchantCode" => config('variables.yoomee_merchant_code'),
"paymentMethod" => $payment_method
];
2022-10-08 21:12:51 +00:00
if (!empty($otp)) {
2022-08-11 09:46:51 +00:00
$paymentData['secretOTP'] = $otp;
}
2022-10-08 21:12:51 +00:00
$payResponse = $this->client->put("passport/pay/$transaction_id", [
2022-08-11 09:46:51 +00:00
'json' => $paymentData,
2022-10-08 21:12:51 +00:00
'timeout' => $this->timeout
2022-08-11 09:46:51 +00:00
]);
$responseData = json_decode($payResponse->getBody()->getContents());
$responseCode = $payResponse->getStatusCode();
2022-10-08 21:12:51 +00:00
if ($responseCode == 202) {
2022-08-11 09:46:51 +00:00
$transaction->update([
'state' => PaymentTransactionState::ACCEPTED,
'aggregator_payment_ref' => $this->getTransactionRef($responseData->message),
]);
return $this->successResponse([
'transaction_id' => $transaction_id,
'token' => $transaction->payment_token
]);
2022-10-08 21:12:51 +00:00
} else {
return $this->errorResponse($responseData, $responseCode);
2022-08-11 09:46:51 +00:00
}
}
return $this->errorResponse(__('errors.unexpected_error'));
}
2022-10-08 21:12:51 +00:00
private function getTransactionRef($message)
2022-08-11 09:46:51 +00:00
{
2022-10-08 21:12:51 +00:00
$needle = 'REF: ';
return substr($message, strlen($needle) + strpos($message, $needle));
2022-08-11 09:46:51 +00:00
}
}