Пример #1
0
 /**
  * Use this to do the final payment. Create the order then process the payment. If
  *  you know the payment is successful right away go ahead and change the order status
  *  as well.
  *  Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
  *  it will redirect to the next step.
  *
  * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  * @param array $shipping_info. Contains shipping info and email in case you need it
  */
 function process_payment($cart, $shipping_info)
 {
     global $mp;
     $timestamp = time();
     $settings = get_option('mp_settings');
     $billing_info = $_SESSION['mp_billing_info'];
     $payment = new MP_Gateway_Worker_AuthorizeNet_AIM($this->API_Endpoint, $settings['gateways']['authorizenet-aim']['delim_data'], $settings['gateways']['authorizenet-aim']['delim_char'], $settings['gateways']['authorizenet-aim']['encap_char'], $settings['gateways']['authorizenet-aim']['api_user'], $settings['gateways']['authorizenet-aim']['api_key'], $settings['gateways']['authorizenet-aim']['mode'] == 'sandbox');
     $payment->transaction($_SESSION['card_num']);
     $totals = array();
     $coupon_code = $mp->get_coupon_code();
     foreach ($cart as $product_id => $variations) {
         foreach ($variations as $variation => $data) {
             $sku = empty($data['SKU']) ? "{$product_id}_{$variation}" : $data['SKU'];
             //total on tax excluded
             $price = $mp->coupon_value_product($coupon_code, $data['price'] * $data['quantity'], $product_id);
             $totals[] = $price;
             //display as tax inclusive
             $payment->addLineItem($sku, substr($data['name'], 0, 31), substr($data['name'] . ' - ' . $data['url'], 0, 254), $data['quantity'], $data['price'], 1);
             $i++;
         }
     }
     $total = array_sum($totals);
     //shipping line
     $shipping_tax = 0;
     if (($shipping_price = $mp->shipping_price(false)) !== false) {
         $total += $shipping_price;
         $shipping_tax = $mp->shipping_tax_price($shipping_price) - $shipping_price;
     }
     //tax line if tax inclusive pricing is off. It it's on it would screw up the totals
     if (!$mp->get_setting('tax->tax_inclusive')) {
         $tax_price = $mp->tax_price(false) + $shipping_tax;
         $total += $tax_price;
     }
     // Billing Info
     $payment->setParameter("x_card_code", $_SESSION['card_code']);
     $payment->setParameter("x_exp_date ", $_SESSION['exp_month'] . $_SESSION['exp_year']);
     $payment->setParameter("x_amount", $total);
     $payment->setParameter("x_currency_code", $this->currencyCode);
     // Order Info
     $payment->setParameter("x_description", "Order ID: " . $_SESSION['mp_order']);
     $payment->setParameter("x_invoice_num", $_SESSION['mp_order']);
     $payment->setParameter("x_test_request", false);
     // this should NEVER be true, even in sandbox mode
     $payment->setParameter("x_duplicate_window", 30);
     // E-mail
     $payment->setParameter("x_header_email_receipt", $settings['gateways']['authorizenet-aim']['header_email_receipt']);
     $payment->setParameter("x_footer_email_receipt", $settings['gateways']['authorizenet-aim']['footer_email_receipt']);
     $payment->setParameter("x_email_customer", strtoupper($settings['gateways']['authorizenet-aim']['email_customer']));
     $_names = explode(" ", $billing_info['name']);
     if (isset($_names[0])) {
         $first_name = array_shift($_names);
     } else {
         $first_name = "";
     }
     if (isset($_names[0])) {
         $last_name = join(" ", $_names);
     } else {
         $last_name = "";
     }
     $address = $billing_info['address1'];
     if (!empty($billing_info['address2'])) {
         $address .= "\n" . $billing_info['address2'];
     }
     //Customer Info
     $payment->setParameter("x_first_name", $first_name);
     $payment->setParameter("x_last_name", $last_name);
     $payment->setParameter("x_address", $address);
     $payment->setParameter("x_city", $billing_info['city']);
     $payment->setParameter("x_state", $billing_info['state']);
     $payment->setParameter("x_country", $billing_info['country']);
     $payment->setParameter("x_zip", $billing_info['zip']);
     $payment->setParameter("x_phone", $billing_info['phone']);
     $payment->setParameter("x_email", $billing_info['email']);
     //only add shipping info if set
     if (!$mp->download_only_cart($cart) && isset($shipping_info['name'])) {
         $_names = explode(" ", $shipping_info['name']);
         if (isset($_names[0])) {
             $shipping_first_name = array_shift($_names);
         } else {
             $shipping_first_name = "";
         }
         if (isset($_names[0])) {
             $shipping_last_name = join(" ", $_names);
         } else {
             $shipping_last_name = "";
         }
         $shipping_address = $shipping_info['address1'];
         if (!empty($billing_info['address2'])) {
             $shipping_address .= "\n" . $shipping_info['address2'];
         }
         $payment->setParameter("x_ship_to_first_name", $shipping_first_name);
         $payment->setParameter("x_ship_to_last_name", $shipping_last_name);
         $payment->setParameter("x_ship_to_address", $shipping_address);
         $payment->setParameter("x_ship_to_city", $shipping_info['city']);
         $payment->setParameter("x_ship_to_state", $shipping_info['state']);
         $payment->setParameter("x_ship_to_country", $shipping_info['country']);
         $payment->setParameter("x_ship_to_zip", $shipping_info['zip']);
     }
     $payment->setParameter("x_customer_ip", $_SERVER['REMOTE_ADDR']);
     $payment->process();
     if ($payment->isApproved()) {
         $paid = $payment->isHeldForReview() ? false : true;
         $payment_info['gateway_public_name'] = $this->public_name;
         $payment_info['gateway_private_name'] = $this->admin_name;
         $payment_info['method'] = $payment->getMethod();
         $payment_info['status'][$timestamp] = $payment->isHeldForReview() ? __('held for review', 'mp') : __('paid', 'mp');
         $payment_info['total'] = $total;
         $payment_info['currency'] = $this->currencyCode;
         $payment_info['transaction_id'] = $payment->getTransactionID();
         //succesful payment, create our order now
         $result = $mp->create_order($_SESSION['mp_order'], $cart, $shipping_info, $payment_info, $paid);
     } else {
         $error = $payment->getResponseText();
         $mp->cart_checkout_error(sprintf(__('There was a problem finalizing your purchase. %s Please <a href="%s">go back and try again</a>.', 'mp'), $error, mp_checkout_step_url('checkout')));
     }
 }
