From 618fbdc3873b646cb2a1cbefd9dda851d437335d Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Mon, 3 Mar 2025 17:30:10 -0600 Subject: [PATCH] integrate revere collect (checkout session) --- ReverePayments/ReverePayments.php | 143 +++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 14 deletions(-) diff --git a/ReverePayments/ReverePayments.php b/ReverePayments/ReverePayments.php index f14440e..41d4558 100644 --- a/ReverePayments/ReverePayments.php +++ b/ReverePayments/ReverePayments.php @@ -22,24 +22,30 @@ class ReverePayments extends Gateway public function getUrl($total, $products, $orderId, $client = null) { $apiKey = ExtensionHelper::getConfig('ReverePayments', 'revere_api_key'); + $publicCheckoutKey = ExtensionHelper::getConfig('ReverePayments', 'revere_public_checkout_key'); + $publicCheckoutKey = 'checkout_public_R7Kgf3GyG9Bru9W5vhR293qd22U8rn8H'; $isTestMode = ExtensionHelper::getConfig('ReverePayments', 'revere_test_mode'); // 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 for Collect Checkout + $lineItems = []; foreach ($products as $product) { - $lineItems[] = [ - 'sku' => $product->id, - // 'name' => $product->name, - // 'description' => $product->description ?? $product->name, - 'quantity' => $product->quantity, - // 'price' => $product->price + error_log('Product: ' . print_r($product, true)); + // Ensure the product exists in Revere Payments + // This will create or update the product if needed + // $this->createOrUpdateProduct($product); + + // Use the generateProductSku method for consistency + // $sku = $this->generateProductSku($product); + $sku = 'abc-123'; + + $lineItems[] = [ + 'sku' => $sku, + 'quantity' => $product->quantity, ]; } @@ -58,9 +64,9 @@ class ReverePayments extends Gateway 'addCustomer' => true ], 'lineItems' => $lineItems, - 'successUrl' => route('clients.invoice.show', $orderId), - 'cancelUrl' => route('clients.invoice.show', $orderId), - 'key' => $apiKey, + 'successUrl' => 'https://shilohcode.com', + 'cancelUrl' => 'https://paymenter.lasthourhosting.org/checkout', + 'key' => $publicCheckoutKey, ]; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); @@ -76,12 +82,15 @@ class ReverePayments extends Gateway } $responseData = json_decode($response, true); + - if (!isset($responseData['url'])) { + if (!isset($responseData['id'])) { throw new Exception('Invalid response from Revere Payments API'); } - return $responseData['url']; + $redirectUrl = "https://collectcheckout.com/collect-checkout/?cartId=" . $responseData['id']; + + return $redirectUrl; } public function webhook(Request $request) @@ -129,6 +138,13 @@ class ReverePayments extends Gateway 'description' => 'Your Revere Payments webhook secret for verifying webhook signatures', 'required' => true, ], + [ + 'name' => 'revere_public_checkout_key', + 'friendlyName' => 'Revere Public Checkout Key', + 'type' => 'text', + 'description' => 'Your Revere Payments Checkout Public Key (starts with checkout_public_)', + 'required' => true, + ], [ 'name' => 'revere_test_mode', 'friendlyName' => 'Test Mode', @@ -139,4 +155,103 @@ class ReverePayments extends Gateway ] ]; } + + /** + * Generate a unique SKU for a product that can be reused + * + * @param object $product The product object + * @return string The generated SKU + */ + private function generateProductSku($product) + { + // Create a unique but consistent SKU based on product attributes + // Format: PM-{product_id}-{hash of product name} + $productNameHash = substr(md5($product->name), 0, 8); + return 'PM-' . $product->product_id . '-' . $productNameHash; + } + + /** + * Create a product in Revere Payments system + * + * @param object $product The product to create + * @return array The created product data + */ + public function createOrUpdateProduct($product) + { + $apiKey = ExtensionHelper::getConfig('ReverePayments', 'revere_api_key'); + $isTestMode = ExtensionHelper::getConfig('ReverePayments', 'revere_test_mode'); + + $baseRevereApiUrl = $isTestMode ? 'https://api.sandbox.reverepayments.dev' : 'https://api.reverepayments.dev'; + + $sku = $this->generateProductSku($product); + + // Check if product already exists + $existingProduct = $this->getProductBySku($sku); + + // Determine if we are adding or updating a product + $action = $existingProduct ? 'update_product' : 'add_product'; + + // Product data to send to Revere Payments + $productData = [ + 'products' => $action, + 'product_sku' => $sku, + 'product_description' => $product->description ?? $product->name, + 'product_cost' => $product->price, + 'product_currency' => 'USD', // Assuming USD, adjust as needed + // Add any other required product attributes here + ]; + + if ($existingProduct) { + $productData['product_id'] = $existingProduct['id']; + } + + // Initialize cURL session + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, 'https://secure.reverepayments.com/api/transact.php'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/x-www-form-urlencoded', + 'Authorization: Bearer ' . $apiKey + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($productData)); + + // Execute cURL request + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpCode < 200 || $httpCode >= 300) { + throw new Exception('Failed to create or update product in Revere Payments: ' . $response); + } + + $responseData = json_decode($response, true); + + if (!isset($responseData['success']) || !$responseData['success']) { + throw new Exception('Invalid response from Revere Payments API: ' . $response); + } + + return $responseData; + } + + /** + * Get a product from Revere Payments by SKU + * + * @param string $sku The SKU of the product to retrieve + * @return array|null The product data or null if not found + */ + public function getProductBySku($sku) + { + $apiKey = ExtensionHelper::getConfig('ReverePayments', 'revere_api_key'); + $isTestMode = ExtensionHelper::getConfig('ReverePayments', 'revere_test_mode'); + + $baseRevereApiUrl = $isTestMode ? 'https://api.sandbox.reverepayments.dev' : 'https://api.reverepayments.dev'; + + // This is a placeholder for the actual API endpoint + // You would need to implement the actual API call to Revere Payments + // to retrieve a product by SKU + + // For now, we'll just return null to indicate the product doesn't exist + return null; + } } \ No newline at end of file