/**
  * Create a simple donation payment.
  *
  * @since 1.0
  */
 public static function create_simple_payment()
 {
     global $give_options;
     // Enable a few options
     $give_options['enable_sequential'] = '1';
     $give_options['sequential_prefix'] = 'GIVE-';
     update_option('give_settings', $give_options);
     $simple_form = Give_Helper_Form::create_simple_form();
     $multilevel_form = Give_Helper_Form::create_multilevel_form();
     /** Generate some donations */
     $user = get_userdata(1);
     $user_info = array('id' => $user->ID, 'email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     $total = 0;
     $simple_price = get_post_meta($simple_form->ID, 'give_price', true);
     $variable_prices = get_post_meta($multilevel_form->ID, 'give_variable_prices', true);
     $variable_item_price = $variable_prices[1]['amount'];
     // == $100
     $total += $variable_item_price + $simple_price;
     $purchase_data = array('price' => number_format((double) $total, 2), 'give_form_title' => 'Test Donation', 'give_form_id' => $simple_form->ID, 'date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user_info['email'], 'user_info' => $user_info, 'currency' => 'USD', 'status' => 'pending');
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     $_SERVER['SERVER_NAME'] = 'give_virtual';
     $payment_id = give_insert_payment($purchase_data);
     $key = $purchase_data['purchase_key'];
     $transaction_id = 'FIR3SID3';
     give_set_payment_transaction_id($payment_id, $transaction_id);
     give_insert_payment_note($payment_id, sprintf(__('PayPal Transaction ID: %s', 'give'), $transaction_id));
     return $payment_id;
 }
Example #2
0
 /**
  * Set it up
  */
 public function setUp()
 {
     parent::setUp();
     //Create a Donation Form
     $this->_post_id = $this->factory->post->create(array('post_title' => 'Test Form', 'post_type' => 'give_forms', 'post_status' => 'publish'));
     $_multi_level_donations = array(array('_give_id' => array('level_id' => '1'), '_give_amount' => '10.00', '_give_text' => 'Basic Level'), array('_give_id' => array('level_id' => '2'), '_give_amount' => '20.00', '_give_text' => 'Intermediate Level'), array('_give_id' => array('level_id' => '3'), '_give_amount' => '40.00', '_give_text' => 'Advanced Level'));
     $meta = array('give_price' => '0.00', '_give_price_option' => 'multi', '_give_price_options_mode' => 'on', '_give_donation_levels' => array_values($_multi_level_donations), 'give_product_notes' => 'Donation Notes', '_give_product_type' => 'default');
     foreach ($meta as $key => $value) {
         update_post_meta($this->_post_id, $key, $value);
     }
     //Generate Donations
     $this->_user_id = $this->factory->user->create(array('role' => 'administrator'));
     $user = get_userdata($this->_user_id);
     $user_info = array('id' => $user->ID, 'email' => '*****@*****.**', 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     $donation_details = array(array('id' => $this->_post_id, 'options' => array('price_id' => 1)));
     $total = 0;
     $prices = get_post_meta($donation_details[0]['id'], '_give_donation_levels', true);
     $item_price = $prices[1]['_give_amount'];
     $total += $item_price;
     $purchase_data = array('price' => number_format((double) $total, 2), 'give_form_title' => get_the_title($this->_post_id), 'give_form_id' => $this->_post_id, 'date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user_info['email'], 'user_info' => $user_info, 'currency' => 'USD', 'status' => 'pending', 'gateway' => 'manual');
     $_SERVER['REMOTE_ADDR'] = '10.0.0.0';
     $_SERVER['SERVER_NAME'] = 'give_virtual';
     $payment_id = give_insert_payment($purchase_data);
     give_update_payment_status($payment_id, 'complete');
 }
Example #3
0
/**
 * Process PayPal Purchase
 *
 * @since 1.0
 *
 * @param array $purchase_data Purchase Data
 *
 * @return void
 */
function give_process_paypal_purchase($purchase_data)
{
    if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'give-gateway')) {
        wp_die(__('Nonce verification has failed', 'give'), __('Error', 'give'), array('response' => 403));
    }
    // Collect payment data
    $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => intval($purchase_data['post_data']['give-form-id']), 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'paypal');
    // Record the pending payment
    $payment = give_insert_payment($payment_data);
    // Check payment
    if (!$payment) {
        // Record the error
        give_record_gateway_error(__('Payment Error', 'give'), sprintf(__('Payment creation failed before sending buyer to PayPal. Payment data: %s', 'give'), json_encode($payment_data)), $payment);
        // Problems? send back
        give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
    } else {
        // Only send to PayPal if the pending payment is created successfully
        $listener_url = add_query_arg('give-listener', 'IPN', home_url('index.php'));
        // Get the success url
        $return_url = add_query_arg(array('payment-confirmation' => 'paypal', 'payment-id' => $payment), get_permalink(give_get_option('success_page')));
        // Get the PayPal redirect uri
        $paypal_redirect = trailingslashit(give_get_paypal_redirect()) . '?';
        //Item name - pass level name if variable priced
        $item_name = $purchase_data['post_data']['give-form-title'];
        if (give_has_variable_prices($purchase_data['post_data']['give-form-id']) && isset($purchase_data['post_data']['give-price-id'])) {
            $item_price_level_text = give_get_price_option_name($purchase_data['post_data']['give-form-id'], $purchase_data['post_data']['give-price-id']);
            //Is there any donation level text?
            if (!empty($item_price_level_text)) {
                $item_name .= ' - ' . $item_price_level_text;
            }
        }
        // Setup PayPal arguments
        $paypal_args = array('business' => give_get_option('paypal_email', false), 'email' => $purchase_data['user_email'], 'invoice' => $purchase_data['purchase_key'], 'amount' => $purchase_data['price'], 'item_name' => $item_name, 'no_shipping' => '1', 'shipping' => '0', 'no_note' => '1', 'currency_code' => give_get_currency(), 'charset' => get_bloginfo('charset'), 'custom' => $payment, 'rm' => '2', 'return' => $return_url, 'cancel_return' => give_get_failed_transaction_uri('?payment-id=' . $payment), 'notify_url' => $listener_url, 'page_style' => give_get_paypal_page_style(), 'cbt' => get_bloginfo('name'), 'bn' => 'WordImpress_Donate_Give_US');
        if (!empty($purchase_data['user_info']['address'])) {
            $paypal_args['address1'] = $purchase_data['user_info']['address']['line1'];
            $paypal_args['address2'] = $purchase_data['user_info']['address']['line2'];
            $paypal_args['city'] = $purchase_data['user_info']['address']['city'];
            $paypal_args['country'] = $purchase_data['user_info']['address']['country'];
        }
        if (give_get_option('paypal_button_type') === 'standard') {
            $paypal_extra_args = array('cmd' => '_xclick');
        } else {
            $paypal_extra_args = array('cmd' => '_donations');
        }
        $paypal_args = array_merge($paypal_extra_args, $paypal_args);
        $paypal_args = apply_filters('give_paypal_redirect_args', $paypal_args, $purchase_data);
        // Build query
        $paypal_redirect .= http_build_query($paypal_args);
        // Fix for some sites that encode the entities
        $paypal_redirect = str_replace('&', '&', $paypal_redirect);
        // Redirect to PayPal
        wp_redirect($paypal_redirect);
        exit;
    }
}
Example #4
0
/**
 * Processes the purchase data and uses the Manual Payment gateway to record
 * the transaction in the Purchase History
 *
 * @since 1.0
 *
 * @param array $purchase_data Purchase Data
 *
 * @return void
 */
function give_manual_payment($purchase_data)
{
    if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'give-gateway')) {
        wp_die(esc_html__('Nonce verification has failed', 'give'), esc_html__('Error', 'give'), array('response' => 403));
    }
    //Create payment_data array
    $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => intval($purchase_data['post_data']['give-form-id']), 'give_price_id' => isset($purchase_data['post_data']['give-price-id']) ? $purchase_data['post_data']['give-price-id'] : '', 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending');
    // Record the pending payment
    $payment = give_insert_payment($payment_data);
    if ($payment) {
        give_update_payment_status($payment, 'publish');
        give_send_to_success_page();
    } else {
        give_record_gateway_error(esc_html__('Payment Error', 'give'), sprintf(esc_html__('The payment creation failed while processing a manual (free or test) donation. Payment data: %s', 'give'), json_encode($payment_data)), $payment);
        // If errors are present, send the user back to the purchase page so they can be corrected
        give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
    }
}
Example #5
0
 public function setUp()
 {
     parent::setUp();
     global $wp_rewrite, $wp_query;
     $GLOBALS['wp_rewrite']->init();
     flush_rewrite_rules(false);
     $this->_api = new Give_API();
     $roles = new Give_Roles();
     $roles->add_roles();
     $roles->add_caps();
     $this->_api->add_endpoint($wp_rewrite);
     $this->_rewrite = $wp_rewrite;
     $this->_query = $wp_query;
     //Create a Donation Form
     $post_id = $this->factory->post->create(array('post_title' => 'Test Form', 'post_type' => 'give_forms', 'post_status' => 'publish'));
     $this->_user_id = $this->factory->user->create(array('role' => 'administrator'));
     wp_set_current_user($this->_user_id);
     $_multi_level_donations = array(array('_give_id' => array('level_id' => '1'), '_give_amount' => '10.00', '_give_text' => 'Basic Level'), array('_give_id' => array('level_id' => '2'), '_give_amount' => '20.00', '_give_text' => 'Intermediate Level'), array('_give_id' => array('level_id' => '3'), '_give_amount' => '40.00', '_give_text' => 'Advanced Level'));
     $meta = array('give_price' => '0.00', '_give_price_option' => 'multi', '_give_price_options_mode' => 'on', '_give_donation_levels' => array_values($_multi_level_donations), '_give_form_earnings' => 120, '_give_form_sales' => 59);
     foreach ($meta as $key => $value) {
         update_post_meta($post_id, $key, $value);
     }
     $this->_post = get_post($post_id);
     $user = get_userdata(1);
     $user_info = array('id' => $user->ID, 'email' => '*****@*****.**', 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     $prices = get_post_meta($post_id, '_give_donation_levels', true);
     $total = $prices[1]['_give_amount'];
     //Add a payment
     $purchase_data = array('price' => number_format((double) $total, 2), 'give_form_title' => get_the_title($post_id), 'give_form_id' => $post_id, 'date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user_info['email'], 'user_info' => $user_info, 'currency' => 'USD', 'status' => 'pending', 'gateway' => 'manual');
     $_SERVER['REMOTE_ADDR'] = '10.0.0.0';
     $this->_payment_id = give_insert_payment($purchase_data);
     give_update_payment_status($this->_payment_id, 'complete');
     $this->_api_output = $this->_api->get_forms();
     $this->_api_output_sales = $this->_api->get_recent_donations();
     global $wp_query;
     $wp_query->query_vars['format'] = 'override';
 }
Example #6
0
/**
 * Process PayPal Purchase.
 *
 * @since 1.0
 *
 * @param array $purchase_data Purchase Data
 *
 * @return void
 */
function give_process_paypal_purchase($purchase_data)
{
    if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'give-gateway')) {
        wp_die(esc_html__('Nonce verification has failed.', 'give'), esc_html__('Error', 'give'), array('response' => 403));
    }
    $form_id = intval($purchase_data['post_data']['give-form-id']);
    $price_id = isset($purchase_data['post_data']['give-price-id']) ? $purchase_data['post_data']['give-price-id'] : '';
    // Collect payment data.
    $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => $form_id, 'give_price_id' => $price_id, 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'paypal');
    // Record the pending payment.
    $payment_id = give_insert_payment($payment_data);
    // Check payment.
    if (!$payment_id) {
        // Record the error.
        give_record_gateway_error(esc_html__('Payment Error', 'give'), sprintf(esc_html__('Payment creation failed before sending donor to PayPal. Payment data: %s', 'give'), json_encode($payment_data)), $payment_id);
        // Problems? Send back.
        give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
    } else {
        // Only send to PayPal if the pending payment is created successfully.
        $listener_url = add_query_arg('give-listener', 'IPN', home_url('index.php'));
        // Get the success url.
        $return_url = add_query_arg(array('payment-confirmation' => 'paypal', 'payment-id' => $payment_id), get_permalink(give_get_option('success_page')));
        // Get the PayPal redirect uri.
        $paypal_redirect = trailingslashit(give_get_paypal_redirect()) . '?';
        //Item name - pass level name if variable priced.
        $item_name = $purchase_data['post_data']['give-form-title'];
        //Verify has variable prices.
        if (give_has_variable_prices($form_id) && isset($purchase_data['post_data']['give-price-id'])) {
            $item_price_level_text = give_get_price_option_name($form_id, $purchase_data['post_data']['give-price-id']);
            $price_level_amount = give_get_price_option_amount($form_id, $purchase_data['post_data']['give-price-id']);
            //Donation given doesn't match selected level (must be a custom amount).
            if ($price_level_amount != give_sanitize_amount($purchase_data['price'])) {
                $custom_amount_text = get_post_meta($form_id, '_give_custom_amount_text', true);
                //user custom amount text if any, fallback to default if not.
                $item_name .= ' - ' . (!empty($custom_amount_text) ? $custom_amount_text : esc_html__('Custom Amount', 'give'));
            } elseif (!empty($item_price_level_text)) {
                $item_name .= ' - ' . $item_price_level_text;
            }
        } elseif (give_get_form_price($form_id) !== give_sanitize_amount($purchase_data['price'])) {
            $custom_amount_text = get_post_meta($form_id, '_give_custom_amount_text', true);
            //user custom amount text if any, fallback to default if not.
            $item_name .= ' - ' . (!empty($custom_amount_text) ? $custom_amount_text : esc_html__('Custom Amount', 'give'));
        }
        // Setup PayPal API params.
        $paypal_args = array('business' => give_get_option('paypal_email', false), 'first_name' => $purchase_data['user_info']['first_name'], 'last_name' => $purchase_data['user_info']['last_name'], 'email' => $purchase_data['user_email'], 'invoice' => $purchase_data['purchase_key'], 'amount' => $purchase_data['price'], 'item_name' => stripslashes($item_name), 'no_shipping' => '1', 'shipping' => '0', 'no_note' => '1', 'currency_code' => give_get_currency(), 'charset' => get_bloginfo('charset'), 'custom' => $payment_id, 'rm' => '2', 'return' => $return_url, 'cancel_return' => give_get_failed_transaction_uri('?payment-id=' . $payment_id), 'notify_url' => $listener_url, 'page_style' => give_get_paypal_page_style(), 'cbt' => get_bloginfo('name'), 'bn' => 'givewp_SP');
        //Add user address if present.
        if (!empty($purchase_data['user_info']['address'])) {
            $paypal_args['address1'] = isset($purchase_data['user_info']['address']['line1']) ? $purchase_data['user_info']['address']['line1'] : '';
            $paypal_args['address2'] = isset($purchase_data['user_info']['address']['line2']) ? $purchase_data['user_info']['address']['line2'] : '';
            $paypal_args['city'] = isset($purchase_data['user_info']['address']['city']) ? $purchase_data['user_info']['address']['city'] : '';
            $paypal_args['state'] = isset($purchase_data['user_info']['address']['state']) ? $purchase_data['user_info']['address']['state'] : '';
            $paypal_args['country'] = isset($purchase_data['user_info']['address']['country']) ? $purchase_data['user_info']['address']['country'] : '';
        }
        //Donations or regular transactions?
        if (give_get_option('paypal_button_type') === 'standard') {
            $paypal_extra_args = array('cmd' => '_xclick');
        } else {
            $paypal_extra_args = array('cmd' => '_donations');
        }
        $paypal_args = array_merge($paypal_extra_args, $paypal_args);
        $paypal_args = apply_filters('give_paypal_redirect_args', $paypal_args, $purchase_data);
        // Build query.
        $paypal_redirect .= http_build_query($paypal_args);
        // Fix for some sites that encode the entities.
        $paypal_redirect = str_replace('&', '&', $paypal_redirect);
        // Redirect to PayPal.
        wp_redirect($paypal_redirect);
        exit;
    }
}
Example #7
0
/**
 * Process the payment
 *
 * @since  1.0
 * @return void
 */
