예제 #1
0
 /**
  * Overrided process payment for Authorize.net
  *
  * @global object $invoice
  * @global array $wpi_settings
  * @param array $data
  */
 static function process_payment($data = null)
 {
     global $invoice, $wpi_settings;
     //** Require our external libraries */
     require_once WPI_Path . '/third-party/authorize.net/authnet.class.php';
     require_once WPI_Path . '/third-party/authorize.net/authnetARB.class.php';
     // Pull in the CCard data from the request, and other variables we'll use
     // If data passed then use it. Otherwise use data from request.
     // It used to make available to do payment processes by WPI_Payment_Api
     $cc_data = is_null($data) ? $_REQUEST['cc_data'] : $data;
     $invoice_id = $invoice['invoice_id'];
     $wp_users_id = $invoice['user_data']['ID'];
     $post_id = wpi_invoice_id_to_post_id($invoice_id);
     //** Recurring */
     $recurring = $invoice['type'] == 'recurring' ? true : false;
     //** Response */
     $response = array('success' => false, 'error' => false, 'data' => null);
     //** Invoice custom id which is sending to authorize.net */
     $cc_data['invoice_id'] = $invoice_id;
     $invoice_obj = new WPI_Invoice();
     $invoice_obj->load_invoice("id={$invoice['invoice_id']}");
     if ($invoice['deposit_amount'] > 0) {
         $amount = (double) $cc_data['amount'];
         if ((double) $cc_data['amount'] > $invoice['net']) {
             $amount = $invoice['net'];
         }
         if ((double) $cc_data['amount'] < $invoice['deposit_amount']) {
             $amount = $invoice['deposit_amount'];
         }
     } else {
         $amount = $invoice['net'];
     }
     //** We assume that all data is good to go, considering we are valadating with JavaScript */
     $payment = new WP_Invoice_Authnet();
     $payment->transaction($cc_data['card_num']);
     //** Billing Info */
     $payment->setParameter("x_card_code", $cc_data['card_code']);
     $payment->setParameter("x_exp_date ", $cc_data['exp_month'] . $cc_data['exp_year']);
     $payment->setParameter("x_amount", $amount);
     $payment->setParameter("x_currency_code", $cc_data['currency_code']);
     if ($recurring) {
         $payment->setParameter("x_recurring_billing", true);
     }
     //** Order Info */
     $payment->setParameter("x_description", $invoice['post_title']);
     $payment->setParameter("x_invoice_id", $invoice['invoice_id']);
     $payment->setParameter("x_duplicate_window", 30);
     //** Customer Info */
     $payment->setParameter("x_first_name", $cc_data['first_name']);
     $payment->setParameter("x_last_name", $cc_data['last_name']);
     $payment->setParameter("x_address", $cc_data['streetaddress']);
     $payment->setParameter("x_city", $cc_data['city']);
     $payment->setParameter("x_state", $cc_data['state']);
     $payment->setParameter("x_country", $cc_data['country']);
     $payment->setParameter("x_zip", $cc_data['zip']);
     $payment->setParameter("x_phone", $cc_data['phonenumber']);
     $payment->setParameter("x_email", $cc_data['user_email']);
     $payment->setParameter("x_cust_id", "WP User - " . $wp_users_id);
     $payment->setParameter("x_customer_ip ", $_SERVER['REMOTE_ADDR']);
     //** Process */
     $payment->process();
     //** Process results */
     if ($payment->isApproved()) {
         update_user_meta($wp_users_id, 'last_name', $cc_data['last_name']);
         update_user_meta($wp_users_id, 'first_name', $cc_data['first_name']);
         update_user_meta($wp_users_id, 'city', $cc_data['city']);
         update_user_meta($wp_users_id, 'state', $cc_data['state']);
         update_user_meta($wp_users_id, 'zip', $cc_data['zip']);
         update_user_meta($wp_users_id, 'streetaddress', $cc_data['streetaddress']);
         update_user_meta($wp_users_id, 'phonenumber', $cc_data['phonenumber']);
         update_user_meta($wp_users_id, 'country', $cc_data['country']);
         do_action('wpi_authorize_user_meta_updated', $cc_data);
         //** Add payment amount */
         $event_note = WPI_Functions::currency_format($amount, $invoice['invoice_id']) . " paid via Authorize.net";
         $event_amount = $amount;
         $event_type = 'add_payment';
         $event_note = urlencode($event_note);
         //** Log balance changes */
         $invoice_obj->add_entry("attribute=balance&note={$event_note}&amount={$event_amount}&type={$event_type}");
         //** Log client IP */
         $success = "Successfully processed by {$_SERVER['REMOTE_ADDR']}";
         $invoice_obj->add_entry("attribute=invoice&note={$success}&type=update");
         //** Log payer email */
         $payer_email = "Authorize.net Payer email: {$cc_data['user_email']}";
         $invoice_obj->add_entry("attribute=invoice&note={$payer_email}&type=update");
         $invoice_obj->save_invoice();
         //** Mark invoice as paid */
         wp_invoice_mark_as_paid($invoice_id, $check = true);
         send_notification($invoice);
         $data['messages'][] = $payment->getResponseText();
         $response['success'] = true;
         $response['error'] = false;
         if ($recurring) {
             $arb = new WP_Invoice_AuthnetARB($invoice);
             //** Customer Info */
             $arb->setParameter('customerId', "WP User - " . $invoice['user_data']['ID']);
             $arb->setParameter('firstName', !empty($cc_data['first_name']) ? $cc_data['first_name'] : '-');
             $arb->setParameter('lastName', !empty($cc_data['last_name']) ? $cc_data['last_name'] : '-');
             $arb->setParameter('address', !empty($cc_data['streetaddress']) ? $cc_data['streetaddress'] : '-');
             $arb->setParameter('city', !empty($cc_data['city']) ? $cc_data['city'] : '-');
             $arb->setParameter('state', !empty($cc_data['state']) ? $cc_data['state'] : '-');
             $arb->setParameter('zip', !empty($cc_data['zip']) ? $cc_data['zip'] : '-');
             $arb->setParameter('country', !empty($cc_data['country']) ? $cc_data['country'] : '-');
             $arb->setParameter('customerEmail', !empty($cc_data['user_email']) ? $cc_data['user_email'] : '-');
             $arb->setParameter('customerPhoneNumber', !empty($cc_data['phonenumber']) ? $cc_data['phonenumber'] : '-');
             //** Billing Info */
             $arb->setParameter('amount', $invoice['net']);
             $arb->setParameter('cardNumber', $cc_data['card_num']);
             $arb->setParameter('expirationDate', $cc_data['exp_month'] . $cc_data['exp_year']);
             //** Subscription Info */
             $arb->setParameter('refID', $invoice['invoice_id']);
             $arb->setParameter('subscrName', $invoice['post_title']);
             $arb->setParameter('interval_length', $invoice['recurring']['wpi_authorize']['length']);
             $arb->setParameter('interval_unit', $invoice['recurring']['wpi_authorize']['unit']);
             //** format: yyyy-mm-dd */
             if ($invoice['recurring']['wpi_authorize']['send_invoice_automatically'] == 'on') {
                 $arb->setParameter('startDate', date("Y-m-d", time()));
             } else {
                 $arb->setParameter('startDate', $invoice['recurring']['wpi_authorize']['start_date']['year'] . '-' . $invoice['recurring']['wpi_authorize']['start_date']['month'] . '-' . $invoice['recurring']['wpi_authorize']['start_date']['day']);
             }
             $arb->setParameter('totalOccurrences', $invoice['recurring']['wpi_authorize']['cycles']);
             $arb->setParameter('trialOccurrences', 1);
             $arb->setParameter('trialAmount', '0.00');
             $arb->setParameter('orderInvoiceNumber', $invoice['invoice_id']);
             $arb->setParameter('orderDescription', $invoice['post_title']);
             $arb->createAccount();
             if ($arb->isSuccessful()) {
                 update_post_meta($post_id, 'subscription_id', $arb->getSubscriberID());
                 WPI_Functions::log_event($post_id, 'invoice', 'update', '', __('Subscription initiated, Subcription ID', WPI) . ' - ' . $arb->getSubscriberID());
                 $data['messages'][] = "Recurring Billing Subscription initiated";
                 $response['success'] = true;
                 $response['error'] = false;
             }
             if ($arb->isError()) {
                 $data['messages'][] = __('One-time credit card payment is processed successfully. However, recurring billing setup failed. ', WPI) . $arb->getResponse();
                 $response['success'] = false;
                 $response['error'] = true;
                 WPI_Functions::log_event($post_id, 'invoice', 'update', '', __('Response Code: ', WPI) . $arb->getResponseCode() . ' | ' . __('Subscription error', WPI) . ' - ' . $arb->getResponse());
             }
         }
     } else {
         $response['success'] = false;
         $response['error'] = true;
         $data['messages'][] = $payment->getResponseText();
     }
     $response['data'] = $data;
     die(json_encode($response));
 }
