-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Mercadopago Integration #926
Merged
IceToast
merged 13 commits into
Ctrlpanel-gg:development
from
drylian:mercadopago-implementation
Jul 24, 2024
+267
−5
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62149da
mercadopago payment add init
drylian 361fbe0
add mercadopago img
drylian 02b7767
tested mercadopago routes
drylian 7012a68
mercado pago end
drylian 4404d4a
Standardization and Organization
drylian 1e1aad1
Merge branch 'development' into mercadopago-implementation
S0ly 924abc0
Add use MercadoPagoSettings and update $notificationId
drylian 2b8c40b
Update MercadoPagoExtension.php
drylian f57ee46
Final organization of resources
drylian dbc12c1
fix: README.md
S0ly d082ca1
add encrypted function
drylian 4764d8d
Merge branch 'mercadopago-implementation' of https://github.com/dryli…
drylian 521e907
Merge branch 'development' into mercadopago-implementation
drylian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
app/Extensions/PaymentGateways/MercadoPago/MercadoPagoExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
<?php | ||
|
||
namespace App\Extensions\PaymentGateways\MercadoPago; | ||
|
||
use App\Classes\AbstractExtension; | ||
use App\Enums\PaymentStatus; | ||
use App\Events\PaymentEvent; | ||
use App\Events\UserUpdateCreditsEvent; | ||
use App\Models\Payment; | ||
use App\Models\ShopProduct; | ||
use App\Models\User; | ||
use App\Traits\Coupon as CouponTrait; | ||
use Exception; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Redirect; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Http; | ||
use App\Notifications\ConfirmPaymentNotification; | ||
|
||
/** | ||
* Summary of MercadoPagoExtension | ||
*/ | ||
class MercadoPagoExtension extends AbstractExtension | ||
{ | ||
use CouponTrait; | ||
|
||
public static function getConfig(): array | ||
{ | ||
return [ | ||
"name" => "MercadoPago", | ||
"RoutesIgnoreCsrf" => [ | ||
"payment/MercadoPagoWebhook" | ||
], | ||
]; | ||
} | ||
|
||
public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string | ||
{ | ||
$user = Auth::user(); | ||
$user = User::findOrFail($user->id); | ||
$url = 'https://api.mercadopago.com/checkout/preferences'; | ||
$settings = new MercadoPagoSettings(); | ||
try { | ||
$response = Http::withHeaders([ | ||
'Content-Type' => 'application/json', | ||
'Authorization' => 'Bearer ' . $settings->access_token, | ||
])->post($url, [ | ||
'back_urls' => [ | ||
'success' => route('payment.MercadoPagoChecker'), | ||
'failure' => route('payment.Cancel'), | ||
'pending' => route('payment.MercadoPagoChecker'), | ||
], | ||
'notification_url' => route('payment.MercadoPagoWebhook'), | ||
'payer' => [ | ||
'email' => $user->email, | ||
], | ||
'items' => [ | ||
[ | ||
'title' => "Order #{$payment->id} - " . $shopProduct->name, | ||
'quantity' => 1, | ||
'unit_price' => $totalPriceString, | ||
'currency_id' => $shopProduct->currency_code, | ||
], | ||
], | ||
'metadata' => [ | ||
'credit_amount' => $shopProduct->quantity, | ||
'user_id' => $user->id, | ||
'crtl_panel_payment_id' => $payment->id, | ||
], | ||
]); | ||
|
||
if ($response->successful()) { | ||
// preferenceID | ||
$preferenceId = $response->json()['id']; | ||
|
||
// Redirect link | ||
return ("https://www.mercadopago.com/checkout/v1/redirect?preference-id=" . $preferenceId); | ||
} else { | ||
Log::error('MercadoPago Payment: ' . $response->body()); | ||
throw new Exception('Payment failed'); | ||
} | ||
} catch (Exception $ex) { | ||
Log::error('MercadoPago Payment: ' . $ex->getMessage()); | ||
throw new Exception('Payment failed'); | ||
} | ||
} | ||
|
||
static function Checker(Request $request): void | ||
{ | ||
// paymentID (not is preferenceID or paymentID for store) | ||
$paymentId = $request->input('payment_id'); | ||
|
||
$MpPayment = self::MpPayment($paymentId, false); | ||
|
||
switch ($MpPayment) { | ||
case "paid": | ||
Redirect::route('home')->with('success', 'Payment successful')->send(); | ||
break; | ||
case "cancelled": | ||
Redirect::route('home')->with('info', 'Your canceled the payment')->send(); | ||
break; | ||
case "processing": | ||
Redirect::route('home')->with('info', 'Your payment is being processed')->send(); | ||
break; | ||
default: | ||
Redirect::route('home')->with('error', 'Your payment is unknown')->send(); | ||
break; | ||
} | ||
} | ||
|
||
static function Webhook(Request $request): JsonResponse | ||
{ | ||
$topic = $request->input('topic'); | ||
$msg = 'unset'; | ||
$status = 400; | ||
if ($topic === 'merchant_order') { | ||
$msg = 'ignored'; | ||
$status = 200; | ||
} else if ($topic === 'payment') { | ||
$msg = 'ignored'; | ||
$status = 200; | ||
} else { | ||
try { | ||
$notificationId = $request->input('data.id') ?? $request->input('id') ?? $request->input('payment_id') ?? 'unknown'; | ||
if ($notificationId == 'unknown') { | ||
$msg = 'unknown payment.'; | ||
$status = 400; | ||
} else if ($notificationId == '123456') { | ||
$msg = 'MercadoPago api test'; | ||
$status = 200; | ||
} else { | ||
$MpPayment = self::MpPayment($notificationId, true); | ||
switch ($MpPayment) { | ||
case "paid": | ||
$msg = $MpPayment; | ||
$status = 200; | ||
break; | ||
|
||
case "cancelled": | ||
$msg = $MpPayment; | ||
$status = 200; | ||
break; | ||
|
||
case "processing": | ||
$msg = $MpPayment; | ||
$status = 200; | ||
break; | ||
default: | ||
$msg = 'unknown'; | ||
$status = 400; | ||
break; | ||
} | ||
} | ||
} catch (Exception $ex) { | ||
Log::error('MercadoPago Webhook(IPN) Payment: ' . $ex->getMessage()); | ||
$msg = 'error'; | ||
$status = 500; | ||
} | ||
} | ||
$response = new JsonResponse($msg, $status); | ||
return $response; | ||
} | ||
/** | ||
* Mercado Pago Payment checker | ||
*/ | ||
private function MpPayment(string $paymentID, bool $notification): string | ||
{ | ||
$MpResponse = "unknown"; | ||
$payment = "unknown"; | ||
$url = "https://api.mercadopago.com/v1/payments/" . $paymentID; | ||
$settings = new MercadoPagoSettings(); | ||
$response = Http::withHeaders([ | ||
'Content-Type' => 'application/json', | ||
'Authorization' => 'Bearer ' . $settings->access_token, | ||
])->get($url); | ||
|
||
if ($response->successful()) { | ||
$mercado = $response->json(); | ||
$status = $mercado->status; | ||
$payment = Payment::findOrFail($mercado->metadata->crtl_panel_payment_id); | ||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id); | ||
|
||
if ($status == "approved") { | ||
// avoids double additions, if the user enters after the webhook has already added the credits | ||
if ($payment->status !== PaymentStatus::PAID) { | ||
$user = User::findOrFail($payment->user_id); | ||
$payment->update([ | ||
'status' => PaymentStatus::PAID, | ||
'payment_id' => $paymentID, | ||
]); | ||
$payment->save(); | ||
if ($notification) { | ||
$user->notify(new ConfirmPaymentNotification($payment)); | ||
} | ||
event(new PaymentEvent($user, $payment, $shopProduct)); | ||
event(new UserUpdateCreditsEvent($user)); | ||
} | ||
$MpResponse = "paid"; | ||
} else { | ||
if ($status == "cancelled") { | ||
$user = User::findOrFail($payment->user_id); | ||
$payment->update([ | ||
'status' => PaymentStatus::CANCELED, | ||
'payment_id' => $paymentID, | ||
]); | ||
$payment->save(); | ||
event(new PaymentEvent($user, $payment, $shopProduct)); | ||
$MpResponse = "cancelled"; | ||
} else { | ||
$user = User::findOrFail($payment->user_id); | ||
$payment->update([ | ||
'status' => PaymentStatus::PROCESSING, | ||
'payment_id' => $paymentID, | ||
]); | ||
$payment->save(); | ||
event(new PaymentEvent($user, $payment, $shopProduct)); | ||
$MpResponse = "processing"; | ||
} | ||
} | ||
} | ||
return $MpResponse; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/Extensions/PaymentGateways/MercadoPago/MercadoPagoSettings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Extensions\PaymentGateways\MercadoPago; | ||
|
||
use Spatie\LaravelSettings\Settings; | ||
|
||
class MercadoPagoSettings extends Settings | ||
{ | ||
|
||
public bool $enabled = false; | ||
public ?string $access_token; | ||
IceToast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static function group(): string | ||
{ | ||
return 'mercadopago'; | ||
} | ||
|
||
public static function getOptionInputData() | ||
{ | ||
return [ | ||
'category_icon' => 'fas fa-dollar-sign', | ||
'access_token' => [ | ||
'type' => 'string', | ||
'label' => 'Access Token Key', | ||
'description' => 'The Access Token of your Mercado Pago App', | ||
], | ||
'enabled' => [ | ||
'type' => 'boolean', | ||
'label' => 'Enabled', | ||
'description' => 'Enable or disable this payment gateway', | ||
], | ||
]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../PaymentGateways/MercadoPago/migrations/2024_01_24_120635_create_mercadopago_settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
use Spatie\LaravelSettings\Migrations\SettingsMigration; | ||
|
||
class CreateMercadoPagoSettings extends SettingsMigration | ||
{ | ||
public function up(): void | ||
{ | ||
$this->migrator->addEncrypted('mpago.access_token', null); | ||
$this->migrator->add('mpago.enabled', false); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
$this->migrator->delete('mpago.access_token'); | ||
$this->migrator->delete('mpago.enabled'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use App\Extensions\PaymentGateways\MercadoPago\MercadoPagoExtension; | ||
|
||
Route::middleware(['web', 'auth'])->group(function () { | ||
Route::get( | ||
'payment/MercadoPagoChecker', | ||
function () { | ||
MercadoPagoExtension::Checker(request()); | ||
} | ||
)->name('payment.MercadoPagoChecker'); | ||
}); | ||
|
||
|
||
Route::post('payment/MercadoPagoWebhook', function () { | ||
MercadoPagoExtension::Webhook(request()); | ||
})->name('payment.MercadoPagoWebhook'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to follow the naming scheme already used in other payment gateways.