/** * Print Errors * * Prints all stored errors. For use during checkout. * If errors exist, they are returned. * * @access private * @since 1.0 * @return void */ function edd_print_errors() { $errors = edd_get_errors(); if ($errors) { echo '<div class="edd_errors">'; // Loop error codes and display errors foreach ($errors as $error_id => $error) { echo '<p class="edd_error" id="edd_error_' . $error_id . '"><strong>' . __('Error', 'edd') . '</strong>: ' . $error . '</p>'; } echo '</div>'; edd_clear_errors(); } }
/** * Print Errors * * Prints all stored errors. For use during checkout. * If errors exist, they are returned. * * @since 1.0 * @uses edd_get_errors() * @uses edd_clear_errors() * @return void */ function edd_print_errors() { $errors = edd_get_errors(); if ($errors) { $classes = apply_filters('edd_error_class', array('edd_errors', 'edd-alert', 'edd-alert-error')); echo '<div class="' . implode(' ', $classes) . '">'; // Loop error codes and display errors foreach ($errors as $error_id => $error) { echo '<p class="edd_error" id="edd_error_' . $error_id . '"><strong>' . __('Error', 'edd') . '</strong>: ' . $error . '</p>'; } echo '</div>'; edd_clear_errors(); } }
/** * Removes a Download from the Cart * * @since 1.0 * @param int $cart_key the cart key to remove. This key is the numerical index of the item contained within the cart array. * @return array Updated cart items */ function edd_remove_from_cart($cart_key) { $cart = edd_get_cart_contents(); do_action('edd_pre_remove_from_cart', $cart_key); if (!is_array($cart)) { return true; // Empty cart } else { $item_id = isset($cart[$cart_key]['id']) ? $cart[$cart_key]['id'] : null; unset($cart[$cart_key]); } EDD()->session->set('edd_cart', $cart); do_action('edd_post_remove_from_cart', $cart_key, $item_id); // Clear all the checkout errors, if any edd_clear_errors(); return $cart; // The updated cart items }
/** * Remove From Cart * * Removes a download from the shopping cart. * Uses edd_get_cart_contents(). * * @access public * @since 1.0 * @param $cart_key INT the cart key to remove * @return array - of updated cart items */ function edd_remove_from_cart($cart_key) { $cart = edd_get_cart_contents(); do_action('edd_pre_remove_from_cart', $cart_key); if (!is_array($cart)) { return true; // empty cart } else { unset($cart[$cart_key]); } $_SESSION['edd_cart'] = $cart; do_action('edd_post_remove_from_cart', $cart_key); // clear all the checkout errors, if any edd_clear_errors(); return $cart; // the updated cart items }
/** * Process stripe checkout submission * * @access public * @since 1.0 * @return void */ function edds_process_stripe_payment($purchase_data) { global $edd_options; if (!class_exists('Stripe')) { require_once EDDS_PLUGIN_DIR . '/Stripe/Stripe.php'; } if (edd_is_test_mode()) { $secret_key = trim($edd_options['test_secret_key']); } else { $secret_key = trim($edd_options['live_secret_key']); } $purchase_summary = edd_get_purchase_summary($purchase_data, false); // make sure we don't have any left over errors present edd_clear_errors(); if (!isset($_POST['edd_stripe_token'])) { // check for fallback mode if (isset($edd_options['stripe_js_fallback'])) { if (!isset($_POST['card_name']) || strlen(trim($_POST['card_name'])) == 0) { edd_set_error('no_card_name', __('Please enter a name for the credit card.', 'edds')); } if (!isset($_POST['card_number']) || strlen(trim($_POST['card_number'])) == 0) { edd_set_error('no_card_number', __('Please enter a credit card number.', 'edds')); } if (!isset($_POST['card_cvc']) || strlen(trim($_POST['card_cvc'])) == 0) { edd_set_error('no_card_cvc', __('Please enter a CVC/CVV for the credit card.', 'edds')); } if (!isset($_POST['card_exp_month']) || strlen(trim($_POST['card_exp_month'])) == 0) { edd_set_error('no_card_exp_month', __('Please enter a expiration month.', 'edds')); } if (!isset($_POST['card_exp_year']) || strlen(trim($_POST['card_exp_year'])) == 0) { edd_set_error('no_card_exp_year', __('Please enter a expiration year.', 'edds')); } $card_data = array('number' => $purchase_data['card_info']['card_number'], 'name' => $purchase_data['card_info']['card_name'], 'exp_month' => $purchase_data['card_info']['card_exp_month'], 'exp_year' => $purchase_data['card_info']['card_exp_year'], 'cvc' => $purchase_data['card_info']['card_cvc'], 'address_line1' => $purchase_data['card_info']['card_address'], 'address_line2' => $purchase_data['card_info']['card_address_2'], 'address_city' => $purchase_data['card_info']['card_city'], 'address_zip' => $purchase_data['card_info']['card_zip'], 'address_state' => $purchase_data['card_info']['card_state'], 'address_country' => $purchase_data['card_info']['card_country']); } else { // no Stripe token edd_set_error('no_token', __('Missing Stripe token. Please contact support.', 'edds')); edd_record_gateway_error(__('Missing Stripe Token', 'edds'), __('A Stripe token failed to be generated. Please check Stripe logs for more information', ' edds')); } } else { $card_data = $_POST['edd_stripe_token']; } $errors = edd_get_errors(); if (!$errors) { try { Stripe::setApiKey($secret_key); // setup the payment details $payment_data = array('price' => $purchase_data['price'], 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => edd_get_currency(), 'downloads' => $purchase_data['downloads'], 'cart_details' => $purchase_data['cart_details'], 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'stripe'); $customer_exists = false; if (is_user_logged_in()) { $user = get_user_by('email', $purchase_data['user_email']); if ($user) { $customer_id = get_user_meta($user->ID, edd_stripe_get_customer_key(), true); if ($customer_id) { $customer_exists = true; try { // Update the customer to ensure their card data is up to date $cu = Stripe_Customer::retrieve($customer_id); if (isset($cu->deleted) && $cu->deleted) { // This customer was deleted $customer_exists = false; } else { $cu->card = $card_data; $cu->save(); } // No customer found } catch (Exception $e) { $customer_exists = false; } } } } if (!$customer_exists) { // Create a customer first so we can retrieve them later for future payments $customer = Stripe_Customer::create(array('description' => $purchase_data['user_email'], 'email' => $purchase_data['user_email'], 'card' => $card_data)); $customer_id = is_array($customer) ? $customer['id'] : $customer->id; if (is_user_logged_in()) { update_user_meta($user->ID, edd_stripe_get_customer_key(), $customer_id); } } if (edds_is_recurring_purchase($purchase_data) && (!empty($customer) || $customer_exists)) { // Process a recurring subscription purchase $cu = Stripe_Customer::retrieve($customer_id); /********************************************************** * Taxes, fees, and discounts have to be handled differently * with recurring subscriptions, so each is added as an * invoice item and then charged as one time items **********************************************************/ $invoice_items = array(); $needs_invoiced = false; if ($purchase_data['tax'] > 0 && !edd_prices_include_tax()) { if (edds_is_zero_decimal_currency()) { $tax = $purchase_data['tax']; } else { $tax = $purchase_data['tax'] * 100; } $invoice = Stripe_InvoiceItem::create(array('customer' => $customer_id, 'amount' => $tax, 'currency' => edd_get_currency(), 'description' => sprintf(__('Sales tax for order %s', 'edds'), $purchase_data['purchase_key']))); if (!empty($invoice->id)) { $invoice_items[] = $invoice->id; } $needs_invoiced = true; } if (!empty($purchase_data['fees'])) { foreach ($purchase_data['fees'] as $fee) { if (edds_is_zero_decimal_currency()) { $fee_amount = $fee['amount']; } else { $fee_amount = $fee['amount'] * 100; } $invoice = Stripe_InvoiceItem::create(array('customer' => $customer_id, 'amount' => $fee_amount, 'currency' => edd_get_currency(), 'description' => $fee['label'])); if (!empty($invoice->id)) { $invoice_items[] = $invoice->id; } } $needs_invoiced = true; } if ($purchase_data['discount'] > 0) { if (edds_is_zero_decimal_currency()) { $discount_amount = $purchase_data['discount']; } else { $discount_amount = $purchase_data['discount'] * 100; } $invoice = Stripe_InvoiceItem::create(array('customer' => $customer_id, 'amount' => $discount_amount * -1, 'currency' => edd_get_currency(), 'description' => $purchase_data['user_info']['discount'])); if (!empty($invoice->id)) { $invoice_items[] = $invoice->id; } $needs_invoiced = true; } try { $plan_id = edds_get_plan_id($purchase_data); // record the pending payment $payment = edd_insert_payment($payment_data); set_transient('_edd_recurring_payment_' . $payment, '1', DAY_IN_SECONDS); // Store the parent payment ID in the user meta EDD_Recurring_Customer::set_customer_payment_id($user->ID, $payment); // Update the customer's subscription in Stripe $customer_response = $cu->updateSubscription(array('plan' => $plan_id)); // Set user as subscriber EDD_Recurring_Customer::set_as_subscriber($user->ID); // store the customer recurring ID EDD_Recurring_Customer::set_customer_id($user->ID, $customer_id); // Set the customer status EDD_Recurring_Customer::set_customer_status($user->ID, 'active'); // Calculate the customer's new expiration date $new_expiration = EDD_Recurring_Customer::calc_user_expiration($user->ID, $payment); // Set the customer's new expiration date EDD_Recurring_Customer::set_customer_expiration($user->ID, $new_expiration); } catch (Stripe_CardError $e) { $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('payment_error', $err['message']); } else { edd_set_error('payment_error', __('There was an error processing your payment, please ensure you have entered your card number correctly.', 'edds')); } edd_record_gateway_error(__('Stripe Error', 'edds'), sprintf(__('There was an error while processing a Stripe payment. Payment data: %s', ' edds'), json_encode($err)), 0); } catch (Stripe_ApiConnectionError $e) { $body = $e->getJsonBody(); $err = $body['error']; edd_set_error('payment_error', __('There was an error processing your payment (Stripe\'s API is down), please try again', 'edds')); edd_record_gateway_error(__('Stripe Error', 'edds'), sprintf(__('There was an error processing your payment (Stripe\'s API was down). Error: %s', 'edds'), json_encode($err['message'])), 0); } catch (Stripe_InvalidRequestError $e) { $body = $e->getJsonBody(); $err = $body['error']; // Bad Request of some sort. Maybe Christoff was here ;) if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('request_error', sprintf(__('The Stripe API request was invalid, please try again. Error: %s', 'edds'), json_encode($err['message']))); } } catch (Stripe_ApiError $e) { $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('request_error', __('The Stripe API request was invalid, please try again', 'edds')); } edd_record_gateway_error(__('Stripe Error', 'edds'), sprintf(__('There was an error with Stripe\'s API: ', 'edds'), json_encode($err['message'])), 0); } catch (Stripe_AuthenticationError $e) { $body = $e->getJsonBody(); $err = $body['error']; // Authentication error. Stripe keys in settings are bad. if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('The API keys entered in settings are incorrect', 'edds')); } } catch (Stripe_Error $e) { $body = $e->getJsonBody(); $err = $body['error']; // generic stripe error if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('Something went wrong.', 'edds')); } } catch (Exception $e) { // some sort of other error $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('Something went wrong.', 'edds')); } } if (!empty($err)) { // Delete any invoice items we created for fees, taxes, and other foreach ($invoice_items as $invoice) { $ii = Stripe_InvoiceItem::retrieve($invoice); $ii->delete(); } edd_send_back_to_checkout('?payment-mode=stripe'); } } elseif (!empty($customer) || $customer_exists) { // Process a normal one-time charge purchase if (!isset($edd_options['stripe_preapprove_only'])) { if (edds_is_zero_decimal_currency()) { $amount = $purchase_data['price']; } else { $amount = $purchase_data['price'] * 100; } $charge = Stripe_Charge::create(array("amount" => $amount, "currency" => edd_get_currency(), "customer" => $customer_id, "description" => html_entity_decode($purchase_summary, ENT_COMPAT, 'UTF-8'), 'statement_description' => substr($purchase_summary, 0, 15), 'metadata' => array('email' => $purchase_data['user_info']['email']))); } // record the pending payment $payment = edd_insert_payment($payment_data); } else { edd_record_gateway_error(__('Customer Creation Failed', 'edds'), sprintf(__('Customer creation failed while processing a payment. Payment Data: %s', ' edds'), json_encode($payment_data)), $payment); } if ($payment && (!empty($customer_id) || !empty($charge))) { if (!empty($needs_invoiced)) { try { // Create the invoice containing taxes / discounts / fees $invoice = Stripe_Invoice::create(array('customer' => $customer_id)); $invoice = $invoice->pay(); } catch (Exception $e) { // If there is nothing to pay, it just means the invoice item was taken care of with the subscription payment } } if (isset($edd_options['stripe_preapprove_only'])) { edd_update_payment_status($payment, 'preapproval'); add_post_meta($payment, '_edds_stripe_customer_id', $customer_id); } else { edd_update_payment_status($payment, 'publish'); } // You should be using Stripe's API here to retrieve the invoice then confirming it's been paid if (!empty($charge)) { edd_insert_payment_note($payment, 'Stripe Charge ID: ' . $charge->id); if (function_exists('edd_set_payment_transaction_id')) { edd_set_payment_transaction_id($payment, $charge->id); } } elseif (!empty($customer_id)) { edd_insert_payment_note($payment, 'Stripe Customer ID: ' . $customer_id); } edd_empty_cart(); edd_send_to_success_page(); } else { edd_set_error('payment_not_recorded', __('Your payment could not be recorded, please contact the site administrator.', 'edds')); // if errors are present, send the user back to the purchase page so they can be corrected edd_send_back_to_checkout('?payment-mode=stripe'); } } catch (Stripe_CardError $e) { $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('payment_error', $err['message']); } else { edd_set_error('payment_error', __('There was an error processing your payment, please ensure you have entered your card number correctly.', 'edds')); } edd_record_gateway_error(__('Stripe Error', 'edds'), sprintf(__('There was an error while processing a Stripe payment. Payment data: %s', ' edds'), json_encode($err)), 0); edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Stripe_ApiConnectionError $e) { $body = $e->getJsonBody(); $err = $body['error']; edd_set_error('payment_error', __('There was an error processing your payment (Stripe\'s API is down), please try again', 'edds')); edd_record_gateway_error(__('Stripe Error', 'edds'), sprintf(__('There was an error processing your payment (Stripe\'s API was down). Error: %s', 'edds'), json_encode($err['message'])), 0); edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Stripe_InvalidRequestError $e) { $body = $e->getJsonBody(); $err = $body['error']; // Bad Request of some sort. Maybe Christoff was here ;) if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('request_error', __('The Stripe API request was invalid, please try again', 'edds')); } edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Stripe_ApiError $e) { $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('request_error', __('The Stripe API request was invalid, please try again', 'edds')); } edd_set_error('request_error', sprintf(__('The Stripe API request was invalid, please try again. Error: %s', 'edds'), json_encode($err['message']))); edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Stripe_AuthenticationError $e) { $body = $e->getJsonBody(); $err = $body['error']; // Authentication error. Stripe keys in settings are bad. if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('The API keys entered in settings are incorrect', 'edds')); } edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Stripe_Error $e) { $body = $e->getJsonBody(); $err = $body['error']; // generic stripe error if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('Something went wrong.', 'edds')); } edd_send_back_to_checkout('?payment-mode=stripe'); } catch (Exception $e) { // some sort of other error $body = $e->getJsonBody(); $err = $body['error']; if (isset($err['message'])) { edd_set_error('request_error', $err['message']); } else { edd_set_error('api_error', __('Something went wrong.', 'edds')); } edd_send_back_to_checkout('?payment-mode=stripe'); } } else { edd_send_back_to_checkout('?payment-mode=stripe'); } }
function edd_downloads_print_errors_clear() { edd_downloads_print_errors(); edd_clear_errors(); }