Exemple #1
0
function espresso_process_paypal_pro($payment_data)
{
    extract($payment_data);
    global $wpdb;
    // Included required files.
    require_once 'includes/paypal.nvp.class.php';
    $paypal_pro_settings = get_option('event_espresso_paypal_pro_settings');
    $sandbox = $paypal_pro_settings['paypal_pro_use_sandbox'];
    // Setup PayPal object
    $PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $paypal_pro_settings['paypal_api_username'], 'APIPassword' => $paypal_pro_settings['paypal_api_password'], 'APISignature' => $paypal_pro_settings['paypal_api_signature']);
    $PayPal = new PayPal($PayPalConfig);
    // Populate data arrays with order data.
    $DPFields = array('paymentaction' => 'Sale', 'ipaddress' => $_SERVER['REMOTE_ADDR'], 'returnfmfdetails' => '1');
    $CCDetails = array('creditcardtype' => $_POST['creditcardtype'], 'acct' => $_POST['card_num'], 'expdate' => $_POST['expmonth'] . $_POST['expyear'], 'cvv2' => $_POST['cvv'], 'startdate' => '', 'issuenumber' => '');
    $PayerInfo = array('email' => $_POST['email'], 'payerid' => '', 'payerstatus' => '', 'business' => '');
    $PayerName = array('salutation' => '', 'firstname' => $_POST['first_name'], 'middlename' => '', 'lastname' => $_POST['last_name'], 'suffix' => '');
    $BillingAddress = array('street' => $_POST['address'], 'street2' => '', 'city' => $_POST['city'], 'state' => $_POST['state'], 'countrycode' => 'US', 'zip' => $_POST['zip'], 'phonenum' => empty($_POST['phone']) ? '' : $_POST['phone']);
    $ShippingAddress = array('shiptoname' => '', 'shiptostreet' => '', 'shiptostreet2' => '', 'shiptocity' => '', 'shiptostate' => '', 'shiptozip' => '', 'shiptocountrycode' => '', 'shiptophonenum' => '');
    $PaymentDetails = array('amt' => $payment_data['total_cost'], 'currencycode' => $paypal_pro_settings['currency_format'], 'itemamt' => '', 'shippingamt' => '', 'handlingamt' => '', 'taxamt' => '', 'desc' => stripslashes_deep($event_name), 'custom' => '', 'invnum' => '', 'notifyurl' => '');
    $OrderItems = array();
    $Item = array('l_name' => stripslashes_deep($event_name), 'l_desc' => stripslashes_deep($event_name), 'l_amt' => $_POST['amount'], 'l_number' => '', 'l_qty' => '1', 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
    array_push($OrderItems, $Item);
    // Wrap all data arrays into a single, "master" array which will be passed into the class function.
    $PayPalRequestData = array('DPFields' => $DPFields, 'CCDetails' => $CCDetails, 'PayerName' => $PayerName, 'BillingAddress' => $BillingAddress, 'PaymentDetails' => $PaymentDetails, 'OrderItems' => $OrderItems);
    $PayPalResult = $PayPal->DoDirectPayment($PayPalRequestData);
    $payment_data['payment_status'] = 'Incomplete';
    $payment_data['txn_type'] = 'PayPal Pro';
    $payment_data['txn_id'] = 0;
    $payment_data['txn_details'] = serialize($_REQUEST);
    $payment_data = apply_filters('filter_hook_espresso_prepare_event_link', $payment_data);
    $payment_data = apply_filters('filter_hook_espresso_get_total_cost', $payment_data);
    $Errors = GetErrors($PayPalResult);
    if (!empty($PayPalResult)) {
        unset($PayPalResult['REQUESTDATA']['CREDITCARDTYPE']);
        unset($PayPalResult['REQUESTDATA']['ACCT']);
        unset($PayPalResult['REQUESTDATA']['EXPDATE']);
        unset($PayPalResult['REQUESTDATA']['CVV2']);
        unset($PayPalResult['RAWREQUEST']);
        $payment_data['txn_id'] = $PayPalResult['TRANSACTIONID'];
        $payment_data['txn_details'] = serialize($PayPalResult);
        if (!APICallSuccessful($PayPalResult['ACK'])) {
            DisplayErrors($Errors);
        } else {
            $payment_data['payment_status'] = 'Completed';
        }
    } else {
        ?>
		<p><?php 
        _e('There was no response from PayPal.', 'event_espresso');
        ?>
</p>
		<?php 
    }
    add_action('action_hook_espresso_email_after_payment', 'espresso_email_after_payment');
    return $payment_data;
}
<?php