예제 #2
0
 /**
  * Runs selected method proccess
  * @global array $wpi_settings
  * @param array $args
  */
 private function run_method($args)
 {
     global $wpi_settings;
     $this->name = $args['venue'];
     $this->method = array_key_exists($args['venue'], $this->defaults) ? $this->defaults[$args['venue']] : $this->defaults[$this->name = key($this->defaults)];
     $this->settings = $wpi_settings['billing'][$this->name]['settings'];
     $this->items = !empty($args['items']) ? $args['items'] : array();
     if (!empty($this->method) && is_array($this->method)) {
         switch ($this->name) {
             case self::WPI_METHOD_AUTHORIZE_NET:
                 $this->url = $this->settings['gateway_url']['value'];
                 $this->method['x_login'] = $this->settings['gateway_username']['value'];
                 $this->method['x_tran_key'] = $this->settings['gateway_tran_key']['value'];
                 $this->method['x_delim_data'] = $this->settings['gateway_delim_data']['value'];
                 $this->method['x_delim_char'] = $this->settings['gateway_delim_char']['value'];
                 $this->method['x_encap_char'] = $this->settings['gateway_encap_char']['value'];
                 $this->method['x_test_request'] = $this->settings['gateway_test_mode']['value'];
                 $this->method['x_card_num'] = !empty($args['cc_number']) ? $args['cc_number'] : '';
                 $this->method['x_card_code'] = !empty($args['cc_code']) ? $args['cc_code'] : '';
                 $this->method['x_exp_date'] = !empty($args['cc_expiration']) ? $args['cc_expiration'] : '';
                 $this->method['x_currency_code'] = !empty($args['currency_code']) ? $args['currency_code'] : '';
                 $this->method['x_email'] = !empty($args['payer_email']) ? $args['payer_email'] : '';
                 $this->method['x_description'] = !empty($args['description']) ? stripslashes($args['description']) : '';
                 $this->method['x_first_name'] = !empty($args['payer_first_name']) ? stripslashes($args['payer_first_name']) : '';
                 $this->method['x_last_name'] = !empty($args['payer_last_name']) ? stripslashes($args['payer_last_name']) : '';
                 $this->method['x_amount'] = !empty($args['amount']) ? $args['amount'] : '0';
                 $this->method['x_company'] = !empty($args['company']) ? stripslashes($args['company']) : '';
                 $this->method['x_address'] = !empty($args['address']) ? stripslashes($args['address']) : '';
                 $this->method['x_city'] = !empty($args['city']) ? stripslashes($args['city']) : '';
                 $this->method['x_state'] = !empty($args['state']) ? $args['state'] : '';
                 $this->method['x_zip'] = !empty($args['zip']) ? $args['zip'] : '';
                 $this->method['x_country'] = !empty($args['country']) ? stripslashes($args['country']) : '';
                 $this->method['x_phone'] = !empty($args['phone']) ? $args['phone'] : '';
                 $this->method['x_fax'] = !empty($args['fax']) ? $args['fax'] : '';
                 require_once WPI_Path . '/third-party/authorize.net/authnet.class.php';
                 $transaction = new WP_Invoice_Authnet($this->method);
                 $transaction->setUrl($this->url);
                 $transaction->addItems($this->items);
                 $transaction->process();
                 $this->response['payment_status'] = $transaction->isApproved() ? self::WPI_METHOD_STATUS_COMPLETE : self::WPI_METHOD_STATUS_ERROR;
                 if (!$transaction->isApproved()) {
                     $this->response['error_message'] = $transaction->getResponseText();
                 }
                 $this->response['receiver_email'] = $this->method['x_email'];
                 $this->response['transaction_id'] = $transaction->getTransactionID();
                 $this->response['payment_method'] = self::WPI_METHOD_AUTHORIZE_NET;
                 break;
             case self::WPI_METHOD_PAYPAL:
                 $this->response['payment_status'] = self::WPI_METHOD_STATUS_COMPLETE;
                 $this->response['receiver_email'] = !empty($args['payer_email']) ? $args['payer_email'] : '';
                 $this->response['payment_method'] = self::WPI_METHOD_PAYPAL;
                 break;
             default:
                 break;
         }
     }
 }