Example #1
0
/**
 * 3-step function to Process & Save Transaction
 *
 * 1) Capture POST
 * 2) Create Charge using wp_stripe_charge()
 * 3) Store Transaction in Custom Post Type
 *
 * @since 1.0
 *
 */
function wp_stripe_charge_initiate()
{
    // Security Check
    if (!wp_verify_nonce($_POST['nonce'], 'wp-stripe-nonce')) {
        wp_die(__('Nonce verification failed!', 'wp-stripe'));
    }
    // Define/Extract Variables
    $public = sanitize_text_field($_POST['wp_stripe_public']);
    $name = sanitize_text_field($_POST['wp_stripe_name']);
    $email = sanitize_email($_POST['wp_stripe_email']);
    // Strip any comments from the amount
    $amount = str_replace(',', '', sanitize_text_field($_POST['wp_stripe_amount']));
    $amount = str_replace('$', '', $amount) * 100;
    $card = sanitize_text_field($_POST['stripeToken']);
    $widget_comment = '';
    if (empty($_POST['wp_stripe_comment'])) {
        $stripe_comment = __('E-mail: ', 'wp-stipe') . sanitize_text_field($_POST['wp_stripe_email']) . ' - ' . __('This transaction has no additional details', 'wp-stripe');
    } else {
        $stripe_comment = __('E-mail: ', 'wp-stipe') . sanitize_text_field($_POST['wp_stripe_email']) . ' - ' . sanitize_text_field($_POST['wp_stripe_comment']);
        $widget_comment = sanitize_text_field($_POST['wp_stripe_comment']);
    }
    // Create Charge
    try {
        $response = wp_stripe_charge($amount, $card, $name, $stripe_comment);
        $id = $response->id;
        $amount = $response->amount / 100;
        $currency = $response->currency;
        $created = $response->created;
        $live = $response->livemode;
        $paid = $response->paid;
        if (isset($response->fee)) {
            $fee = $response->fee;
        }
        $result = '<div class="wp-stripe-notification alert alert-success"> ' . sprintf(__('Success, you just transferred %s', 'wp-stripe'), '<span class="wp-stripe-currency">' . esc_html($currency) . '</span> ' . esc_html($amount)) . ' !</div>';
        // Save Charge
        if ($paid === true) {
            $post_id = wp_insert_post(array('post_type' => 'wp-stripe-trx', 'post_author' => 1, 'post_content' => $widget_comment, 'post_title' => $id, 'post_status' => 'publish'));
            // Define Livemode
            if ($live) {
                $live = 'LIVE';
            } else {
                $live = 'TEST';
            }
            // Define Public (for Widget)
            if ($public === 'public') {
                $public = 'YES';
            } else {
                $public = 'NO';
            }
            // Update Meta
            update_post_meta($post_id, 'wp-stripe-public', $public);
            update_post_meta($post_id, 'wp-stripe-name', $name);
            update_post_meta($post_id, 'wp-stripe-email', $email);
            update_post_meta($post_id, 'wp-stripe-live', $live);
            update_post_meta($post_id, 'wp-stripe-date', $created);
            update_post_meta($post_id, 'wp-stripe-amount', $amount);
            update_post_meta($post_id, 'wp-stripe-currency', strtoupper($currency));
            if (isset($fee)) {
                update_post_meta($post_id, 'wp-stripe-fee', $fee);
            }
            do_action('wp_stripe_post_successful_charge', $response, $email, $stripe_comment);
            // Update Project
            // wp_stripe_update_project_transactions( 'add', $project_id , $post_id );
        }
        // Error
    } catch (Exception $e) {
        $result = '<div class="wp-stripe-notification wp-stripe-failure">' . sprint_f(__('Oops, something went wrong (%s)', 'wp-stripe'), $e->getMessage()) . '</div>';
        do_action('wp_stripe_post_fail_charge', $email, $e->getMessage());
    }
    // Return Results to JS
    header('Content-Type: application/json');
    echo json_encode($result);
    exit;
}
/**
 * 3-step function to Process & Save Transaction
 *
 * 1) Capture POST
 * 2) Create Charge using wp_stripe_charge()
 * 3) Store Transaction in Custom Post Type
 *
 * @since 1.0
 *
 */
function wp_stripe_charge_initiate()
{
    if (isset($_POST['wp_stripe_form']) == '1') {
        // Define/Extract Variables
        $public = $_POST['wp_stripe_public'];
        $name = $_POST['wp_stripe_name'];
        $email = $_POST['wp_stripe_email'];
        $amount = $_POST['wp_stripe_amount'] * 100;
        $card = $_POST['stripeToken'];
        if (!$_POST['wp_stripe_comment']) {
            $comment = __('This transaction has no additional details', 'wp-stripe');
        } else {
            $comment = $_POST['wp_stripe_comment'];
        }
        // Create Charge
        try {
            $response = wp_stripe_charge($amount, $card, $name, $comment);
            $id = $response->id;
            $amount = $response->amount / 100;
            $currency = $response->currency;
            $created = $response->created;
            $live = $response->livemode;
            $paid = $response->paid;
            $fee = $response->fee;
            echo '<div class="wp-stripe-notification wp-stripe-success"> ' . __('Success, you just transferred ', 'wp-stripe') . '<span class="wp-stripe-currency">' . $currency . '</span> ' . $amount . ' !</div>';
            // Save Charge
            if ($paid == true) {
                $new_post = array('ID' => '', 'post_type' => 'wp-stripe-trx', 'post_author' => 1, 'post_content' => $comment, 'post_title' => $id, 'post_status' => 'publish');
                $post_id = wp_insert_post($new_post);
                // Define Livemode
                if ($live) {
                    $live = 'LIVE';
                } else {
                    $live = 'TEST';
                }
                // Define Public (for Widget)
                if ($public == 'public') {
                    $public = 'YES';
                } else {
                    $public = 'NO';
                }
                // Update Meta
                update_post_meta($post_id, 'wp-stripe-public', $public);
                update_post_meta($post_id, 'wp-stripe-name', $name);
                update_post_meta($post_id, 'wp-stripe-email', $email);
                update_post_meta($post_id, 'wp-stripe-live', $live);
                update_post_meta($post_id, 'wp-stripe-date', $created);
                update_post_meta($post_id, 'wp-stripe-amount', $amount);
                update_post_meta($post_id, 'wp-stripe-currency', strtoupper($currency));
                update_post_meta($post_id, 'wp-stripe-fee', $fee);
                // TODO Add Project or Plan Post ID
            }
            // Error
        } catch (Exception $e) {
            echo '<div class="wp-stripe-notification wp-stripe-failure">' . __('Oops, something went wrong', 'wp-stripe') . ' (' . $e->getMessage() . ')</div>';
        }
    }
}