168 lines
6.2 KiB
PHP
168 lines
6.2 KiB
PHP
<?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;
|
|
use Illuminate\Support\Str;
|
|
|
|
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(
|
|
* path="/yoomee/methods",
|
|
* summary="Afficher la liste des methodes de paiements de Yoomee",
|
|
* tags={"Yoomee"},
|
|
* security={{"api_key":{}}},
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="OK",
|
|
* @OA\JsonContent(
|
|
* ref="#/components/schemas/ApiResponse",
|
|
* example = {
|
|
* "status" : 200,
|
|
* "response" : {{"Yoomee","MTN","Orange","EU"}},
|
|
* "error":null
|
|
* }
|
|
* )
|
|
* )
|
|
* )
|
|
*/
|
|
public function getMethods()
|
|
{
|
|
$response = $this->client->get('operators');
|
|
$providers = json_decode($response->getBody()->getContents());
|
|
$methods = [];
|
|
foreach ($providers as $provider){
|
|
$key = 'providers.'.$provider;
|
|
$methods[$provider] = Lang::has($key) ? __($key) : $provider;
|
|
}
|
|
return $this->successResponse([
|
|
'hasWebview' => false,
|
|
'methods' => $methods
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
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
|
|
$createResponse = $this->client->post('passport', [
|
|
'json' => [
|
|
"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')
|
|
],
|
|
'timeout' => $this->timeout
|
|
]);
|
|
|
|
if ($createResponse->getStatusCode() == 201) {
|
|
$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,
|
|
'payment_token' => Str::random(32),
|
|
'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
|
|
];
|
|
if (!empty($otp)) {
|
|
$paymentData['secretOTP'] = $otp;
|
|
}
|
|
$payResponse = $this->client->put("passport/pay/$transaction_id", [
|
|
'json' => $paymentData,
|
|
'timeout' => $this->timeout
|
|
]);
|
|
|
|
$responseData = json_decode($payResponse->getBody()->getContents());
|
|
$responseCode = $payResponse->getStatusCode();
|
|
|
|
if ($responseCode == 202) {
|
|
$transaction->update([
|
|
'state' => PaymentTransactionState::ACCEPTED,
|
|
'aggregator_payment_ref' => $this->getTransactionRef($responseData->message),
|
|
]);
|
|
return $this->successResponse([
|
|
'transaction_id' => $transaction_id,
|
|
'token' => $transaction->payment_token
|
|
]);
|
|
} else {
|
|
return $this->errorResponse($responseData, $responseCode);
|
|
}
|
|
}
|
|
|
|
return $this->errorResponse(__('errors.unexpected_error'));
|
|
}
|
|
|
|
private function getTransactionRef($message)
|
|
{
|
|
$needle = 'REF: ';
|
|
return substr($message, strlen($needle) + strpos($message, $needle));
|
|
}
|
|
}
|