Compare commits

...

2 Commits

Author SHA1 Message Date
Thomas Cross
618fbdc387 integrate revere collect (checkout session) 2025-03-03 17:30:10 -06:00
Thomas Cross
2e174ec785 integrate with revere payment session 2025-03-03 14:40:59 -06:00

View File

@ -22,57 +22,75 @@ class ReverePayments extends Gateway
public function getUrl($total, $products, $orderId, $client = null) public function getUrl($total, $products, $orderId, $client = null)
{ {
$apiKey = ExtensionHelper::getConfig('ReverePayments', 'revere_api_key'); $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'); $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';
// Create line items array $baseCartUrl = $isTestMode ? 'https://secure.reverepayments.com' : 'https://secure.reverepayments.com';
$items = [];
$lineItems = [];
foreach ($products as $product) { foreach ($products as $product) {
$items[] = [ error_log('Product: ' . print_r($product, true));
'name' => $product->name,
'amount' => $product->price * 100, // Convert to cents // Ensure the product exists in Revere Payments
'quantity' => $product->quantity // 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,
]; ];
} }
// Create payment session // Create payment request
$ch = curl_init(); $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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json' 'Content-Type: application/json'
]); ]);
$data = [ $data = [
'amount' => $total * 100, // Convert to cents 'type' => 'sale',
'currency' => ExtensionHelper::getCurrency(), 'customerVault' => [
'line_items' => $items, 'addCustomer' => true
'success_url' => route('clients.invoice.show', $orderId),
'cancel_url' => route('clients.invoice.show', $orderId),
'metadata' => [
'order_id' => $orderId,
'user_id' => auth()->user()->id
], ],
'customer' => [ 'lineItems' => $lineItems,
'email' => auth()->user()->email 'successUrl' => 'https://shilohcode.com',
] 'cancelUrl' => 'https://paymenter.lasthourhosting.org/checkout',
'key' => $publicCheckoutKey,
]; ];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 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); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
if ($httpCode !== 200) { if ($httpCode < 200 || $httpCode >= 300) {
throw new Exception('Failed to create Revere payment session'); throw new Exception('Failed to create Revere payment session: ' . $response);
} }
return json_decode($response); $responseData = json_decode($response, true);
if (!isset($responseData['id'])) {
throw new Exception('Invalid response from Revere Payments API');
}
$redirectUrl = "https://collectcheckout.com/collect-checkout/?cartId=" . $responseData['id'];
return $redirectUrl;
} }
public function webhook(Request $request) public function webhook(Request $request)
@ -100,8 +118,7 @@ class ReverePayments extends Gateway
public function pay($total, $products, $orderId) public function pay($total, $products, $orderId)
{ {
$session = $this->getUrl($total, $products, $orderId); return $this->getUrl($total, $products, $orderId);
return $session->checkout_url;
} }
public function getConfig() public function getConfig()
@ -111,7 +128,7 @@ class ReverePayments extends Gateway
'name' => 'revere_api_key', 'name' => 'revere_api_key',
'friendlyName' => 'Revere API Key', 'friendlyName' => 'Revere API Key',
'type' => 'text', 'type' => 'text',
'description' => 'Your Revere Payments API key', 'description' => 'Your Revere Payments Checkout Public Key (starts with checkout_public_)',
'required' => true, 'required' => true,
], ],
[ [
@ -121,6 +138,13 @@ class ReverePayments extends Gateway
'description' => 'Your Revere Payments webhook secret for verifying webhook signatures', 'description' => 'Your Revere Payments webhook secret for verifying webhook signatures',
'required' => true, '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', 'name' => 'revere_test_mode',
'friendlyName' => 'Test Mode', 'friendlyName' => 'Test Mode',
@ -131,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;
}
} }