From e8a8c804a006ecddf77c5933579b3d5239d82db8 Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 6 Mar 2025 09:05:24 -0600 Subject: [PATCH] ignore already added skus --- ReverePayments/ReverePayments.php | 99 +++++++++++++++++-------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/ReverePayments/ReverePayments.php b/ReverePayments/ReverePayments.php index 95869b2..e070019 100644 --- a/ReverePayments/ReverePayments.php +++ b/ReverePayments/ReverePayments.php @@ -165,8 +165,7 @@ class ReverePayments extends Gateway { // 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; + return $product->name; } /** @@ -179,30 +178,19 @@ class ReverePayments extends Gateway { $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); - - $action = $existingProduct ? 'update_product' : 'add_product'; - - // Product data to send to Revere Payments + + // Try to ADD the product and if it fails, update the product $productData = [ 'security_key' => $apiKey, - 'products' => $action, + 'products' => 'add_product', 'product_sku' => $sku, - 'product_description' => $product->description ?? $product->name, + 'product_description' => $product->name ?? $product->description, 'product_cost' => $product->price, 'product_currency' => 'USD', ]; - 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'); @@ -210,7 +198,7 @@ class ReverePayments extends Gateway curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded', - 'Authorization: Bearer ' . $apiKey + 'Accept: application/json', ]); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($productData)); @@ -222,36 +210,57 @@ class ReverePayments extends Gateway if ($httpCode < 200 || $httpCode >= 300) { throw new Exception('Failed to create or update product in Revere Payments: ' . $response); } - - $responseData = json_decode($response, true); - parse_str($response, $responseArray); + + $responseData = []; + parse_str($response, $responseData); - - if (!isset($responseArray['response']) || $responseArray['response'] != "1") { - throw new Exception('Invalid response from Revere Payments API: ' . $responseArray); + $isSkuInUse = isset($responseData['responsetext']) && str_contains($responseData['responsetext'], "SKU already in use"); + + + if ($isSkuInUse) { + return $responseData; + + // Try to update the product if it already exists + // If the response text contains "SKU already in use", we need to update the sku + + // $productData = [ + // 'security_key' => $apiKey, + // 'products' => 'update_product', + // // 'product_id' => $responseData['product_id'], + // 'product_sku' => $sku, + // 'product_description' => $product->name ?? $product->description, + // 'product_cost' => $product->price, + // 'product_currency' => 'USD', + // ]; + + // // 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', + // 'Accept: application/json', + // ]); + // 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['response']) || $responseData['response'] != "1") { + throw new Exception('Invalid response from Revere Payments API: ' . $responseData . '\n\n'. $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