feat: WIP reverepay paymenter extension
This commit is contained in:
parent
db2432e996
commit
e7f89b09b1
133
ReverePay/index.php
Normal file
133
ReverePay/index.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Extensions\Gateways\ReverePay;
|
||||
|
||||
use App\Classes\Extensions\Gateway;
|
||||
use App\Helpers\ExtensionHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use Exception;
|
||||
|
||||
class ReverePay extends Gateway
|
||||
{
|
||||
public function getMetadata()
|
||||
{
|
||||
return [
|
||||
'display_name' => 'ReverePay',
|
||||
'version' => '1.0.0',
|
||||
'author' => 'Paymenter',
|
||||
'website' => 'https://paymenter.org',
|
||||
];
|
||||
}
|
||||
|
||||
public function getUrl($total, $products, $orderId, $client = null)
|
||||
{
|
||||
$apiKey = ExtensionHelper::getConfig('ReverePay', 'revere_api_key');
|
||||
$isTestMode = ExtensionHelper::getConfig('ReverePay', 'revere_test_mode');
|
||||
|
||||
$baseUrl = $isTestMode ? 'https://sandbox-api.reverepayments.dev/v1' : 'https://api.reverepayments.dev/v1';
|
||||
|
||||
// Create line items array
|
||||
$items = [];
|
||||
foreach ($products as $product) {
|
||||
$items[] = [
|
||||
'name' => $product->name,
|
||||
'amount' => $product->price * 100, // Convert to cents
|
||||
'quantity' => $product->quantity
|
||||
];
|
||||
}
|
||||
|
||||
// Create payment session
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/payment-sessions');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Authorization: Bearer ' . $apiKey,
|
||||
'Content-Type: application/json'
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'amount' => $total * 100, // Convert to cents
|
||||
'currency' => ExtensionHelper::getCurrency(),
|
||||
'line_items' => $items,
|
||||
'success_url' => route('clients.invoice.show', $orderId),
|
||||
'cancel_url' => route('clients.invoice.show', $orderId),
|
||||
'metadata' => [
|
||||
'order_id' => $orderId,
|
||||
'user_id' => auth()->user()->id
|
||||
],
|
||||
'customer' => [
|
||||
'email' => auth()->user()->email
|
||||
]
|
||||
];
|
||||
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
throw new Exception('Failed to create Revere payment session');
|
||||
}
|
||||
|
||||
return json_decode($response);
|
||||
}
|
||||
|
||||
public function webhook(Request $request)
|
||||
{
|
||||
$payload = $request->getContent();
|
||||
$signature = $request->header('revere-signature');
|
||||
$webhookSecret = ExtensionHelper::getConfig('ReverePay', 'revere_webhook_secret');
|
||||
|
||||
// Verify webhook signature
|
||||
$computedSignature = hash_hmac('sha256', $payload, $webhookSecret);
|
||||
if (!hash_equals($computedSignature, $signature)) {
|
||||
http_response_code(400);
|
||||
exit('Invalid signature');
|
||||
}
|
||||
|
||||
$event = json_decode($payload);
|
||||
|
||||
if ($event->type === 'payment.succeeded') {
|
||||
$orderId = $event->data->metadata->order_id;
|
||||
ExtensionHelper::paymentDone($orderId);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function pay($total, $products, $orderId)
|
||||
{
|
||||
$session = $this->getUrl($total, $products, $orderId);
|
||||
return $session->checkout_url;
|
||||
}
|
||||
|
||||
public function getConfig()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'name' => 'revere_api_key',
|
||||
'friendlyName' => 'Revere API Key',
|
||||
'type' => 'text',
|
||||
'description' => 'Your Revere Payments API key',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'revere_webhook_secret',
|
||||
'friendlyName' => 'Revere Webhook Secret',
|
||||
'type' => 'text',
|
||||
'description' => 'Your Revere Payments webhook secret for verifying webhook signatures',
|
||||
'required' => true,
|
||||
],
|
||||
[
|
||||
'name' => 'revere_test_mode',
|
||||
'friendlyName' => 'Test Mode',
|
||||
'type' => 'boolean',
|
||||
'description' => 'Enable test mode to use sandbox environment',
|
||||
'required' => false,
|
||||
'default' => false,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
9
ReverePay/routes.php
Normal file
9
ReverePay/routes.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
include_once __DIR__ . '/index.php';
|
||||
|
||||
Route::post('/reverepay/webhook', function () {
|
||||
ReverePay_webhook(request());
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user