From 2e174ec7853429d5d91ea09eca2252690f73ac2f Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Mon, 3 Mar 2025 14:40:59 -0600 Subject: [PATCH] integrate with revere payment session --- ReverePayments/ReverePayments.php | 64 +++++++++++++++++-------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/ReverePayments/ReverePayments.php b/ReverePayments/ReverePayments.php index ecc68f4..f14440e 100644 --- a/ReverePayments/ReverePayments.php +++ b/ReverePayments/ReverePayments.php @@ -24,55 +24,64 @@ class ReverePayments extends Gateway $apiKey = ExtensionHelper::getConfig('ReverePayments', 'revere_api_key'); $isTestMode = ExtensionHelper::getConfig('ReverePayments', 'revere_test_mode'); - $baseUrl = $isTestMode ? 'https://api.sandbox.reverepayments.dev' : 'https://api.reverepayments.dev'; + // TODO: find out what the sandbox url is + $baseRevereApiUrl = $isTestMode ? 'https://api.sandbox.reverepayments.dev' : 'https://api.reverepayments.dev'; + $baseCartUrl = $isTestMode ? 'https://secure.reverepayments.com' : 'https://secure.reverepayments.com'; + - // Create line items array - $items = []; + + // Create line items array for Collect Checkout + $lineItems = []; foreach ($products as $product) { - $items[] = [ - 'name' => $product->name, - 'amount' => $product->price * 100, // Convert to cents - 'quantity' => $product->quantity + $lineItems[] = [ + 'sku' => $product->id, + // 'name' => $product->name, + // 'description' => $product->description ?? $product->name, + 'quantity' => $product->quantity, + // 'price' => $product->price + ]; } - // Create payment session + // Create payment request $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $baseUrl . '/payment-sessions'); + curl_setopt($ch, CURLOPT_URL, $baseCartUrl . '/api/v4/cart'); 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 + 'type' => 'sale', + 'customerVault' => [ + 'addCustomer' => true ], - 'customer' => [ - 'email' => auth()->user()->email - ] + 'lineItems' => $lineItems, + 'successUrl' => route('clients.invoice.show', $orderId), + 'cancelUrl' => route('clients.invoice.show', $orderId), + 'key' => $apiKey, ]; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); - curl_setopt($curlHandle, CURLOPT_VERBOSE, true); + // Enable verbose output for debugging + curl_setopt($ch, CURLOPT_VERBOSE, true); $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'); + if ($httpCode < 200 || $httpCode >= 300) { + throw new Exception('Failed to create Revere payment session: ' . $response); } - return json_decode($response); + $responseData = json_decode($response, true); + + if (!isset($responseData['url'])) { + throw new Exception('Invalid response from Revere Payments API'); + } + + return $responseData['url']; } public function webhook(Request $request) @@ -100,8 +109,7 @@ class ReverePayments extends Gateway public function pay($total, $products, $orderId) { - $session = $this->getUrl($total, $products, $orderId); - return $session->checkout_url; + return $this->getUrl($total, $products, $orderId); } public function getConfig() @@ -111,7 +119,7 @@ class ReverePayments extends Gateway 'name' => 'revere_api_key', 'friendlyName' => 'Revere API Key', 'type' => 'text', - 'description' => 'Your Revere Payments API key', + 'description' => 'Your Revere Payments Checkout Public Key (starts with checkout_public_)', 'required' => true, ], [