/** * do_payment * * Process the PayFlow transaction with PayPal. * * @access public * @param mixed $order * @param mixed $card_number * @param mixed $card_exp * @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_exp, $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 →</a>', 'wc-paypal-express'), home_url()), "error"); } /* * Check if the PayPal_PayFlow class has already been established. */ if (!class_exists('PayPal_PayFlow')) { require_once 'lib/angelleye/paypal-php-library/includes/paypal.class.php'; require_once 'lib/angelleye/paypal-php-library/includes/paypal.payflow.class.php'; } /** * Create PayPal_PayFlow object. */ $PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $this->paypal_user, 'APIPassword' => trim($this->paypal_password), 'APIVendor' => $this->paypal_vendor, 'APIPartner' => $this->paypal_partner); $PayPal = new PayPal_PayFlow($PayPalConfig); /** * Pulled from original Woo extension. */ if (empty($GLOBALS['wp_rewrite'])) { $GLOBALS['wp_rewrite'] = new WP_Rewrite(); } $this->add_log($order->get_checkout_order_received_url()); try { /** * Parameter set by original Woo. I can probably ditch this, but leaving it for now. */ $url = $this->testmode == 'yes' ? $this->testurl : $this->liveurl; /** * PayPal PayFlow Gateway Request Params */ $PayPalRequestData = array('tender' => 'C', 'trxtype' => 'S', 'acct' => $card_number, 'expdate' => $card_exp, 'amt' => $order->get_total(), 'currency' => get_option('woocommerce_currency'), 'dutyamt' => '', 'freightamt' => '', 'taxamt' => '', 'taxexempt' => '', 'comment1' => $order->customer_note ? wptexturize($order->customer_note) : '', 'comment2' => '', 'cvv2' => $card_csc, 'recurring' => '', 'swipe' => '', 'orderid' => preg_replace("/[^0-9,.]/", "", $order->get_order_number()), 'orderdesc' => 'Order ' . $order->get_order_number() . ' on ' . get_bloginfo('name'), 'billtoemail' => $order->billing_email, 'billtophonenum' => '', 'billtofirstname' => $order->billing_first_name, 'billtomiddlename' => '', 'billtolastname' => $order->billing_last_name, 'billtostreet' => $order->billing_address_1 . ' ' . $order->billing_address_2, 'billtocity' => $order->billing_city, 'billtostate' => $order->billing_state, 'billtozip' => $order->billing_postcode, 'billtocountry' => $order->billing_country, 'origid' => '', 'custref' => '', 'custcode' => '', 'custip' => $this->get_user_ip(), 'invnum' => str_replace("#", "", $order->get_order_number()), 'ponum' => '', 'starttime' => '', 'endtime' => '', 'securetoken' => '', 'partialauth' => '', 'authcode' => ''); /** * Shipping info */ if ($order->shipping_address_1) { $PayPalRequestData['SHIPTOFIRSTNAME'] = $order->shipping_first_name; $PayPalRequestData['SHIPTOLASTNAME'] = $order->shipping_last_name; $PayPalRequestData['SHIPTOSTREET'] = $order->shipping_address_1 . ' ' . $order->shipping_address_2; $PayPalRequestData['SHIPTOCITY'] = $order->shipping_city; $PayPalRequestData['SHIPTOSTATE'] = $order->shipping_state; $PayPalRequestData['SHIPTOCOUNTRY'] = $order->shipping_country; $PayPalRequestData['SHIPTOZIP'] = $order->shipping_postcode; } /* Send Item details */ $item_loop = 0; if (sizeof($order->get_items()) > 0) { $ITEMAMT = 0; foreach ($order->get_items() as $item) { $item['name'] = html_entity_decode($item['name'], ENT_NOQUOTES, 'UTF-8'); $_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(); } $item_meta = new WC_Order_Item_Meta($item['item_meta']); $meta = $item_meta->display(true, true); if (!empty($meta)) { $item['name'] .= " - " . str_replace(", \n", " - ", $meta); } } 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); } $PayPalRequestData['L_NUMBER' . $item_loop] = $sku; $PayPalRequestData['L_NAME' . $item_loop] = $item['name']; $PayPalRequestData['L_COST' . $item_loop] = $product_price; $PayPalRequestData['L_QTY' . $item_loop] = $item['qty']; if ($sku) { $PayPalRequestData['L_SKU' . $item_loop] = $sku; } $ITEMAMT += $product_price * $item['qty']; $item_loop++; } } //Cart Discount if ($order->get_cart_discount() > 0) { foreach (WC()->cart->get_coupons('cart') as $code => $coupon) { $PayPalRequestData['L_NUMBER' . $item_loop] = $code; $PayPalRequestData['L_NAME' . $item_loop] = 'Cart Discount'; $PayPalRequestData['L_AMT' . $item_loop] = '-' . WC()->cart->coupon_discount_amounts[$code]; $PayPalRequestData['L_QTY' . $item_loop] = 1; $item_loop++; } $ITEMAMT = $ITEMAMT - $order->get_cart_discount(); } //Order Discount if ($order->get_order_discount() > 0) { foreach (WC()->cart->get_coupons('order') as $code => $coupon) { $PayPalRequestData['L_NUMBER' . $item_loop] = $code; $PayPalRequestData['L_NAME' . $item_loop] = 'Order Discount'; $PayPalRequestData['L_AMT' . $item_loop] = '-' . WC()->cart->coupon_discount_amounts[$code]; $PayPalRequestData['L_QTY' . $item_loop] = 1; $item_loop++; } $ITEMAMT = $ITEMAMT - $order->get_order_discount(); } 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(); } //tax if ($tax > 0) { $PayPalRequestData['TAXAMT'] = $tax; } // Shipping if ($shipping > 0) { $PayPalRequestData['FREIGHTAMT'] = $shipping; } $PayPalRequestData['ITEMAMT'] = number_format($ITEMAMT, 2, '.', ''); } /** * Woo's original extension wasn't sending the request with * character count like it's supposed to. This was added * to fix that, but now that we're using my library it's * already handled correctly so this won't be necessary. */ /*foreach ($post_data as $key=>$value){ $send_data[]= $key."[".strlen($value)."]=$value"; } $send_data = implode("&", $send_data);*/ /** * Pass data to to the class and store the $PayPalResult */ $PayPalResult = $PayPal->ProcessTransaction($PayPalRequestData); /** * Log results */ $this->add_log('PayFlow Endpoint: ' . $PayPal->APIEndPoint); $this->add_log(print_r($PayPalResult, true)); /** * Error check */ if (empty($PayPalResult['RAWRESPONSE'])) { throw new Exception(__('Empty PayPal response.', 'wc_paypal_pro')); } /** * More logs */ $this->add_log(add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('thanks'))))); /** * Check for errors or fraud filter warnings and proceed accordingly. */ if (isset($PayPalResult['RESULT']) && ($PayPalResult['RESULT'] == 0 || $PayPalResult['RESULT'] == 126)) { // Add order note if ($PayPalResult['RESULT'] == 126) { $order->add_order_note($PayPalResult['RESPMSG']); $order->add_order_note($PayPalResult['PREFPSMSG']); $order->add_order_note("The payment was flagged by a fraud filter, please check your PayPal Manager account to review and accept or deny the payment."); } else { $order->add_order_note(sprintf(__('PayPal Pro payment completed (PNREF: %s)', 'wc_paypal_pro'), $PayPalResult['PNREF'])); } // 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 { // Payment failed :( $order->update_status('failed', __('PayPal Pro payment failed. Payment was rejected due to an error: ', 'wc_paypal_pro') . '(' . $parsed_response['RESULT'] . ') ' . '"' . $parsed_response['RESPMSG'] . '"'); wc_add_notice(__('Payment error:', 'wc_paypal_pro') . ' ' . $parsed_response['RESPMSG'], "error"); return; } } catch (Exception $e) { wc_add_notice(__('Connection error:', 'wc_paypal_pro') . ': "' . $e->getMessage() . '"', "error"); return; } }
<?php // Include required library files. require_once '../includes/config.php'; require_once '../includes/paypal.class.php'; require_once '../includes/paypal.payflow.class.php'; // Create PayPal object. $PayPalConfig = array('Sandbox' => $sandbox, 'APIUsername' => $payflow_username, 'APIPassword' => $payflow_password, 'APIVendor' => $payflow_vendor, 'APIPartner' => $payflow_partner); $PayPal = new PayPal_PayFlow($PayPalConfig); // Prepare request arrays $PayPalRequestData = array('tender' => '', 'trxtype' => '', 'acct' => '', 'expdate' => '', 'amt' => '', 'dutyamt' => '', 'freightamt' => '', 'taxamt' => '', 'taxexempt' => '', 'comment1' => '', 'comment2' => '', 'cvv2' => '', 'recurring' => '', 'swipe' => '', 'orderid' => '', 'billtoemail' => '', 'billtophonenum' => '', 'billtofirstname' => '', 'billtomiddlename' => '', 'billtolastname' => '', 'billtostreet' => '', 'billtocity' => '', 'billtostate' => '', 'billtozip' => '', 'billtocountry' => '', 'shiptofirstname' => '', 'shiptomiddlename' => '', 'shiptolastname' => '', 'shiptostreet' => '', 'shiptostate' => '', 'shiptozip' => '', 'shiptocountry' => '', 'origid' => '', 'custref' => '', 'custcode' => '', 'custip' => '', 'invnum' => '', 'ponum' => '', 'starttime' => '', 'endtime' => '', 'securetoken' => '', 'partialauth' => '', 'authcode' => ''); // Pass data into class for processing with PayPal and load the response array into $PayPalResult $PayPalResult = $PayPal->ProcessTransaction($PayPalRequestData); // Write the contents of the response array to the screen for demo purposes. echo '<pre />'; print_r($PayPalResult);