Beispiel #1
0
function bizz_check_ipn_response()
{
    if (isset($_GET['paypalListener']) && $_GET['paypalListener'] == 'paypal_standard_IPN') {
        // variables
        $listener = new IpnListener();
        $listener->use_sandbox = TEST_MODE;
        $listener->use_ssl = true;
        $listener->use_curl = false;
        try {
            $listener->requirePostMethod();
            $verified = $listener->processIpn();
        } catch (Exception $e) {
            error_log($e->getMessage(), 3, 'error_log');
            exit(0);
        }
        if ($verified) {
            // Get transaction details
            $posted = $listener->getPostData();
            // Post ID
            $order_id = $posted['custom'];
            $order_key = $posted['invoice'];
            // Save details
            $r = '';
            foreach ($posted as $key => $value) {
                $r .= str_pad($key, 25) . "{$value}<br/>";
            }
            $transaction_details = get_post_meta($order_id, 'bizzthemes_booking_paypal_details', 1);
            $transaction_details = !empty($transaction_details) ? $transaction_details . '<br/><hr/><br/>' . $r : $r;
            update_post_meta($order_id, 'bizzthemes_booking_paypal_details', $transaction_details);
            // get booking meta
            $booking_custom = get_post_custom($order_id);
            foreach ($booking_custom as $key => $value) {
                $bookopts[$key] = $value[0];
            }
            // Lowercase
            $posted['payment_status'] = strtolower($posted['payment_status']);
            $posted['txn_type'] = strtolower($posted['txn_type']);
            // Sandbox fix
            if ($posted['test_ipn'] == 1 && $posted['payment_status'] == 'pending') {
                $posted['payment_status'] = 'completed';
            }
            // We are here so lets check status and do actions
            switch ($posted['payment_status']) {
                case 'completed':
                    // Check valid txn_type
                    $accepted_types = array('cart', 'instant', 'express_checkout', 'web_accept', 'masspay', 'send_money');
                    if (!in_array($posted['txn_type'], $accepted_types)) {
                        if (DEBUG_MODE) {
                            error_log('Invalid type:' . $posted['txn_type'], 3, 'error_log');
                        }
                        exit;
                    }
                    // Saved booking
                    $deposit_amount = get_post_meta($order_id, 'bizzthemes_car_pay_deposit', 1);
                    $total_amount = get_post_meta($order_id, 'bizzthemes_car_pay_total', 1);
                    // Validate Amount
                    if (round($deposit_amount, 2) != $posted['mc_gross']) {
                        if (DEBUG_MODE) {
                            error_log('Payment error: Amounts do not match (gross ' . $posted['mc_gross'] . ', saved ' . round($deposit_amount, 2) . ')', 3, 'error_log');
                        }
                        exit;
                    }
                    // Store PP Details
                    if (!empty($posted['payer_email'])) {
                        update_post_meta($order_id, 'bizzthemes_bookings_email', $posted['payer_email']);
                    }
                    if (!empty($posted['txn_id'])) {
                        update_post_meta($order_id, 'bizzthemes_car_transaction_id', $posted['txn_id']);
                    }
                    if (!empty($posted['first_name'])) {
                        update_post_meta($order_id, 'bizzthemes_bookings_fname', $posted['first_name']);
                    }
                    if (!empty($posted['last_name'])) {
                        update_post_meta($order_id, 'bizzthemes_bookings_lname', $posted['last_name']);
                    }
                    // Paid
                    update_post_meta($order_id, 'bizzthemes_car_pay_paid', round($posted['mc_gross'], 2));
                    // Completed?
                    if (round($total_amount, 2) == $posted['mc_gross']) {
                        update_post_meta($order_id, 'bizzthemes_bookings_status', 'approved');
                    }
                    break;
                case 'denied':
                case 'expired':
                case 'failed':
                case 'voided':
                    // Only handle full refunds, not partial
                    update_post_meta($order_id, 'bizzthemes_bookings_status', 'cancelled');
                    break;
                case "refunded":
                    // Only handle full refunds, not partial
                    update_post_meta($order_id, 'bizzthemes_bookings_status', 'refunded');
                    update_post_meta($order_id, 'bizzthemes_car_pay_paid', '0');
                    booking_send_notification('refunded', $bookopts);
                    break;
                case "reversed":
                case "chargeback":
                    // Mark order as refunded
                    update_post_meta($order_id, 'bizzthemes_bookings_status', 'refunded');
                    update_post_meta($order_id, 'bizzthemes_car_pay_paid', '0');
                    break;
                default:
                    // No action
                    break;
            }
            exit;
            // error_log('getPaymentData: '.$r, 3, 'error_log');
            //error_log('getTextReport: '.$listener->getTextReport(), 3, 'error_log');
            // mail('*****@*****.**', 'Verified IPN', $listener->getTextReport());
        } else {
            /* zapis v bazo o napačni transakciji */
            // mail('*****@*****.**', 'Invalid IPN', $listener->getTextReport());
            error_log('getTextReport: ' . $listener->getTextReport(), 3, 'error_log');
        }
    }
}