Пример #2
0
 /**
  * Use this to do the final payment. Create the order then process the payment. If
  *  you know the payment is successful right away go ahead and change the order status
  *  as well.
  *  Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
  *  it will redirect to the next step.
  *
  * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  * @param array $shipping_info. Contains shipping info and email in case you need it
  */
 function process_payment($cart, $shipping_info)
 {
     global $mp;
     $timestamp = time();
     $settings = get_option('mp_settings');
     $billing_info = $_SESSION['mp_billing_info'];
     $payment = new MP_Gateway_Worker_AuthorizeNet_AIM($this->API_Endpoint, $settings['gateways']['authorizenet-aim']['delim_data'], $settings['gateways']['authorizenet-aim']['delim_char'], $settings['gateways']['authorizenet-aim']['encap_char'], $settings['gateways']['authorizenet-aim']['api_user'], $settings['gateways']['authorizenet-aim']['api_key'], $settings['gateways']['authorizenet-aim']['mode'] == 'sandbox');
     $payment->transaction($_SESSION['card_num']);
     $totals = array();
     foreach ($cart as $product_id => $variations) {
         foreach ($variations as $variation => $data) {
             $sku = empty($data['SKU']) ? "{$product_id}_{$variation}" : $data['SKU'];
             $totals[] = $mp->before_tax_price($data['price'], $product_id) * $data['quantity'];
             $payment->addLineItem($sku, substr($data['name'], 0, 31), substr($data['name'] . ' - ' . $data['url'], 0, 254), $data['quantity'], $mp->before_tax_price($data['price'], $product_id), 1);
             $i++;
         }
     }
     $total = array_sum($totals);
     //coupon line
     if ($coupon = $mp->coupon_value($mp->get_coupon_code(), $total)) {
         $total = $coupon['new_total'];
     }
     //shipping line
     if (($shipping_price = $mp->shipping_price()) !== false) {
         $total = $total + $shipping_price;
     }
     //tax line
     if (($tax_price = $mp->tax_price()) !== false) {
         $total = $total + $tax_price;
     }
     // Billing Info
     $payment->setParameter("x_card_code", $_SESSION['card_code']);
     $payment->setParameter("x_exp_date ", $_SESSION['exp_month'] . $_SESSION['exp_year']);
     $payment->setParameter("x_amount", $total);
     // Order Info
     $payment->setParameter("x_description", "Order ID: " . $_SESSION['mp_order']);
     $payment->setParameter("x_invoice_num", $_SESSION['mp_order']);
     if ($settings['gateways']['authorizenet-aim']['mode'] == 'sandbox') {
         $payment->setParameter("x_test_request", true);
     } else {
         $payment->setParameter("x_test_request", false);
     }
     $payment->setParameter("x_duplicate_window", 30);
     // E-mail
     $payment->setParameter("x_header_email_receipt", $settings['gateways']['authorizenet-aim']['header_email_receipt']);
     $payment->setParameter("x_footer_email_receipt", $settings['gateways']['authorizenet-aim']['footer_email_receipt']);
     $payment->setParameter("x_email_customer", strtoupper($settings['gateways']['authorizenet-aim']['email_customer']));
     $_names = split(" ", $billing_info['name']);
     if (isset($_names[0])) {
         $first_name = array_shift($_names);
     } else {
         $first_name = "";
     }
     if (isset($_names[0])) {
         $last_name = join(" ", $_names);
     } else {
         $last_name = "";
     }
     $address = $billing_info['address1'];
     if (!empty($billing_info['address2'])) {
         $address .= "\n" . $billing_info['address2'];
     }
     //Customer Info
     $payment->setParameter("x_first_name", $first_name);
     $payment->setParameter("x_last_name", $last_name);
     $payment->setParameter("x_address", $address);
     $payment->setParameter("x_city", $billing_info['city']);
     $payment->setParameter("x_state", $billing_info['state']);
     $payment->setParameter("x_country", $billing_info['country']);
     $payment->setParameter("x_zip", $billing_info['zip']);
     $payment->setParameter("x_phone", $billing_info['phone']);
     $payment->setParameter("x_email", $billing_info['email']);
     //only add shipping info if set
     if (!$mp->download_only_cart($cart) && isset($shipping_info['name'])) {
         $_names = split(" ", $shipping_info['name']);
         if (isset($_names[0])) {
             $shipping_first_name = array_shift($_names);
         } else {
             $shipping_first_name = "";
         }
         if (isset($_names[0])) {
             $shipping_last_name = join(" ", $_names);
         } else {
             $shipping_last_name = "";
         }
         $shipping_address = $shipping_info['address1'];
         if (!empty($billing_info['address2'])) {
             $shipping_address .= "\n" . $shipping_info['address2'];
         }
         $payment->setParameter("x_ship_to_first_name", $shipping_first_name);
         $payment->setParameter("x_ship_to_last_name", $shipping_last_name);
         $payment->setParameter("x_ship_to_address", $shipping_address);
         $payment->setParameter("x_ship_to_city", $shipping_info['city']);
         $payment->setParameter("x_ship_to_state", $shipping_info['state']);
         $payment->setParameter("x_ship_to_country", $shipping_info['country']);
         $payment->setParameter("x_ship_to_zip", $shipping_info['zip']);
     }
     $payment->setParameter("x_customer_ip", $_SERVER['REMOTE_ADDR']);
     $payment->process();
     if ($payment->isApproved()) {
         $status = __('The payment has been completed, and the funds have been added successfully to your account balance.', 'mp');
         $paid = true;
         $payment_info['gateway_public_name'] = $this->public_name;
         $payment_info['gateway_private_name'] = $this->admin_name;
         $payment_info['method'] = $payment->getMethod();
         $payment_info['status'][$timestamp] = "paid";
         $payment_info['total'] = $total;
         $payment_info['currency'] = "USD";
         // Authorize.net only supports USD transactions
         $payment_info['transaction_id'] = $payment->getTransactionID();
         //succesful payment, create our order now
         $result = $mp->create_order($_SESSION['mp_order'], $cart, $shipping_info, $payment_info, $paid);
     } else {
         $error = $payment->getResponseText();
         $mp->cart_checkout_error(sprintf(__('There was a problem finalizing your purchase. %s Please <a href="%s">go back and try again</a>.', 'mp'), $error, mp_checkout_step_url('checkout')));
     }
 }