vapvarun icon

Hook to a specific function related to customer creation

vapvarun | PRO | 05/17/25 05:33:00 PM UTC | 0 ⭐ | 306 👁️ | Never ⏰ | []
text |

8.4 KB

|

None

|

0 👍

/

0 👎

 /**
 * Fix for "Unable to create customer" error in EDD Stripe
 */
 /**
 * Debug EDD Stripe customer creation issues
 * Uncomment this function temporarily to see what data is being sent to Stripe
 */
/*
function debug_edd_stripe_customer_creation($args) {
    // Log customer creation args for debugging
    error_log('EDD Stripe Customer Args: ' . print_r($args, true));
    return $args;
}
add_filter('edds_create_customer_args', 'debug_edd_stripe_customer_creation', 99);
*/
 /**
 * Fix Stripe customer creation by ensuring valid data format
 *
 * @param array $args Arguments for customer creation
 * @return array Modified arguments
 */
function fix_edd_stripe_customer_creation($args) {
    // Make sure we have array to work with
    if (!is_array($args)) {
        return $args;
    }
     // CRITICAL FIX: Clean and validate required fields for Stripe customer creation
     // 1. Email must be valid
    if (isset($args['email'])) {
        $args['email'] = sanitize_email($args['email']);
         // If email is invalid after sanitization, remove it
        if (!is_email($args['email'])) {
            unset($args['email']);
        }
    }
     // 2. Name must not be empty or contain invalid characters
    if (isset($args['name'])) {
        $args['name'] = sanitize_text_field($args['name']);
         // If name is empty after sanitization, remove it
        if (empty($args['name'])) {
            unset($args['name']);
        }
    }
     // 3. Address must be in correct format
    if (isset($args['address']) && is_array($args['address'])) {
        $clean_address = array();
         // Only include valid address fields with proper keys
        $valid_keys = array('line1', 'line2', 'city', 'state', 'postal_code', 'country');
         foreach ($valid_keys as $key) {
            if (isset($args['address'][$key])) {
                $clean_address[$key] = sanitize_text_field($args['address'][$key]);
                 // Remove empty values
                if (empty($clean_address[$key])) {
                    unset($clean_address[$key]);
                }
            }
        }
         // Only include address if we have at least one valid field
        if (!empty($clean_address)) {
            $args['address'] = $clean_address;
        } else {
            unset($args['address']);
        }
    } elseif (isset($args['address']) && !is_array($args['address'])) {
        // Remove address if it's not in array format
        unset($args['address']);
    }
     // 4. Fix potential phone number format issues
    if (isset($args['phone'])) {
        // Remove all non-numeric characters except + (for international format)
        $phone = preg_replace('/[^0-9+]/', '', $args['phone']);
         if (!empty($phone)) {
            $args['phone'] = $phone;
        } else {
            unset($args['phone']);
        }
    }
     // 5. Make sure metadata is in correct format
    if (isset($args['metadata']) && !is_array($args['metadata'])) {
        unset($args['metadata']);
    } elseif (isset($args['metadata'])) {
        // Clean metadata values - only strings allowed
        foreach ($args['metadata'] as $key => $value) {
            if (!is_string($value) && !is_numeric($value)) {
                $args['metadata'][$key] = (string)$value;
            }
        }
    }
     // 6. Make sure desc/description is a string
    if (isset($args['description']) && !is_string($args['description'])) {
        $args['description'] = (string)$args['description'];
    }
     // 7. Remove any empty parameters
    foreach ($args as $key => $value) {
        if ($value === '' || $value === null) {
            unset($args[$key]);
        }
    }
     return $args;
}
add_filter('edds_create_customer_args', 'fix_edd_stripe_customer_creation', 9999); // High priority to run last
 /**
 * Fix payment intent creation to work with existing customers
 */
function fix_edd_stripe_payment_intent($args) {
    // Make sure we have valid data
    if (!is_array($args)) {
        return $args;
    }
     // Sometimes billing_details can cause issues if not properly formatted
    if (isset($args['billing_details'])) {
        // Ensure address is an array if present
        if (isset($args['billing_details']['address']) && !is_array($args['billing_details']['address'])) {
            unset($args['billing_details']['address']);
        }
         // Clean up empty values from billing_details
        foreach ($args['billing_details'] as $key => $value) {
            if (empty($value)) {
                unset($args['billing_details'][$key]);
            }
        }
         // If all billing details were removed, remove the entire property
        if (empty($args['billing_details'])) {
            unset($args['billing_details']);
        }
    }
     // Same for shipping
    if (isset($args['shipping'])) {
        if (isset($args['shipping']['address']) && !is_array($args['shipping']['address'])) {
            unset($args['shipping']['address']);
        }
         // If address was removed, remove shipping entirely
        if (!isset($args['shipping']['address']) || empty($args['shipping']['address'])) {
            unset($args['shipping']);
        }
    }
     return $args;
}
add_filter('edds_create_payment_intent_args', 'fix_edd_stripe_payment_intent', 9999);
 /**
 * Handle errors during customer creation to provide more insight
 */
function fix_edd_stripe_handle_customer_error($error_data, $error_obj) {
    // Check if this is a customer creation error
    if (strpos($error_data['message'], 'Unable to create customer') !== false) {
        // Add additional information to help debugging
        error_log('EDD Stripe Customer Creation Error: ' . print_r($error_obj, true));
         // Modify error message to be more helpful
        $error_data['message'] = 'Unable to create customer in Stripe. Please ensure your billing information is complete and valid.';
    }
     return $error_data;
}
add_filter('edds_api_request_error', 'fix_edd_stripe_handle_customer_error', 10, 2);
 /**
 * Last resort - fallback function for when regular customer creation fails
 * This uses a simplified approach to avoid common issues
 */
function fix_edd_stripe_customer_creation_fallback($customer_id, $args, $error) {
    // Only run if customer creation failed
    if (!is_wp_error($customer_id) || $customer_id !== false) {
        return $customer_id;
    }
     // Try to get the email from args
    $email = !empty($args['email']) ? $args['email'] : '';
     // If no email, we can't create a customer
    if (empty($email)) {
        return $customer_id; // Return original error
    }
     try {
        // Include mandatory Stripe files if not already included
        if (class_exists('\EDD\Vendors\Stripe\Customer') && function_exists('edds_api_request')) {
            // Create a minimal customer with just the required fields
            $min_args = array(
                'email' => $email,
                'metadata' => array(
                    'edd_customer_id' => isset($args['metadata']['edd_customer_id']) ? $args['metadata']['edd_customer_id'] : '0'
                )
            );
             // Add name if available and valid
            if (!empty($args['name'])) {
                $min_args['name'] = sanitize_text_field($args['name']);
            }
             // Create customer using the API
            $customer = edds_api_request('Customer', 'create', $min_args);
             if (!empty($customer->id)) {
                // Log success
                error_log('EDD Stripe: Successfully created customer using fallback method: ' . $customer->id);
                return $customer->id;
            }
        }
    } catch (Exception $e) {
        // Log fallback error
        error_log('EDD Stripe Customer Fallback Error: ' . $e->getMessage());
    }
     // If we get here, even fallback failed
    return $customer_id;
}
 // Hook to a specific function related to customer creation
// Note: This is a hypothetical hook - you may need to find the right one in your EDD Stripe plugin
// add_filter('edds_create_customer_result', 'fix_edd_stripe_customer_creation_fallback', 999, 3);

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