Просмотр исходного кода

fix: return 200 OK instead of redirects for WooCommerce OAuth callback #18

WooCommerce expects callback endpoints to return 200 OK responses.
WooCommerce itself handles redirecting the user to the return_url.
Our previous implementation was trying to redirect WooCommerce, which
caused the error 'An error occurred in the request and at the time
were unable to send the consumer data'.
Claude 5 месяцев назад
Родитель
Сommit
3e024ba3f7
1 измененных файлов с 39 добавлено и 51 удалено
  1. 39 51
      supabase/functions/oauth-woocommerce/index.ts

+ 39 - 51
supabase/functions/oauth-woocommerce/index.ts

@@ -373,36 +373,31 @@ serve(async (req) => {
 
       if (success !== '1') {
         console.error('[WooCommerce] OAuth rejected by user')
-        return new Response(null, {
-          status: 302,
-          headers: {
-            ...corsHeaders,
-            'Location': `${frontendUrl}/webshops?error=woocommerce_oauth_rejected`
-          }
-        })
+        // Return 200 OK - WooCommerce handles the redirect to return_url
+        return new Response(
+          JSON.stringify({ success: false, message: 'OAuth rejected by user' }),
+          { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+        )
       }
 
       if (!userId || !consumerKey || !consumerSecret || !storeUrlParam) {
         console.error('[WooCommerce] Missing required callback parameters')
-        return new Response(null, {
-          status: 302,
-          headers: {
-            ...corsHeaders,
-            'Location': `${frontendUrl}/webshops?error=woocommerce_oauth_failed`
-          }
-        })
+        // Return 200 OK - WooCommerce handles the redirect to return_url
+        return new Response(
+          JSON.stringify({ success: false, message: 'Missing required parameters' }),
+          { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+        )
       }
 
       // Validate store URL
       const validation = validateStoreUrl(storeUrlParam)
       if (!validation.valid) {
-        return new Response(null, {
-          status: 302,
-          headers: {
-            ...corsHeaders,
-            'Location': `${frontendUrl}/webshops?error=invalid_store_url`
-          }
-        })
+        console.error('[WooCommerce] Invalid store URL:', validation.error)
+        // Return 200 OK - WooCommerce handles the redirect to return_url
+        return new Response(
+          JSON.stringify({ success: false, message: validation.error }),
+          { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+        )
       }
 
       // Test API connection
@@ -414,13 +409,11 @@ serve(async (req) => {
 
       if (!testResult.success) {
         console.error('[WooCommerce] API connection test failed:', testResult.error)
-        return new Response(null, {
-          status: 302,
-          headers: {
-            ...corsHeaders,
-            'Location': `${frontendUrl}/webshops?error=woocommerce_connection_failed`
-          }
-        })
+        // Return 200 OK - WooCommerce handles the redirect to return_url
+        return new Response(
+          JSON.stringify({ success: false, message: testResult.error }),
+          { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+        )
       }
 
       // Extract store info from API response
@@ -454,25 +447,21 @@ serve(async (req) => {
 
       if (insertError) {
         console.error('[WooCommerce] Error storing credentials:', insertError)
-        return new Response(null, {
-          status: 302,
-          headers: {
-            ...corsHeaders,
-            'Location': `${frontendUrl}/webshops?error=failed_to_save`
-          }
-        })
+        // Return 200 OK - WooCommerce handles the redirect to return_url
+        return new Response(
+          JSON.stringify({ success: false, message: 'Failed to save credentials' }),
+          { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+        )
       }
 
       console.log(`[WooCommerce] Store connected successfully: ${storeName}`)
 
-      // Redirect back to frontend with success
-      return new Response(null, {
-        status: 302,
-        headers: {
-          ...corsHeaders,
-          'Location': `${frontendUrl}/webshops?wc_connected=true&store=${encodeURIComponent(storeName)}`
-        }
-      })
+      // Return 200 OK with success message
+      // WooCommerce will redirect the user to the return_url we specified in the init request
+      return new Response(
+        JSON.stringify({ success: true, message: 'Store connected successfully', storeName }),
+        { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+      )
     }
 
     // Unknown action
@@ -483,13 +472,12 @@ serve(async (req) => {
 
   } catch (error) {
     console.error('[WooCommerce] Error:', error)
-    const frontendUrl = Deno.env.get('FRONTEND_URL') || 'https://shopcall.ai'
-    return new Response(null, {
-      status: 302,
-      headers: {
-        ...corsHeaders,
-        'Location': `${frontendUrl}/webshops?error=internal_error`
-      }
-    })
+    return new Response(
+      JSON.stringify({
+        error: 'Internal server error',
+        message: error instanceof Error ? error.message : 'Unknown error'
+      }),
+      { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
+    )
   }
 })