public static function createAddress()
 {
     $addr = new ShippingAddress();
     $addr->setLine1(self::$line1);
     $addr->setLine2(self::$line2);
     $addr->setCity(self::$city);
     $addr->setState(self::$state);
     $addr->setPostalCode(self::$postalCode);
     $addr->setCountryCode(self::$countryCode);
     $addr->setPhone(self::$phone);
     $addr->setRecipientName(self::$recipientName);
     return $addr;
 }
 function complete_cart($order_exists = true)
 {
     global $insert_id;
     // check
     $check_query = xtc_db_query("SELECT * \n                                   FROM " . TABLE_PAYPAL_PAYMENT . "\n                                  WHERE payment_id = '" . xtc_db_input($_SESSION['paypal']['paymentId']) . "'");
     if (xtc_db_num_rows($check_query) > 0) {
         $status_id = $this->order_status_tmp;
         if ($status_id < 0) {
             $check_query = xtc_db_query("SELECT orders_status\n                                         FROM " . TABLE_ORDERS . " \n                                        WHERE orders_id = '" . (int) $insert_id . "'");
             $check = xtc_db_fetch_array($check_query);
             $status_id = $check['orders_status'];
         }
         $this->update_order('duplicate call, cancel', $status_id, $insert_id);
         return;
     }
     // auth
     $apiContext = $this->apiContext();
     try {
         // Get the payment Object by passing paymentId
         $payment = Payment::get($_SESSION['paypal']['paymentId'], $apiContext);
     } catch (Exception $ex) {
         $this->LoggingManager->log(print_r($ex, true), 'DEBUG');
         $this->remove_order($insert_id);
         unset($_SESSION['paypal']);
         unset($_SESSION['tmp_oID']);
         xtc_redirect(xtc_href_link(FILENAME_SHOPPING_CART, 'payment_error=' . $this->code, 'NONSSL'));
     }
     // set order
     $order = new order($insert_id);
     $patches_array = array();
     $patchRequest = new PatchRequest();
     $payment_address = new Address();
     $payment_address->setLine1($this->encode_utf8($order->billing['street_address']))->setCity($this->encode_utf8($order->billing['city']))->setState($this->encode_utf8($order->billing['state'] != '' ? xtc_get_zone_code($order->billing['country_id'], $order->billing['zone_id'], $order->billing['state']) : ''))->setPostalCode($this->encode_utf8($order->billing['postcode']))->setCountryCode($this->encode_utf8(isset($order->billing['country_iso_2']) ? $order->billing['country_iso_2'] : $order->billing['country']['iso_code_2']));
     if ($order->billing['suburb'] != '') {
         $payment_address->setLine2($this->encode_utf8($order->billing['suburb']));
     }
     $patch_payment = new Patch();
     $patch_payment->setOp('add')->setPath('/potential_payer_info/billing_address')->setValue($payment_address);
     $patches_array[] = $patch_payment;
     // set address
     $shipping_address = new ShippingAddress();
     $shipping_address->setRecipientName($this->encode_utf8($order->delivery['firstname'] . ' ' . $order->delivery['lastname']))->setLine1($this->encode_utf8($order->delivery['street_address']))->setCity($this->encode_utf8($order->delivery['city']))->setCountryCode($this->encode_utf8(isset($order->delivery['country_iso_2']) ? $order->delivery['country_iso_2'] : $order->delivery['country']['iso_code_2']))->setPostalCode($this->encode_utf8($order->delivery['postcode']))->setState($this->encode_utf8($order->delivery['state'] != '' ? xtc_get_zone_code($order->delivery['country_id'], $order->delivery['zone_id'], $order->delivery['state']) : ''));
     if ($order->delivery['suburb'] != '') {
         $shipping_address->setLine2($this->encode_utf8($order->delivery['suburb']));
     }
     $patch_shipping = new Patch();
     $patch_shipping->setOp('add')->setPath('/transactions/0/item_list/shipping_address')->setValue($shipping_address);
     $patches_array[] = $patch_shipping;
     $patch_invoice = new Patch();
     $patch_invoice->setOp('replace')->setPath('/transactions/0/invoice_number')->setValue($this->get_config('PAYPAL_CONFIG_INVOICE_PREFIX') . $insert_id);
     $patches_array[] = $patch_invoice;
     // set details
     $this->details = new Details();
     // set amount
     $this->amount = new Amount();
     // set totals
     $this->get_totals($order->totals);
     $this->amount->setCurrency($order->info['currency'])->setDetails($this->details);
     $patch_amount = new Patch();
     $patch_amount->setOp('replace')->setPath('/transactions/0/amount')->setValue($this->amount);
     $patches_array[] = $patch_amount;
     // set ItemList
     if ($this->get_config('PAYPAL_ADD_CART_DETAILS') == '0' || $this->check_discount() === true) {
         $item = array();
         $item[0] = new Item();
         $item[0]->setName($this->encode_utf8(MODULE_PAYMENT_PAYPAL_TEXT_ORDER))->setCurrency($order->info['currency'])->setQuantity(1)->setPrice($this->details->getSubtotal());
     } else {
         for ($i = 0, $n = sizeof($order->products); $i < $n; $i++) {
             $item[$i] = new Item();
             $item[$i]->setName($this->encode_utf8($order->products[$i]['name']))->setCurrency($order->info['currency'])->setQuantity($order->products[$i]['qty'])->setPrice($order->products[$i]['price'])->setSku($order->products[$i]['model'] != '' ? $order->products[$i]['model'] : $order->products[$i]['id']);
         }
     }
     $patch_items = new Patch();
     $patch_items->setOp('replace')->setPath('/transactions/0/item_list/items')->setValue($item);
     $patches_array[] = $patch_items;
     $patchRequest->setPatches($patches_array);
     try {
         // update payment
         $payment->update($patchRequest, $apiContext);
     } catch (Exception $ex) {
         $this->LoggingManager->log(print_r($ex, true), 'DEBUG');
         if ($order_exists === false) {
             unset($_SESSION['paypal']);
             xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=' . $this->code, 'SSL'));
         }
         $this->remove_order($insert_id);
         unset($_SESSION['paypal']);
         unset($_SESSION['tmp_oID']);
         xtc_redirect(xtc_href_link(FILENAME_SHOPPING_CART, 'payment_error=' . $this->code, 'NONSSL'));
     }
     $payment = Payment::get($_SESSION['paypal']['paymentId'], $apiContext);
     // PaymentExecution
     $execution = new PaymentExecution();
     $execution->setPayerId($_SESSION['paypal']['PayerID']);
     try {
         // Execute the payment
         $payment->execute($execution, $apiContext);
     } catch (Exception $ex) {
         $this->LoggingManager->log(print_r($ex, true), 'DEBUG');
         $this->remove_order($insert_id);
         unset($_SESSION['paypal']);
         unset($_SESSION['tmp_oID']);
         xtc_redirect(xtc_href_link(FILENAME_SHOPPING_CART, 'payment_error=' . $this->code, 'NONSSL'));
     }
     // capture
     if (($this->transaction_type == 'order' || $this->transaction_type == 'authorize') && $this->get_config('PAYPAL_CAPTURE_MANUELL') == '0') {
         $this->capture_payment($payment);
     }
     $sql_data_array = array('orders_id' => $insert_id, 'payment_id' => $_SESSION['paypal']['paymentId'], 'payer_id' => $_SESSION['paypal']['PayerID']);
     xtc_db_perform(TABLE_PAYPAL_PAYMENT, $sql_data_array);
     try {
         // Get the payment Object by passing paymentId
         $payment = Payment::get($_SESSION['paypal']['paymentId'], $apiContext);
     } catch (Exception $ex) {
         $this->LoggingManager->log(print_r($ex, true), 'DEBUG');
         $this->remove_order($insert_id);
         unset($_SESSION['paypal']);
         unset($_SESSION['tmp_oID']);
         xtc_redirect(xtc_href_link(FILENAME_SHOPPING_CART, 'payment_error=' . $this->code, 'NONSSL'));
     }
     $status = $this->get_orders_status($payment);
     if ($status['status_id'] < 0) {
         $check_query = xtc_db_query("SELECT orders_status\n                                     FROM " . TABLE_ORDERS . " \n                                    WHERE orders_id = '" . (int) $insert_id . "'");
         $check = xtc_db_fetch_array($check_query);
         $status['status_id'] = $check['orders_status'];
     }
     $this->update_order($status['comment'], $status['status_id'], $insert_id);
 }
 function generateShippingAddress($addressee, $address)
 {
     $address = new ShippingAddress();
     $address->setRecipientName($addressee);
     $address->setLine1($address->line1);
     $address->setLine2($address->line2);
     $address->setCity($address->city);
     $address->setState($address->province->name);
     $address->setCountryCode($address->country->name);
     $address->setPostalCode($address->zip);
     return $address;
 }