Skip to content

Commit 30db8d8

Browse files
committed
Refactor payment initiation process in InitPaymentController and update route
This commit modifies the payment initiation method in InitPaymentController, renaming it to 'payin' and streamlining the payment process with improved error handling and response structure. Additionally, it updates the API route to reflect the new method name. A new public key for Coolpay is added to the .env.ci file, and minor adjustments are made in related services and controllers for better integration.
1 parent a89e4ad commit 30db8d8

File tree

6 files changed

+101
-68
lines changed

6 files changed

+101
-68
lines changed

.env.ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LOG_CHANNEL=stack
88
LOG_DEPRECATIONS_CHANNEL=null
99
LOG_LEVEL=debug
1010

11+
1112
DB_CONNECTION=mysql
1213
DB_HOST=127.0.0.1
1314
DB_PORT=3306

app/Http/Controllers/Coins/InitPaymentController.php

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,48 @@
88
use App\Http\Controllers\Controller;
99
use Illuminate\Support\Facades\Auth;
1010
use Illuminate\Support\Facades\Http;
11+
use App\Jobs\PaymentProcessingCoinsJob;
1112

1213
class InitPaymentController extends Controller
1314
{
14-
public function PaymentCoin(Request $request){
15-
$reference=Auth::guard('api')->user()->id . '-' . uniqid();
16-
$response=$this->initPayment($request,$reference);
17-
$responseCharge=$this->charge($response,$request->phone,$request->paymentMethod);
18-
return response()->json([
19-
"status"=>"success",
20-
"message"=>"Payment initiated",
21-
"reference"=>$response,
22-
"statusCharge"=>$responseCharge
23-
]);
24-
}
25-
26-
private function initPayment(Request $request,$reference){
27-
NotchPay::setApiKey(env("NOTCHPAY_API_KEY"));
15+
public function payin(Request $request){
2816
try{
17+
$userId=Auth::guard('api')->user()->id;
18+
$url = "https://my-coolpay.com/api/".env("PUBLIC_KEY_COOLPAY_COINS")."/payin";
2919

30-
$url = "https://api.notchpay.co/payments/initialize";
31-
32-
$urlCallback="https://api-akevas.akevas.com/api/notchpay/coins/webhook";
33-
34-
35-
36-
$response=Http::acceptJson()->withBody(json_encode(
37-
[
38-
"email"=>Auth::guard('api')->user()->email,
39-
"amount"=>"10",
40-
"currency"=>"XAF",
41-
"reference"=>$reference,
42-
"phone"=>$request->phone,
43-
"type"=>"coins",
44-
"callback"=>$urlCallback,
45-
46-
]
47-
),'application/json')->withHeaders([
48-
"Authorization"=>env("NOTCHPAY_API_KEY")
49-
])->post($url);
50-
$responseData=json_decode($response);
51-
return $responseData->transaction->reference;
52-
53-
}catch(Exception $e){
54-
return response()->json([
55-
"status"=>"error",
56-
"message"=>$e->getMessage()
57-
],500);
58-
}
59-
}
60-
private function charge($reference,$phone,$provider){
61-
NotchPay::setApiKey(env("NOTCHPAY_API_KEY"));
62-
try{
63-
$url = "https://api.notchpay.co/payments/".$reference;
64-
$response=Http::acceptJson()->withBody(json_encode(
20+
$response=Http::acceptJson()->withBody(
21+
json_encode(
6522
[
66-
"channel" =>$provider,
67-
"data" => [
68-
"phone" => "+237".$phone
69-
]
70-
]
71-
),'application/json')->withHeaders([
72-
"Authorization"=>env("NOTCHPAY_API_KEY")
73-
])->post($url);
23+
"customer_phone_number"=>$request->paymentPhone,
24+
"transaction_amount"=>$request->payinAmount,
25+
]
26+
)
27+
)->post($url);
28+
7429
$responseData=json_decode($response);
75-
return $responseData->status;
76-
}catch(Exception $e){
77-
return response()->json([
78-
"status"=>"error",
79-
"message"=>$e->getMessage()
80-
],500);
30+
31+
if(isset($responseData->message ) && $responseData->message == "Le solde du compte du payeur est insuffisant."){
32+
return response()->json([
33+
"status"=>"low",
34+
"message"=>"Le solde du compte du payeur est insuffisant.",
35+
]);
36+
}else{
37+
38+
39+
PaymentProcessingCoinsJob::dispatch($request->payinAmount,$userId,$responseData->transaction_ref);
40+
41+
return response()->json([
42+
"status"=>$responseData->status,
43+
"message"=>"Payment initiated",
44+
"reference"=>$responseData->transaction_ref,
45+
"statusCharge"=>$responseData->action
46+
]);
47+
}
48+
49+
}catch (\Exception $e) {
50+
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
8151
}
8252
}
53+
54+
public function checkStatusCoins()
8355
}