// Include required library files.
require_once 'includes/config.php';
require_once 'includes/paypal.class.php';
// Create PayPal object.
$PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $api_username, 'APIPassword' => $api_password, 'APISignature' => $api_signature);
$PayPal = new PayPal($PayPalConfig);
// Prepare request arrays
$DPFields = array('paymentaction' => '', 'ipaddress' => '192.168.100.51', 'returnfmfdetails' => '');
$CCDetails = array('creditcardtype' => 'Amex', 'acct' => '371558013221013', 'expdate' => '042016', 'cvv2' => '6273', 'startdate' => '', 'issuenumber' => '');
$PayerInfo = array('email' => '*****@*****.**', 'firstname' => 'Michael', 'lastname' => 'Abitbol');
$BillingAddress = array('street' => '150 North Radnor Chester Road Suite F-200', 'street2' => '', 'city' => 'Radnor', 'state' => 'Pennsylvania', 'countrycode' => 'US', 'zip' => '19087', 'phonenum' => '');
$ShippingAddress = array('shiptoname' => 'Michael', 'shiptostreet' => '150 North Radnor Chester Road Suite F-200', 'shiptostreet2' => '', 'shiptocity' => 'Radnor', 'shiptostate' => 'Pennsylvania', 'shiptozip' => '19087', 'shiptocountry' => 'US', 'shiptophonenum' => '');
$PaymentDetails = array('amt' => '1', 'currencycode' => 'USD', 'itemamt' => '', 'shippingamt' => '', 'insuranceamt' => '', 'shipdiscamt' => '', 'handlingamt' => '', 'taxamt' => '', 'desc' => '', 'custom' => '', 'invnum' => '', 'notifyurl' => '', 'recurring' => '');
// For order items you populate a nested array with multiple $Item arrays.  Normally you'll be looping through cart items to populate the $Item
// array and then push it into the $OrderItems array at the end of each loop for an entire collection of all items in $OrderItems.
$OrderItems = array();
$Item = array('l_name' => '', 'l_desc' => '', 'l_amt' => '', 'l_number' => '', 'l_qty' => '', 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
array_push($OrderItems, $Item);
$Secure3D = array('authstatus3d' => '', 'mpivendor3ds' => '', 'cavv' => '', 'eci3ds' => '', 'xid' => '');
$PayPalRequestData = array('DPFields' => $DPFields, 'CCDetails' => $CCDetails, 'PayerInfo' => $PayerInfo, 'BillingAddress' => $BillingAddress, 'ShippingAddress' => $ShippingAddress, 'PaymentDetails' => $PaymentDetails, 'OrderItems' => $OrderItems);
// Pass data into class for processing with PayPal and load the response array into $PayPalResult
$PayPalResult = $PayPal->DoDirectPayment($PayPalRequestData);
// Write the contents of the response array to the screen for demo purposes.
echo '<pre />';
print_r($PayPalResult);
 /**
  * do_payment
  *
  * Makes the request to PayPal's DoDirectPayment API
  *
  * @access public
  * @param mixed $order
  * @param mixed $card_number
  * @param mixed $card_type
  * @param mixed $card_exp_month
  * @param mixed $card_exp_year
  * @param mixed $card_csc
  * @param string $centinelPAResStatus (default: '')
  * @param string $centinelEnrolled (default: '')
  * @param string $centinelCavv (default: '')
  * @param string $centinelEciFlag (default: '')
  * @param string $centinelXid (default: '')
  * @return void
  */
 function do_payment($order, $card_number, $card_type, $card_exp_month, $card_exp_year, $card_csc, $centinelPAResStatus = '', $centinelEnrolled = '', $centinelCavv = '', $centinelEciFlag = '', $centinelXid = '')
 {
     /*
      * Display message to user if session has expired.
      */
     if (sizeof(WC()->cart->get_cart()) == 0) {
         wc_add_notice(sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>', 'wc-paypal-express'), home_url()), "error");
     }
     /*
      * Check if the PayPal class has already been established.
      */
     if (!class_exists('PayPal')) {
         require_once 'lib/angelleye/paypal-php-library/includes/paypal.class.php';
     }
     /*
      * Create PayPal object.
      */
     $PayPalConfig = array('Sandbox' => $this->testmode == 'yes' ? TRUE : FALSE, 'APIUsername' => $this->api_username, 'APIPassword' => $this->api_password, 'APISignature' => $this->api_signature);
     $PayPal = new PayPal($PayPalConfig);
     if (empty($GLOBALS['wp_rewrite'])) {
         $GLOBALS['wp_rewrite'] = new WP_Rewrite();
     }
     $card_exp = $card_exp_month . $card_exp_year;
     /**
      * Generate PayPal request
      */
     $DPFields = array('paymentaction' => 'Sale', 'ipaddress' => $this->get_user_ip(), 'returnfmfdetails' => '');
     $CCDetails = array('creditcardtype' => $card_type, 'acct' => $card_number, 'expdate' => $card_exp, 'cvv2' => $card_csc, 'startdate' => '', 'issuenumber' => '');
     $PayerInfo = array('email' => $order->billing_email, 'firstname' => $order->billing_first_name, 'lastname' => $order->billing_last_name);
     $BillingAddress = array('street' => $order->billing_address_1, 'street2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'countrycode' => $order->billing_country, 'zip' => $order->billing_postcode, 'phonenum' => $order->billing_phone);
     $ShippingAddress = array('shiptoname' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'shiptostreet' => $order->shipping_address_1, 'shiptostreet2' => $order->shipping_address_2, 'shiptocity' => $order->shipping_city, 'shiptostate' => $order->shipping_state, 'shiptozip' => $order->shipping_postcode, 'shiptocountry' => $order->shipping_country, 'shiptophonenum' => $order->shipping_phone);
     $PaymentDetails = array('amt' => $order->get_total(), 'currencycode' => get_option('woocommerce_currency'), 'insuranceamt' => '', 'shipdiscamt' => '', 'handlingamt' => '', 'desc' => '', 'custom' => $order->customer_note ? wptexturize($order->customer_note) : '', 'invnum' => $invoice_number = preg_replace("/[^0-9,.]/", "", $order->id), 'notifyurl' => '', 'recurring' => '');
     $OrderItems = array();
     $item_loop = 0;
     if (sizeof($order->get_items()) > 0) {
         $ITEMAMT = $TAXAMT = 0;
         $inc_tax = get_option('woocommerce_prices_include_tax') == 'yes' ? true : false;
         foreach ($order->get_items() as $item) {
             $_product = $order->get_product_from_item($item);
             if ($item['qty']) {
                 $sku = $_product->get_sku();
                 if ($_product->product_type == 'variation') {
                     if (empty($sku)) {
                         $sku = $_product->parent->get_sku();
                     }
                     //$this->log->add('paypal-pro', print_r($item['item_meta'], true));
                     $item_meta = new WC_Order_Item_Meta($item['item_meta']);
                     $meta = $item_meta->display(true, true);
                     $item['name'] = html_entity_decode($item['name'], ENT_NOQUOTES, 'UTF-8');
                     if (!empty($meta)) {
                         $item['name'] .= " - " . str_replace(", \n", " - ", $meta);
                     }
                 }
                 /**
                  * Get price based on text setting.
                  */
                 if (get_option('woocommerce_prices_include_tax') == 'yes') {
                     $product_price = $order->get_item_subtotal($item, true, false);
                 } else {
                     $product_price = $order->get_item_subtotal($item, false, true);
                 }
                 $Item = array('l_name' => $item['name'], 'l_desc' => '', 'l_amt' => number_format($product_price, 2, '.', ''), 'l_number' => $sku, 'l_qty' => $item['qty'], 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
                 array_push($OrderItems, $Item);
                 $ITEMAMT += $product_price * $item['qty'];
                 $item_loop++;
             }
         }
         //Cart Discount
         if ($order->get_cart_discount() > 0) {
             foreach (WC()->cart->get_coupons('cart') as $code => $coupon) {
                 $Item = array('l_name' => 'Cart Discount', 'l_desc' => '', 'l_amt' => '-' . WC()->cart->coupon_discount_amounts[$code], 'l_number' => $code, 'l_qty' => '1', 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
                 array_push($OrderItems, $Item);
             }
             $ITEMAMT = $ITEMAMT - $order->get_cart_discount();
         }
         //Order Discount
         if ($order->get_order_discount() > 0) {
             foreach (WC()->cart->get_coupons('order') as $code => $coupon) {
                 $Item = array('l_name' => 'Order Discount', 'l_desc' => '', 'l_amt' => '-' . WC()->cart->coupon_discount_amounts[$code], 'l_number' => $code, 'l_qty' => '1', 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
                 array_push($OrderItems, $Item);
             }
             $ITEMAMT = $ITEMAMT - $order->get_order_discount();
         }
         /**
          * Get shipping and tax.
          */
         if (get_option('woocommerce_prices_include_tax') == 'yes') {
             $shipping = $order->get_total_shipping() + $order->get_shipping_tax();
             $tax = 0;
         } else {
             $shipping = $order->get_total_shipping();
             $tax = $order->get_total_tax();
         }
         if ($tax > 0) {
             $PaymentDetails['taxamt'] = $tax;
             // Required if you specify itemized cart tax details. Sum of tax for all items on the order.  Total sales tax.
         }
         if ($shipping > 0) {
             $PaymentDetails['shippingamt'] = $shipping;
             // Total shipping costs for the order.  If you specify shippingamt, you must also specify itemamt.
         }
         $PaymentDetails['itemamt'] = number_format($ITEMAMT, 2, '.', '');
         // Required if you include itemized cart details. (L_AMTn, etc.)  Subtotal of items not including S&H, or tax.
     }
     if ($this->debug) {
         $log = $post_data;
         $log['ACCT'] = '****';
         $log['CVV2'] = '****';
         $this->log->add('paypal-pro', 'Do payment request ' . print_r($log, true));
     }
     /**
      * 3D Secure Params
      */
     if ($this->enable_3dsecure) {
         $Secure3D = array('authstatus3d' => $centinelPAResStatus, 'mpivendor3ds' => $centinelEnrolled, 'cavv' => $centinelCavv, 'eci3ds' => $centinelEciFlag, 'xid' => $centinelXid);
     } else {
         $Secure3D = array();
     }
     $PayPalRequestData = array('DPFields' => $DPFields, 'CCDetails' => $CCDetails, 'PayerInfo' => $PayerInfo, 'BillingAddress' => $BillingAddress, 'ShippingAddress' => $ShippingAddress, 'PaymentDetails' => $PaymentDetails, 'OrderItems' => $OrderItems, 'Secure3D' => $Secure3D);
     // Pass data into class for processing with PayPal and load the response array into $PayPalResult
     $PayPalResult = $PayPal->DoDirectPayment($PayPalRequestData);
     if ($this->debug) {
         $this->log->add('paypal-pro', 'Result ' . print_r($PayPalResult, true));
     }
     if (empty($PayPalResult)) {
         throw new Exception(__('Empty PayPal response.', 'wc_paypal_pro'));
     }
     if ($PayPal->APICallSuccessful($PayPalResult['ACK'])) {
         // Add order note
         $order->add_order_note(sprintf(__('PayPal Pro payment completed (Transaction ID: %s, Correlation ID: %s)', 'wc_paypal_pro'), $parsed_response['TRANSACTIONID'], $parsed_response['CORRELATIONID']));
         // Payment complete
         $order->payment_complete();
         // Remove cart
         WC()->cart->empty_cart();
         // Return thank you page redirect
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } else {
         if ($this->debug) {
             $this->log->add('paypal-pro', 'Error ' . print_r($PayPalResult['ERRORS'], true));
         }
         throw new Exception(__('There was a problem connecting to the payment gateway.', 'wc_paypal_pro'));
         // Get error message
         $error_code = $PayPalResult['ERRORS'][0]['L_ERRORCODE'];
         $error_message = $error_code . '-' . $PayPalResult['ERRORS'][0]['L_LONGMESSAGE'];
         // Payment failed :(
         $order->update_status('failed', sprintf(__('PayPal Pro payment failed (Correlation ID: %s). Payment was rejected due to an error: ', 'wc_paypal_pro'), $parsed_response['CORRELATIONID']) . '(' . $parsed_response['L_ERRORCODE0'] . ') ' . '"' . $error_message . '"');
         wc_add_notice(__('Payment error:', 'wc_paypal_pro') . ' ' . $error_message, "error");
         return;
     }
 }
function espresso_process_paypal_pro($payment_data)
{
    extract($payment_data);
    global $wpdb;
    // Included required files.
    require_once 'includes/paypal.nvp.class.php';
    $paypal_pro_settings = get_option('event_espresso_paypal_pro_settings');
    $sandbox = $paypal_pro_settings['paypal_pro_use_sandbox'];
    // Setup PayPal object
    $PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $paypal_pro_settings['paypal_api_username'], 'APIPassword' => $paypal_pro_settings['paypal_api_password'], 'APISignature' => $paypal_pro_settings['paypal_api_signature']);
    $PayPal = new PayPal($PayPalConfig);
    //Find the correct amount so that unsavory characters don't change it in the previous form
    $sql = "SELECT ea.amount_pd, ed.event_name FROM " . EVENTS_ATTENDEE_TABLE . " ea ";
    $sql .= "JOIN " . EVENTS_DETAIL_TABLE . " ed ";
    $sql .= "ON ed.id = ea.event_id ";
    $sql .= " WHERE registration_id = '" . espresso_registration_id($_POST['id']) . "' ";
    $sql .= " ORDER BY ea.id ASC LIMIT 1";
    $r = $wpdb->get_row($sql);
    if (!$r || $wpdb->num_rows == 0) {
        exit("Looks like something went wrong.  Please try again or notify the website administrator.");
    }
    // Populate data arrays with order data.
    $DPFields = array('paymentaction' => 'Sale', 'ipaddress' => $_SERVER['REMOTE_ADDR'], 'returnfmfdetails' => '1');
    $CCDetails = array('creditcardtype' => $_POST['creditcardtype'], 'acct' => $_POST['card_num'], 'expdate' => $_POST['expmonth'] . $_POST['expyear'], 'cvv2' => $_POST['cvv'], 'startdate' => '', 'issuenumber' => '');
    $PayerInfo = array('email' => $_POST['email'], 'payerid' => '', 'payerstatus' => '', 'business' => '');
    $PayerName = array('salutation' => '', 'firstname' => $_POST['first_name'], 'middlename' => '', 'lastname' => $_POST['last_name'], 'suffix' => '');
    $BillingAddress = array('street' => $_POST['address'], 'street2' => '', 'city' => $_POST['city'], 'state' => $_POST['state'], 'countrycode' => 'US', 'zip' => $_POST['zip'], 'phonenum' => empty($_POST['phone']) ? '' : $_POST['phone']);
    $ShippingAddress = array('shiptoname' => '', 'shiptostreet' => '', 'shiptostreet2' => '', 'shiptocity' => '', 'shiptostate' => '', 'shiptozip' => '', 'shiptocountrycode' => '', 'shiptophonenum' => '');
    $PaymentDetails = array('amt' => $r->amount_pd, 'currencycode' => $paypal_pro_settings['currency_format'], 'itemamt' => '', 'shippingamt' => '', 'handlingamt' => '', 'taxamt' => '', 'desc' => stripslashes_deep($r->event_name), 'custom' => '', 'invnum' => '', 'notifyurl' => '');
    $OrderItems = array();
    $Item = array('l_name' => stripslashes_deep($r->event_name), 'l_desc' => stripslashes_deep($r->event_name), 'l_amt' => $_POST['amount'], 'l_number' => '', 'l_qty' => '1', 'l_taxamt' => '', 'l_ebayitemnumber' => '', 'l_ebayitemauctiontxnid' => '', 'l_ebayitemorderid' => '');
    array_push($OrderItems, $Item);
    // Wrap all data arrays into a single, "master" array which will be passed into the class function.
    $PayPalRequestData = array('DPFields' => $DPFields, 'CCDetails' => $CCDetails, 'PayerName' => $PayerName, 'BillingAddress' => $BillingAddress, 'PaymentDetails' => $PaymentDetails, 'OrderItems' => $OrderItems);
    $PayPalResult = $PayPal->DoDirectPayment($PayPalRequestData);
    $payment_data['payment_status'] = 'Incomplete';
    $payment_data['txn_type'] = 'PPP';
    $payment_data['txn_id'] = 0;
    $payment_data['txn_details'] = serialize($_REQUEST);
    $payment_data = apply_filters('filter_hook_espresso_prepare_event_link', $payment_data);
    $payment_data = apply_filters('filter_hook_espresso_get_total_cost', $payment_data);
    $Errors = GetErrors($PayPalResult);
    if (!empty($PayPalResult)) {
        $payment_data['txn_id'] = $PayPalResult['TRANSACTIONID'];
        $payment_data['txn_details'] = serialize($PayPalResult);
        if (!APICallSuccessful($PayPalResult['ACK'])) {
            DisplayErrors($Errors);
        } else {
            $payment_data['payment_status'] = 'Completed';
        }
    } else {
        ?>
		<p><?php 
        _e('There was no response from Paypal.', 'event_espresso');
        ?>
</p>
		<?php 
    }
    $payment_data = apply_filters('filter_hook_espresso_update_attendee_payment_data_in_db', $payment_data);
    do_action('action_hook_espresso_email_after_payment', $payment_data);
    return $payment_data;
}