function give_offline_process_payment($purchase_data)
{
    $purchase_summary = give_get_purchase_summary($purchase_data);
    // setup the payment details
    $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => intval($purchase_data['post_data']['give-form-id']), 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'offline');
    // record the pending payment
    $payment = give_insert_payment($payment_data);
    if ($payment) {
        give_offline_send_admin_notice($payment);
        give_offline_send_donor_instructions($payment);
        give_send_to_success_page();
    } else {
        // if errors are present, send the user back to the donation form so they can be corrected
        give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
    }
}
 /**
  * Create Simple Donation w/ Fee
  *
  * @return bool|int
  */
 public static function create_simple_payment_with_fee()
 {
     global $give_options;
     // Enable a few options
     $give_options['sequential_prefix'] = 'GIVE-';
     $simple_form = Give_Helper_Form::create_simple_form();
     // Generate some sales
     $user = get_userdata(1);
     $user_info = array('id' => $user->ID, 'email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
     $donation_details = array(array('id' => $simple_form->ID, 'options' => array('price_id' => 0), 'quantity' => 2));
     $total = 0;
     $simple_price = get_post_meta($simple_form->ID, 'give_price', true);
     $total += $simple_price;
     $payment_details = array(array('name' => 'Test Donation', 'id' => $simple_form->ID, 'options' => array('price_id' => 1), 'price' => $simple_price * 2, 'item_price' => $simple_price, 'quantity' => 2));
     $purchase_data = array('price' => number_format((double) $total, 2), 'date' => date('Y-m-d H:i:s', strtotime('-1 day')), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user_info['email'], 'user_info' => $user_info, 'currency' => 'USD', 'donations' => $donation_details, 'payment_details' => $payment_details, 'status' => 'pending');
     $fee_args = array('label' => 'Test Fee', 'type' => 'test', 'amount' => 5);
     //@TODO: Incorporate Fees
     //Give()->fees->add_fee( $fee_args );
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     $_SERVER['SERVER_NAME'] = 'give_virtual';
     $payment_id = give_insert_payment($purchase_data);
     $transaction_id = 'FIR3SID3';
     $payment = new Give_Payment($payment_id);
     $payment->transaction_id = $transaction_id;
     $payment->save();
     give_insert_payment_note($payment_id, sprintf(esc_html__('PayPal Transaction ID: %s', 'give'), $transaction_id));
     return $payment_id;
 }
 /**
  * Authorize.net Payments
  *
  * @param $purchase_data
  */
 public function give_process_authorize_net_payment($purchase_data)
 {
     if (!isset($_POST['card_number']) || $_POST['card_number'] == '') {
         give_set_error('empty_card', __('You must enter a card number', 'give'));
     }
     if (!isset($_POST['card_name']) || $_POST['card_name'] == '') {
         give_set_error('empty_card_name', __('You must enter the name on your card', 'give'));
     }
     if (!isset($_POST['card_exp_month']) || $_POST['card_exp_month'] == '') {
         give_set_error('empty_month', __('You must enter an expiration month', 'give'));
     }
     if (!isset($_POST['card_exp_year']) || $_POST['card_exp_year'] == '') {
         give_set_error('empty_year', __('You must enter an expiration year', 'give'));
     }
     if (!isset($_POST['card_cvc']) || $_POST['card_cvc'] == '' || strlen($_POST['card_cvc']) < 3) {
         give_set_error('empty_cvc', __('You must enter a valid CVC', 'give'));
     }
     $errors = give_get_errors();
     //No errors: Continue with payment processing
     if (!$errors) {
         //Include Authorize SDK
         require_once GIVE_AUTHORIZE_PLUGIN_DIR . '/includes/anet_php_sdk/AuthorizeNet.php';
         if (!give_is_test_mode()) {
             //LIVE:
             $authorize_api_login = give_get_option('give_api_login');
             $authorize_trans_key = give_get_option('give_transaction_key');
         } else {
             //SANDBOX
             $authorize_api_login = give_get_option('give_authorize_sandbox_api_login');
             $authorize_trans_key = give_get_option('give_authorize_sandbox_transaction_key');
         }
         //Check for credentials entered
         if (empty($authorize_api_login) || empty($authorize_trans_key)) {
             give_set_error('error_id_here', __('Error: Missing API Login or Transaction key. Please enter them in the plugin settings.', 'give-authorize'));
             return;
         }
         //Proceed with Authorize AIM
         $transaction = new AuthorizeNetAIM($authorize_api_login, $authorize_trans_key);
         $transaction->VERIFY_PEER = false;
         //Sandbox or not?
         if (give_is_test_mode()) {
             $transaction->setSandbox(true);
         } else {
             $transaction->setSandbox(false);
         }
         $card_info = $purchase_data['card_info'];
         $card_names = explode(' ', $card_info['card_name']);
         $first_name = isset($card_names[0]) ? $card_names[0] : $purchase_data['user_info']['first_name'];
         if (!empty($card_names[1])) {
             unset($card_names[0]);
             $last_name = implode(' ', $card_names);
         } else {
             $last_name = $purchase_data['user_info']['last_name'];
         }
         $transaction->amount = $purchase_data['price'];
         $transaction->card_num = strip_tags(trim($card_info['card_number']));
         $transaction->card_code = strip_tags(trim($card_info['card_cvc']));
         $transaction->exp_date = strip_tags(trim($card_info['card_exp_month'])) . '/' . strip_tags(trim($card_info['card_exp_year']));
         $transaction->description = give_get_purchase_summary($purchase_data);
         $transaction->first_name = $first_name;
         $transaction->last_name = $last_name;
         $transaction->address = $card_info['card_address'] . ' ' . $card_info['card_address_2'];
         $transaction->city = $card_info['card_city'];
         $transaction->country = $card_info['card_country'];
         $transaction->state = $card_info['card_state'];
         $transaction->zip = $card_info['card_zip'];
         $transaction->customer_ip = give_get_ip();
         $transaction->email = $purchase_data['user_email'];
         $transaction->invoice_num = $purchase_data['purchase_key'];
         try {
             $response = $transaction->authorizeAndCapture();
             if ($response->approved) {
                 $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => intval($purchase_data['post_data']['give-form-id']), 'price_id' => isset($purchase_data['post_data']['give-price-id']) ? intval($purchase_data['post_data']['give-price-id']) : '', 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => 'authorizenet');
                 $payment = give_insert_payment($payment_data);
                 if ($payment) {
                     give_update_payment_status($payment, 'publish');
                     give_send_to_success_page();
                 } else {
                     give_set_error('authorize_error', __('Error: your payment could not be recorded. Please try again', 'give'));
                     give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
                 }
             } else {
                 if (isset($response->response_reason_text)) {
                     $error = $response->response_reason_text;
                 } elseif (isset($response->error_message)) {
                     $error = $response->error_message;
                 } else {
                     $error = '';
                 }
                 if (strpos(strtolower($error), 'the credit card number is invalid') !== false) {
                     give_set_error('invalid_card', __('Your card number is invalid', 'give'));
                 } elseif (strpos(strtolower($error), 'this transaction has been declined') !== false) {
                     give_set_error('invalid_card', __('Your card has been declined', 'give'));
                 } elseif (isset($response->response_reason_text)) {
                     give_set_error('api_error', $response->response_reason_text);
                 } elseif (isset($response->error_message)) {
                     give_set_error('api_error', $response->error_message);
                 } else {
                     give_set_error('api_error', sprintf(__('An error occurred. Error data: %s', 'give'), print_r($response, true)));
                 }
                 give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
             }
         } catch (AuthorizeNetException $e) {
             give_set_error('request_error', $e->getMessage());
             give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
         }
     } else {
         give_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['give-gateway']);
     }
 }
