Advertisement
vapvarun

Hook to a specific function related to customer creation

May 17th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1.  
  2. /**
  3. * Fix for "Unable to create customer" error in EDD Stripe
  4. */
  5.  
  6. /**
  7. * Debug EDD Stripe customer creation issues
  8. * Uncomment this function temporarily to see what data is being sent to Stripe
  9. */
  10. /*
  11. function debug_edd_stripe_customer_creation($args) {
  12. // Log customer creation args for debugging
  13. error_log('EDD Stripe Customer Args: ' . print_r($args, true));
  14. return $args;
  15. }
  16. add_filter('edds_create_customer_args', 'debug_edd_stripe_customer_creation', 99);
  17. */
  18.  
  19. /**
  20. * Fix Stripe customer creation by ensuring valid data format
  21. *
  22. * @param array $args Arguments for customer creation
  23. * @return array Modified arguments
  24. */
  25. function fix_edd_stripe_customer_creation($args) {
  26. // Make sure we have array to work with
  27. if (!is_array($args)) {
  28. return $args;
  29. }
  30.  
  31. // CRITICAL FIX: Clean and validate required fields for Stripe customer creation
  32.  
  33. // 1. Email must be valid
  34. if (isset($args['email'])) {
  35. $args['email'] = sanitize_email($args['email']);
  36.  
  37. // If email is invalid after sanitization, remove it
  38. if (!is_email($args['email'])) {
  39. unset($args['email']);
  40. }
  41. }
  42.  
  43. // 2. Name must not be empty or contain invalid characters
  44. if (isset($args['name'])) {
  45. $args['name'] = sanitize_text_field($args['name']);
  46.  
  47. // If name is empty after sanitization, remove it
  48. if (empty($args['name'])) {
  49. unset($args['name']);
  50. }
  51. }
  52.  
  53. // 3. Address must be in correct format
  54. if (isset($args['address']) && is_array($args['address'])) {
  55. $clean_address = array();
  56.  
  57. // Only include valid address fields with proper keys
  58. $valid_keys = array('line1', 'line2', 'city', 'state', 'postal_code', 'country');
  59.  
  60. foreach ($valid_keys as $key) {
  61. if (isset($args['address'][$key])) {
  62. $clean_address[$key] = sanitize_text_field($args['address'][$key]);
  63.  
  64. // Remove empty values
  65. if (empty($clean_address[$key])) {
  66. unset($clean_address[$key]);
  67. }
  68. }
  69. }
  70.  
  71. // Only include address if we have at least one valid field
  72. if (!empty($clean_address)) {
  73. $args['address'] = $clean_address;
  74. } else {
  75. unset($args['address']);
  76. }
  77. } elseif (isset($args['address']) && !is_array($args['address'])) {
  78. // Remove address if it's not in array format
  79. unset($args['address']);
  80. }
  81.  
  82. // 4. Fix potential phone number format issues
  83. if (isset($args['phone'])) {
  84. // Remove all non-numeric characters except + (for international format)
  85. $phone = preg_replace('/[^0-9+]/', '', $args['phone']);
  86.  
  87. if (!empty($phone)) {
  88. $args['phone'] = $phone;
  89. } else {
  90. unset($args['phone']);
  91. }
  92. }
  93.  
  94. // 5. Make sure metadata is in correct format
  95. if (isset($args['metadata']) && !is_array($args['metadata'])) {
  96. unset($args['metadata']);
  97. } elseif (isset($args['metadata'])) {
  98. // Clean metadata values - only strings allowed
  99. foreach ($args['metadata'] as $key => $value) {
  100. if (!is_string($value) && !is_numeric($value)) {
  101. $args['metadata'][$key] = (string)$value;
  102. }
  103. }
  104. }
  105.  
  106. // 6. Make sure desc/description is a string
  107. if (isset($args['description']) && !is_string($args['description'])) {
  108. $args['description'] = (string)$args['description'];
  109. }
  110.  
  111. // 7. Remove any empty parameters
  112. foreach ($args as $key => $value) {
  113. if ($value === '' || $value === null) {
  114. unset($args[$key]);
  115. }
  116. }
  117.  
  118. return $args;
  119. }
  120. add_filter('edds_create_customer_args', 'fix_edd_stripe_customer_creation', 9999); // High priority to run last
  121.  
  122. /**
  123. * Fix payment intent creation to work with existing customers
  124. */
  125. function fix_edd_stripe_payment_intent($args) {
  126. // Make sure we have valid data
  127. if (!is_array($args)) {
  128. return $args;
  129. }
  130.  
  131. // Sometimes billing_details can cause issues if not properly formatted
  132. if (isset($args['billing_details'])) {
  133. // Ensure address is an array if present
  134. if (isset($args['billing_details']['address']) && !is_array($args['billing_details']['address'])) {
  135. unset($args['billing_details']['address']);
  136. }
  137.  
  138. // Clean up empty values from billing_details
  139. foreach ($args['billing_details'] as $key => $value) {
  140. if (empty($value)) {
  141. unset($args['billing_details'][$key]);
  142. }
  143. }
  144.  
  145. // If all billing details were removed, remove the entire property
  146. if (empty($args['billing_details'])) {
  147. unset($args['billing_details']);
  148. }
  149. }
  150.  
  151. // Same for shipping
  152. if (isset($args['shipping'])) {
  153. if (isset($args['shipping']['address']) && !is_array($args['shipping']['address'])) {
  154. unset($args['shipping']['address']);
  155. }
  156.  
  157. // If address was removed, remove shipping entirely
  158. if (!isset($args['shipping']['address']) || empty($args['shipping']['address'])) {
  159. unset($args['shipping']);
  160. }
  161. }
  162.  
  163. return $args;
  164. }
  165. add_filter('edds_create_payment_intent_args', 'fix_edd_stripe_payment_intent', 9999);
  166.  
  167. /**
  168. * Handle errors during customer creation to provide more insight
  169. */
  170. function fix_edd_stripe_handle_customer_error($error_data, $error_obj) {
  171. // Check if this is a customer creation error
  172. if (strpos($error_data['message'], 'Unable to create customer') !== false) {
  173. // Add additional information to help debugging
  174. error_log('EDD Stripe Customer Creation Error: ' . print_r($error_obj, true));
  175.  
  176. // Modify error message to be more helpful
  177. $error_data['message'] = 'Unable to create customer in Stripe. Please ensure your billing information is complete and valid.';
  178. }
  179.  
  180. return $error_data;
  181. }
  182. add_filter('edds_api_request_error', 'fix_edd_stripe_handle_customer_error', 10, 2);
  183.  
  184. /**
  185. * Last resort - fallback function for when regular customer creation fails
  186. * This uses a simplified approach to avoid common issues
  187. */
  188. function fix_edd_stripe_customer_creation_fallback($customer_id, $args, $error) {
  189. // Only run if customer creation failed
  190. if (!is_wp_error($customer_id) || $customer_id !== false) {
  191. return $customer_id;
  192. }
  193.  
  194. // Try to get the email from args
  195. $email = !empty($args['email']) ? $args['email'] : '';
  196.  
  197. // If no email, we can't create a customer
  198. if (empty($email)) {
  199. return $customer_id; // Return original error
  200. }
  201.  
  202. try {
  203. // Include mandatory Stripe files if not already included
  204. if (class_exists('\EDD\Vendors\Stripe\Customer') && function_exists('edds_api_request')) {
  205. // Create a minimal customer with just the required fields
  206. $min_args = array(
  207. 'email' => $email,
  208. 'metadata' => array(
  209. 'edd_customer_id' => isset($args['metadata']['edd_customer_id']) ? $args['metadata']['edd_customer_id'] : '0'
  210. )
  211. );
  212.  
  213. // Add name if available and valid
  214. if (!empty($args['name'])) {
  215. $min_args['name'] = sanitize_text_field($args['name']);
  216. }
  217.  
  218. // Create customer using the API
  219. $customer = edds_api_request('Customer', 'create', $min_args);
  220.  
  221. if (!empty($customer->id)) {
  222. // Log success
  223. error_log('EDD Stripe: Successfully created customer using fallback method: ' . $customer->id);
  224. return $customer->id;
  225. }
  226. }
  227. } catch (Exception $e) {
  228. // Log fallback error
  229. error_log('EDD Stripe Customer Fallback Error: ' . $e->getMessage());
  230. }
  231.  
  232. // If we get here, even fallback failed
  233. return $customer_id;
  234. }
  235.  
  236. // Hook to a specific function related to customer creation
  237. // Note: This is a hypothetical hook - you may need to find the right one in your EDD Stripe plugin
  238. // add_filter('edds_create_customer_result', 'fix_edd_stripe_customer_creation_fallback', 999, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement