/** * Shortcode to output a number box - [stripe_radio] * * @since 2.0.0 */ function stripe_radio($attr) { static $counter = 1; global $sc_script_options; $attr = shortcode_atts(array('id' => '', 'label' => '', 'default' => '', 'options' => '', 'is_quantity' => 'false', 'amounts' => '', 'is_amount' => 'false'), $attr, 'stripe_radio'); $id = $attr['id']; $label = $attr['label']; $default = $attr['default']; $options = $attr['options']; $is_quantity = $attr['is_quantity']; $amounts = $attr['amounts']; $is_amount = $attr['is_amount']; Shortcode_Tracker::add_new_shortcode('stripe_radio_' . $counter, 'stripe_radio', $attr, false); // Check for ID and if it doesn't exist then we will make our own if ($id == '') { $id = 'sc_cf_radio_' . $counter; } $options = explode(',', $options); if (!empty($amounts)) { $amounts = explode(',', str_replace(' ', '', $amounts)); if (count($options) != count($amounts)) { Shortcode_Tracker::update_error_count(); if (current_user_can('manage_options')) { Shortcode_Tracker::add_error_message('<h6>' . __('Your number of options and amounts are not equal.', 'sc') . '</h6>'); } return ''; } } if ($is_amount == 'true') { if (current_user_can('manage_options')) { echo '<h6>' . sprintf(__('The "is_amount" attribute is deprecated and will be removed in an upcoming release. Please use the new "amounts" attribute instead. %s', 'sc'), '<a href="' . SC_WEBSITE_BASE_URL . 'docs/shortcodes/stripe-custom-fields/" target="_blank">' . __('See Documentation', 'sc') . '</a>') . '</h6>'; } } $quantity_html = 'true' == $is_quantity ? 'data-sc-quanitity="true" ' : ''; $quantity_class = 'true' == $is_quantity ? ' sc-cf-quantity' : ''; $amount_class = !empty($amounts) || $is_amount == 'true' ? ' sc-cf-amount' : ''; $html = !empty($label) ? '<label>' . $label . '</label>' : ''; $html .= '<div class="sc-radio-group">'; $i = 1; foreach ($options as $option) { $option = trim($option); $value = $option; if (empty($default)) { $default = $option; } if ($is_amount == 'true') { $currency = strtoupper($sc_script_options['script']['currency']); $amount = sc_stripe_to_formatted_amount($option, $currency); if ($currency == 'USD') { $option_name = '$' . $amount; } else { $option_name = $amount . ' ' . $currency; } } else { if (!empty($amounts)) { $value = $amounts[$i - 1]; } } if ($default == $option && $is_quantity != 'true' && !empty($amounts)) { $sc_script_options['script']['amount'] = $value; } // Don't use built-in checked() function here for now since we need "checked" in double quotes. $html .= '<label title="' . esc_attr($option) . '">'; $html .= '<input type="radio" name="sc_form_field[' . esc_attr($id) . ']" value="' . (isset($option_name) ? $option_name : $option) . '" ' . 'data-sc-price="' . esc_attr($value) . '" ' . ($default == $option ? 'checked="checked"' : '') . ' class="' . esc_attr($id) . '_' . $i . $quantity_class . $amount_class . '" data-parsley-errors-container=".' . apply_filters('sc_form_group_class', 'sc-form-group') . '" ' . $quantity_html . '>'; $html .= '<span>' . (isset($option_name) ? $option_name : $option) . '</span>'; $html .= '</label>'; $i++; } $html .= '</div>'; //sc-radio-group $attr['currency'] = strtoupper($sc_script_options['script']['currency']); $args = $this->get_args($id, $attr, $counter); // Incrememnt static counter $counter++; $this->total_fields(); return '<div class="' . apply_filters('sc_form_group_class', 'sc-form-group') . '">' . apply_filters('sc_stripe_radio', $html, $args) . '</div>'; }
/** * Function to process the [stripe] shortcode * * @since 1.0.0 */ function sc_stripe_shortcode($attr, $content = null) { global $sc_options; static $uid = 1; extract(shortcode_atts(array('name' => !empty($sc_options['name']) ? $sc_options['name'] : get_bloginfo('title'), 'description' => '', 'amount' => '', 'image_url' => !empty($sc_options['image_url']) ? $sc_options['image_url'] : '', 'currency' => !empty($sc_options['currency']) ? $sc_options['currency'] : 'USD', 'checkout_button_label' => !empty($sc_options['checkout_button_label']) ? $sc_options['checkout_button_label'] : '', 'billing' => !empty($sc_options['billing']) ? 'true' : 'false', 'payment_button_label' => !empty($sc_options['payment_button_label']) ? $sc_options['payment_button_label'] : __('Pay with Card', 'sc'), 'enable_remember' => !empty($sc_options['enable_remember']) ? 'true' : 'false', 'success_redirect_url' => !empty($sc_options['success_redirect_url']) ? $sc_options['success_redirect_url'] : get_permalink(), 'failure_redirect_url' => !empty($sc_options['failure_redirect_url']) ? $sc_options['failure_redirect_url'] : get_permalink(), 'prefill_email' => 'false', 'verify_zip' => !empty($sc_options['verify_zip']) ? 'true' : 'false', 'test_mode' => 'false'), $attr, 'stripe')); // Check if in test mode or live mode if (!empty($sc_options['enable_live_key']) && $sc_options['enable_live_key'] == 1 && $test_mode != 'true') { $data_key = !empty($sc_options['live_publish_key']) ? $sc_options['live_publish_key'] : ''; if (empty($sc_options['live_secret_key'])) { $data_key = ''; } } else { $data_key = !empty($sc_options['test_publish_key']) ? $sc_options['test_publish_key'] : ''; if (empty($sc_options['test_secret_key'])) { $data_key = ''; } } if (empty($data_key)) { if (current_user_can('manage_options')) { return '<h6>' . __('You must enter your API keys before the Stripe button will show up here.', 'sc') . '</h6>'; } return ''; } if (!empty($prefill_email) && $prefill_email !== 'false') { // Get current logged in user email if (is_user_logged_in()) { $prefill_email = get_userdata(get_current_user_id())->user_email; } else { $prefill_email = 'false'; } } $html = '<form id="sc_checkout_form_' . $uid . '" method="POST" action="" data-sc-id="' . $uid . '" class="sc-checkout-form">'; $html .= '<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="' . $data_key . '" ' . (!empty($image_url) ? 'data-image="' . $image_url . '" ' : '') . (!empty($name) ? 'data-name="' . $name . '" ' : '') . (!empty($description) ? 'data-description="' . $description . '" ' : '') . (!empty($amount) ? 'data-amount="' . $amount . '" ' : '') . (!empty($currency) ? 'data-currency="' . $currency . '" ' : '') . (!empty($checkout_button_label) ? 'data-panel-label="' . $checkout_button_label . '" ' : '') . (!empty($verify_zip) ? 'data-zip-code="' . $verify_zip . '" ' : '') . (!empty($prefill_email) && 'false' != $prefill_email ? 'data-email="' . $prefill_email . '" ' : '') . (!empty($payment_button_label) ? 'data-label="' . $payment_button_label . '" ' : '') . (!empty($enable_remember) ? 'data-allow-remember-me="' . $enable_remember . '" ' : 'data-allow-remember-me="true" ') . (!empty($billing) ? 'data-billing-address="' . $billing . '" ' : 'data-billing-address="false" ') . '></script>'; $html .= '<input type="hidden" name="sc-name" value="' . esc_attr($name) . '" />'; $html .= '<input type="hidden" name="sc-description" value="' . esc_attr($description) . '" />'; $html .= '<input type="hidden" name="sc-amount" class="sc_amount" value="' . esc_attr($amount) . '" />'; $html .= '<input type="hidden" name="sc-redirect" value="' . esc_attr(!empty($success_redirect_url) ? $success_redirect_url : get_permalink()) . '" />'; $html .= '<input type="hidden" name="sc-redirect-fail" value="' . esc_attr(!empty($failure_redirect_url) ? $failure_redirect_url : get_permalink()) . '" />'; $html .= '<input type="hidden" name="sc-currency" value="' . esc_attr($currency) . '" />'; if ($test_mode == 'true') { $html .= '<input type="hidden" name="sc_test_mode" value="true" />'; } $html .= '</form>'; // Increment static uid counter $uid++; //Stripe minimum amount allowed. $stripe_minimum_amount = 50; if (empty($amount) || $amount < $stripe_minimum_amount || !isset($amount)) { if (current_user_can('manage_options')) { $html = '<h6>'; $html .= __('Stripe checkout requires an amount of ', 'sc') . $stripe_minimum_amount; $html .= ' (' . sc_stripe_to_formatted_amount($stripe_minimum_amount, $currency) . ' ' . $currency . ')'; $html .= __(' or larger.', 'sc'); $html .= '</h6>'; return $html; } return ''; } else { if (!isset($_GET['charge'])) { return $html; } } return ''; }
function sc_show_payment_details($content) { global $sc_options; // TODO $html out once finalized. $html = ''; $test_mode = isset($_GET['test_mode']) ? 'true' : 'false'; sc_set_stripe_key($test_mode); // Successful charge output. if (isset($_GET['charge']) && !isset($_GET['charge_failed'])) { if (empty($sc_options['disable_success_message'])) { $charge_id = esc_html($_GET['charge']); // https://stripe.com/docs/api/php#charges $charge_response = Stripe_Charge::retrieve($charge_id); $html = '<div class="sc-payment-details-wrap">'; $html .= '<p>' . __('Congratulations. Your payment went through!', 'sc') . '</p>' . "\n"; if (!empty($charge_response->description)) { $html .= '<p>' . __("Here's what you bought:", 'sc') . '</p>'; $html .= $charge_response->description . '<br>' . "\n"; } if (isset($_GET['store_name']) && !empty($_GET['store_name'])) { $html .= 'From: ' . esc_html($_GET['store_name']) . '<br/>' . "\n"; } $html .= '<br><strong>' . __('Total Paid: ', 'sc') . sc_stripe_to_formatted_amount($charge_response->amount, $charge_response->currency) . ' ' . strtoupper($charge_response->currency) . '</strong>' . "\n"; $html .= '<p>' . sprintf(__('Your transaction ID is: %s', 'sc'), $charge_id) . '</p>'; $html .= '</div>'; return apply_filters('sc_payment_details', $html, $charge_response) . $content; } else { return $content; } } elseif (isset($_GET['charge_failed'])) { // TODO Failed charge output. $html = '<div class="sc-payment-details-wrap sc-payment-details-error">'; $html .= __('Sorry, but your card was declined and your payment was not processed.', 'sc'); $html .= '<br><br>' . sprintf(__('Transaction ID: %s', 'sc'), esc_html($_GET['charge'])); $html .= '</div>'; return apply_filters('sc_payment_details_error', $html) . $content; } return $content; }