Example #10
0
 /**
  * Process purchase.
  *
  * @since 1.0.0
  *
  * @param array $purchase_data Purchase Data
  *
  * @return void
  */
 function process_purchase($purchase_data)
 {
     if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'give-gateway')) {
         wp_die(__('Nonce verification has failed', 'pronamic_ideal'), __('Error', 'pronamic_ideal'), array('response' => 403));
     }
     $form_id = intval($purchase_data['post_data']['give-form-id']);
     // Collect payment data
     $payment_data = array('price' => $purchase_data['price'], 'give_form_title' => $purchase_data['post_data']['give-form-title'], 'give_form_id' => $form_id, 'date' => $purchase_data['date'], 'user_email' => $purchase_data['user_email'], 'purchase_key' => $purchase_data['purchase_key'], 'currency' => give_get_currency(), 'user_info' => $purchase_data['user_info'], 'status' => 'pending', 'gateway' => $this->id);
     // Record the pending payment
     $donation_id = give_insert_payment($payment_data);
     if (!$donation_id) {
         // Record the error
         // /wp-admin/edit.php?post_type=give_forms&page=give-reports&tab=logs&view=gateway_errors
         // @see https://github.com/WordImpress/Give/blob/1.3.6/includes/gateways/functions.php#L267-L285
         give_record_gateway_error(__('Payment Error', 'pronamic_ideal'), sprintf(__('Payment creation failed before sending buyer to payment provider. Payment data: %s', 'pronamic_ideal'), json_encode($payment_data)), $donation_id);
         // Problems? send back
         // @see https://github.com/WordImpress/Give/blob/1.3.6/includes/forms/functions.php#L150-L184
         give_send_back_to_checkout(array('payment-error' => true, 'payment-mode' => $purchase_data['post_data']['give-gateway']));
     } else {
         $config_id = give_get_option(sprintf('give_%s_configuration', $this->id));
         $gateway = Pronamic_WP_Pay_Plugin::get_gateway($config_id);
         if ($gateway) {
             // Data
             $data = new Pronamic_WP_Pay_Extensions_Give_PaymentData($donation_id, $this);
             $gateway->set_payment_method($this->payment_method);
             $payment = Pronamic_WP_Pay_Plugin::start($config_id, $gateway, $data, $this->payment_method);
             $error = $gateway->get_error();
             if (is_wp_error($error)) {
                 // Record the error
                 // /wp-admin/edit.php?post_type=give_forms&page=give-reports&tab=logs&view=gateway_errors
                 // @see https://github.com/WordImpress/Give/blob/1.3.6/includes/gateways/functions.php#L267-L285
                 give_record_gateway_error(__('Payment Error', 'pronamic_ideal'), implode('<br />', $error->get_error_messages()), $donation_id);
                 // Problems? send back
                 // @see https://github.com/WordImpress/Give/blob/1.3.6/includes/forms/functions.php#L150-L184
                 give_send_back_to_checkout(array('payment-error' => true, 'payment-mode' => $purchase_data['post_data']['give-gateway']));
             } else {
                 // Redirect
                 $gateway->redirect($payment);
             }
         }
     }
 }