Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * Set it Up
  */
 public function setUp()
 {
     parent::setUp();
     $payment_id = Give_Helper_Payment::create_simple_payment();
     $this->_payment_key = give_get_payment_key($payment_id);
     $this->_payment_id = $payment_id;
     $this->_key = $this->_payment_key;
     $this->_transaction_id = 'FIR3SID3';
     give_set_payment_transaction_id($payment_id, $this->_transaction_id);
     give_insert_payment_note($payment_id, sprintf(esc_html__('PayPal Transaction ID: %s', 'give'), $this->_transaction_id));
     // Make sure we're working off a clean object caching in WP Core.
     // Prevents some payment_meta from not being present.
     clean_post_cache($payment_id);
     update_postmeta_cache(array($payment_id));
 }
Esempio n. 3
0
/**
 * Process web accept (one time) payment IPNs.
 *
 * @since 1.0
 *
 * @param array $data       IPN Data
 * @param int   $payment_id The payment ID from Give.
 *
 * @return void
 */
function give_process_paypal_web_accept_and_cart($data, $payment_id)
{
    //Only allow through these transaction types.
    if ($data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && strtolower($data['payment_status']) != 'refunded') {
        return;
    }
    //Need $payment_id to continue.
    if (empty($payment_id)) {
        return;
    }
    // Collect donation payment details.
    $paypal_amount = $data['mc_gross'];
    $payment_status = strtolower($data['payment_status']);
    $currency_code = strtolower($data['mc_currency']);
    $business_email = isset($data['business']) && is_email($data['business']) ? trim($data['business']) : trim($data['receiver_email']);
    $payment_meta = give_get_payment_meta($payment_id);
    // Must be a PayPal standard IPN.
    if (give_get_payment_gateway($payment_id) != 'paypal') {
        return;
    }
    // Verify payment recipient
    if (strcasecmp($business_email, trim(give_get_option('paypal_email'))) != 0) {
        give_record_gateway_error(esc_html__('IPN Error', 'give'), sprintf(esc_html__('Invalid business email in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
        give_update_payment_status($payment_id, 'failed');
        give_insert_payment_note($payment_id, esc_html__('Payment failed due to invalid PayPal business email.', 'give'));
        return;
    }
    // Verify payment currency.
    if ($currency_code != strtolower($payment_meta['currency'])) {
        give_record_gateway_error(esc_html__('IPN Error', 'give'), sprintf(esc_html__('Invalid currency in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
        give_update_payment_status($payment_id, 'failed');
        give_insert_payment_note($payment_id, esc_html__('Payment failed due to invalid currency in PayPal IPN.', 'give'));
        return;
    }
    //Process refunds & reversed.
    if ($payment_status == 'refunded' || $payment_status == 'reversed') {
        give_process_paypal_refund($data, $payment_id);
        return;
    }
    // Only complete payments once.
    if (get_post_status($payment_id) == 'publish') {
        return;
    }
    // Retrieve the total donation amount (before PayPal).
    $payment_amount = give_get_payment_amount($payment_id);
    //Check that the donation PP and local db amounts match.
    if (number_format((double) $paypal_amount, 2) < number_format((double) $payment_amount, 2)) {
        // The prices don't match
        give_record_gateway_error(esc_html__('IPN Error', 'give'), sprintf(esc_html__('Invalid payment amount in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
        give_update_payment_status($payment_id, 'failed');
        give_insert_payment_note($payment_id, esc_html__('Payment failed due to invalid amount in PayPal IPN.', 'give'));
        return;
    }
    //Process completed donations.
    if ($payment_status == 'completed' || give_is_test_mode()) {
        give_insert_payment_note($payment_id, sprintf(esc_html__('PayPal Transaction ID: %s', 'give'), $data['txn_id']));
        give_set_payment_transaction_id($payment_id, $data['txn_id']);
        give_update_payment_status($payment_id, 'publish');
    } elseif ('pending' == $payment_status && isset($data['pending_reason'])) {
        // Look for possible pending reasons, such as an echeck.
        $note = give_paypal_get_pending_donation_note(strtolower($data['pending_reason']));
        if (!empty($note)) {
            give_insert_payment_note($payment_id, $note);
        }
    }
}
Esempio n. 4
0
/**
 * Process web accept (one time) payment IPNs
 *
 * @since 1.0
 *
 * @param array $data IPN Data
 *
 * @return void
 */
function give_process_paypal_web_accept_and_cart($data, $payment_id)
{
    if ($data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded') {
        return;
    }
    if (empty($payment_id)) {
        return;
    }
    // Collect payment details
    $purchase_key = isset($data['invoice']) ? $data['invoice'] : $data['item_number'];
    $paypal_amount = $data['mc_gross'];
    $payment_status = strtolower($data['payment_status']);
    $currency_code = strtolower($data['mc_currency']);
    $business_email = isset($data['business']) && is_email($data['business']) ? trim($data['business']) : trim($data['receiver_email']);
    $payment_meta = give_get_payment_meta($payment_id);
    if (give_get_payment_gateway($payment_id) != 'paypal') {
        return;
        // this isn't a PayPal standard IPN
    }
    // Verify payment recipient
    if (strcasecmp($business_email, trim(give_get_option('paypal_email'))) != 0) {
        give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid business email in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
        give_update_payment_status($payment_id, 'failed');
        give_insert_payment_note($payment_id, __('Payment failed due to invalid PayPal business email.', 'give'));
        return;
    }
    // Verify payment currency
    if ($currency_code != strtolower($payment_meta['currency'])) {
        give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid currency in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
        give_update_payment_status($payment_id, 'failed');
        give_insert_payment_note($payment_id, __('Payment failed due to invalid currency in PayPal IPN.', 'give'));
        return;
    }
    if (!give_get_payment_user_email($payment_id)) {
        // No email associated with purchase, so store from PayPal
        give_update_payment_meta($payment_id, '_give_payment_user_email', $data['payer_email']);
        // Setup and store the donors's details
        $address = array();
        $address['line1'] = !empty($data['address_street']) ? sanitize_text_field($data['address_street']) : false;
        $address['city'] = !empty($data['address_city']) ? sanitize_text_field($data['address_city']) : false;
        $address['state'] = !empty($data['address_state']) ? sanitize_text_field($data['address_state']) : false;
        $address['country'] = !empty($data['address_country_code']) ? sanitize_text_field($data['address_country_code']) : false;
        $address['zip'] = !empty($data['address_zip']) ? sanitize_text_field($data['address_zip']) : false;
        $user_info = array('id' => '-1', 'email' => sanitize_text_field($data['payer_email']), 'first_name' => sanitize_text_field($data['first_name']), 'last_name' => sanitize_text_field($data['last_name']), 'discount' => '', 'address' => $address);
        $payment_meta['user_info'] = $user_info;
        give_update_payment_meta($payment_id, '_give_payment_meta', $payment_meta);
    }
    if ($payment_status == 'refunded' || $payment_status == 'reversed') {
        // Process a refund
        give_process_paypal_refund($data, $payment_id);
    } else {
        if (get_post_status($payment_id) == 'publish') {
            return;
            // Only complete payments once
        }
        // Retrieve the total purchase amount (before PayPal)
        $payment_amount = give_get_payment_amount($payment_id);
        if (number_format((double) $paypal_amount, 2) < number_format((double) $payment_amount, 2)) {
            // The prices don't match
            give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid payment amount in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
            give_update_payment_status($payment_id, 'failed');
            give_insert_payment_note($payment_id, __('Payment failed due to invalid amount in PayPal IPN.', 'give'));
            return;
        }
        if ($purchase_key != give_get_payment_key($payment_id)) {
            // Purchase keys don't match
            give_record_gateway_error(__('IPN Error', 'give'), sprintf(__('Invalid purchase key in IPN response. IPN data: %s', 'give'), json_encode($data)), $payment_id);
            give_update_payment_status($payment_id, 'failed');
            give_insert_payment_note($payment_id, __('Payment failed due to invalid purchase key in PayPal IPN.', 'give'));
            return;
        }
        if ($payment_status == 'completed' || give_is_test_mode()) {
            give_insert_payment_note($payment_id, sprintf(__('PayPal Transaction ID: %s', 'give'), $data['txn_id']));
            give_set_payment_transaction_id($payment_id, $data['txn_id']);
            give_update_payment_status($payment_id, 'publish');
        } else {
            if ('pending' == $payment_status && isset($data['pending_reason'])) {
                // Look for possible pending reasons, such as an echeck
                $note = '';
                switch (strtolower($data['pending_reason'])) {
                    case 'echeck':
                        $note = __('Payment made via eCheck and will clear automatically in 5-8 days', 'give');
                        break;
                    case 'address':
                        $note = __('Payment requires a confirmed donor address and must be accepted manually through PayPal', 'give');
                        break;
                    case 'intl':
                        $note = __('Payment must be accepted manually through PayPal due to international account regulations', 'give');
                        break;
                    case 'multi-currency':
                        $note = __('Payment received in non-shop currency and must be accepted manually through PayPal', 'give');
                        break;
                    case 'paymentreview':
                    case 'regulatory_review':
                        $note = __('Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'give');
                        break;
                    case 'unilateral':
                        $note = __('Payment was sent to non-confirmed or non-registered email address.', 'give');
                        break;
                    case 'upgrade':
                        $note = __('PayPal account must be upgraded before this payment can be accepted', 'give');
                        break;
                    case 'verify':
                        $note = __('PayPal account is not verified. Verify account in order to accept this payment', 'give');
                        break;
                    case 'other':
                        $note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance', 'give');
                        break;
                }
                if (!empty($note)) {
                    give_insert_payment_note($payment_id, $note);
                }
            }
        }
    }
}