app/Http/Controllers/Payment/Coolpay/Product/CheckPayinController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public function checkStatus(Request $request){
2121
]);
2222
return response()->json(['status' => $responseData->transaction_status]);
2323
}
24+
2425
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
use App\Services\Payment\Coolpay\VerifyPayinService;
12+
use App\Services\Payment\ValidatePaymentCoinService;
13+
14+
class PaymentProcessingCoinsJob implements ShouldQueue
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
/**
19+
* Create a new job instance.
20+
*/
21+
22+
public $amount;
23+
public $reference;
24+
25+
public function __construct($amount,$reference)
26+
{
27+
$this->amount=$amount;
28+
$this->reference=$this->reference;
29+
}
30+
31+
/**
32+
* Execute the job.
33+
*/
34+
public function handle(): void
35+
{
36+
$userId=Auth::guard('api')->user()->id;
37+
38+
$paymentStatus = (new VerifyPayinService())->verify($reference);
39+
$responseStatus = $paymentStatus->getData(true)['status'] ?? null;
40+
if (isset($responseStatus) && $responseStatus === "PENDING") {
41+
42+
// On redéclenche le job avec les mêmes données, pas d'objet Request
43+
Self::dispatch($amount, $reference)->delay(now()->addSeconds(15));
44+
}
45+
if (isset($responseStatus) && $responseStatus === "FAILED" || $responseStatus === "CANCELED") {
46+
47+
48+
return;
49+
}
50+
51+
if (isset($responseStatus) && $responseStatus == 'SUCCESS') {
52+
(new ValidatePaymentCoinService())->handle($this->reference,$this->amount,$userId);
53+
}
54+
55+
}
56+
}

app/Services/Payment/ValidatePaymentCoinService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Shop;
66
use App\Models\Payment;
7+
use App\Services\Payment\Coolpay\VerifyPayinService;
78

89
class ValidatePaymentCoinService
910
{
@@ -22,10 +23,12 @@ public function handle(
2223
'payment_of' => 'coins',
2324
'user_id' => $userId,
2425
]);
25-
26-
$shop = Shop::where('user_id', $userId)->first();
27-
$shop->coins += $amount;
28-
$shop->save();
26+
27+
$shop = Shop::where('user_id', $userId)->first();
28+
$shop->coins += $amount;
29+
$shop->save();
30+
31+
2932
}
3033
}
3134
}

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
Route::post("check/payment/subscription/product/callback", [SubscribeProductController::class, "paymentCallBack"]);
174174

175175
Route::post('validate/payment/coins',[ValidatePaymentCoinController::class,'handle']);
176-
Route::post("init/payment/coins", [InitCoinsPaymentController::class, "PaymentCoin"]);
176+
Route::post("init/payment/coins", [InitCoinsPaymentController::class, "payin"]);
177177
Route::post("init/payment/subscription/shop", [SubscribeShopController::class, "initPay"]);
178178
Route::post('init/payment/subscription/shop/pending/{membership_id}/{shop_id}/{transaction_ref}', [SubscribeShopController::class, 'initPaymentPending']);
179179
Route::apiResource('/shops', ShopController::class);

0 commit comments

Comments
 (0)