function isxwpe_add_xero_invoice_to_account() { // Function to add invoices to xero account global $wpdb; // This if statement start when user complete checkout if (isset($_REQUEST['wpsc_action']) && $_REQUEST['wpsc_action'] == 'submit_checkout') { $samp_array = array(); $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "xero_auth"); // Collect auth credentials if (!empty($results)) { foreach ($results as $result) { if ($result->credential == 'application_key') { $application_key = $result->value; } if ($result->credential == 'secret_key') { $secret_key = $result->value; } if ($result->credential == 'redirect_url') { $redirect_url = $result->value; } if ($result->credential == 'oauth_token') { $oauth_token = $result->value; } if ($result->credential == 'oauth_verifier') { $oauth_verifier = $result->value; } if ($result->credential == 'oauth_token_secret') { $oauth_token_secret = $result->value; } } } $checkout_session_id = wpsc_get_customer_meta('checkout_session_id'); // Find transaction Id $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_purchase_logs", ARRAY_A); // Collect transaction details foreach ($results as $temp_results) { if ($temp_results['sessionid'] == $checkout_session_id) { $samp_array = $temp_results; } } if (!empty($samp_array)) { $purchase_id = $samp_array['id']; $total_price = $samp_array['totalprice']; $user_id = $samp_array['user_ID']; $user_email = ''; $name = ''; $user_details = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_submited_form_data WHERE `log_id` = {$purchase_id}", ARRAY_A); foreach ($user_details as $user_data) { if (is_email($user_data['value'])) { $user_email = $user_data['value']; } if (isset($user_data['form_id']) && $user_data['form_id'] == 2) { $name = $name . $user_data['value']; } if (isset($user_data['form_id']) && $user_data['form_id'] == 3) { $name = $name . ' ' . $user_data['value']; } } } $currency = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "options WHERE `option_name` ='currency_type'"); $currency_id = $currency[0]->option_value; $currency_details = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_currency_list WHERE id = {$currency_id}"); $currency_code = $currency_details[0]->code; // Set currency code for xero same as currency on WPecommerse plugin $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_cart_contents WHERE purchaseid = {$purchase_id} "); // Select product details $purchased_items = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_cart_contents WHERE purchaseid = {$purchase_id} "); $wp_wpsc_purchase_logs = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}" . "wpsc_purchase_logs WHERE id = {$purchase_id} "); $wp_wpsc_purchase_log = $wp_wpsc_purchase_logs[0]; $line_items = ''; foreach ($purchased_items as $selected_items) { //print_r($purchased_items); $line_items .= '<LineItem> <Description>' . $selected_items->name . '</Description> <Quantity>' . $selected_items->quantity . '</Quantity> <TaxAmount>' . $selected_items->tax_charged . '</TaxAmount> <UnitAmount>' . $selected_items->price . '</UnitAmount> </LineItem>'; } require plugin_dir_path(__FILE__) . 'xero_library/lib/XeroOAuth.php'; define("XRO_APP_TYPE", "Public"); $useragent = "Xero-OAuth-PHP Public"; define("OAUTH_CALLBACK", $redirect_url); $signatures = array('consumer_key' => $application_key, 'shared_secret' => $secret_key, 'core_version' => '2.0', 'payroll_version' => '1.0', 'file_version' => '1.0', 'access_token' => $oauth_token, 'access_token_secret' => $oauth_token_secret); $target_directorys = wp_upload_dir(); $target_directory = $target_directorys['basedir']; if (XRO_APP_TYPE == "Private" || XRO_APP_TYPE == "Public") { $signatures['rsa_private_key'] = $target_directory . '/xero_invoice/private_keys/privatekey.pem'; $signatures['rsa_public_key'] = $target_directory . '/xero_invoice/private_keys/publickey.cer'; } $XeroOAuth = new XeroOAuth(array_merge(array('application_type' => XRO_APP_TYPE, 'oauth_callback' => OAUTH_CALLBACK, 'user_agent' => $useragent), $signatures)); $initialCheck = $XeroOAuth->diagnostics(); $today = date('Y-m-d'); include plugin_dir_path(__FILE__) . 'tests/testRunner.php'; $xml = "<Invoices>\n <Invoice>\n <Type>ACCREC</Type>\n\t\t\t\t\t\t<CurrencyCode>{$currency_code}</CurrencyCode>\n <Contact>\n <Name>{$name}</Name>\n\t\t\t\t\t\t <EmailAddress>{$user_email}</EmailAddress>\n </Contact>\n <Date>{$today}</Date>\n <LineAmountTypes>Exclusive</LineAmountTypes>\n <LineItems>\n {$line_items}\n </LineItems>\n </Invoice>\n </Invoices>"; //$myfile = fopen(WP_PLUGIN_DIR."/invoice-sync-for-xero-and-wpecommerce/XerSyncLog.txt", "w") or die("Unable to open file!"); $response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array(), $xml); if ($XeroOAuth->response['code'] == 200) { // Add transaction details to DB id transaction is success $invoice = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']); $invoice_id = $invoice->Invoices[0]->Invoice->InvoiceNumber; $wpdb->insert("{$wpdb->prefix}" . "xero_history", array('session_id' => $checkout_session_id, 'purchase_id' => $purchase_id, 'invoice_id' => $invoice_id, 'user_email' => $user_email), array('%d', '%d', '%s', '%s')); } else { //outputError($XeroOAuth); } } }
$initialCheck = $XeroOAuth->diagnostics(); $checkErrors = count($initialCheck); if ($checkErrors > 0) { // you could handle any config errors here, or keep on truckin if you like to live dangerously foreach ($initialCheck as $check) { echo 'Error: ' . $check . PHP_EOL; } } else { $here = XeroOAuth::php_self(); session_start(); $oauthSession = retrieveSession(); include 'tests/tests.php'; if (isset($_REQUEST['oauth_verifier'])) { $XeroOAuth->config['access_token'] = $_SESSION['oauth']['oauth_token']; $XeroOAuth->config['access_token_secret'] = $_SESSION['oauth']['oauth_token_secret']; $code = $XeroOAuth->request('GET', $XeroOAuth->url('AccessToken', ''), array('oauth_verifier' => $_REQUEST['oauth_verifier'], 'oauth_token' => $_REQUEST['oauth_token'])); if ($XeroOAuth->response['code'] == 200) { $response = $XeroOAuth->extract_params($XeroOAuth->response['response']); $session = persistSession($response); unset($_SESSION['oauth']); header("Location: {$here}"); } else { outputError($XeroOAuth); } // start the OAuth dance } elseif (isset($_REQUEST['authenticate']) || isset($_REQUEST['authorize'])) { $params = array('oauth_callback' => OAUTH_CALLBACK); $response = $XeroOAuth->request('GET', $XeroOAuth->url('RequestToken', ''), $params); if ($XeroOAuth->response['code'] == 200) { $scope = ""; // $scope = 'payroll.payrollcalendars,payroll.superfunds,payroll.payruns,payroll.payslip,payroll.employees,payroll.TaxDeclaration';
include 'tests/testRunner.php'; $initialCheck = $XeroOAuth->diagnostics(); $checkErrors = count($initialCheck); if ($checkErrors > 0) { // you could handle any config errors here, or keep on truckin if you like to live dangerously foreach ($initialCheck as $check) { echo 'Error: ' . $check . PHP_EOL; } } else { $session = persistSession(array('oauth_token' => $XeroOAuth->config['consumer_key'], 'oauth_token_secret' => $XeroOAuth->config['shared_secret'], 'oauth_session_handle' => '')); $oauthSession = retrieveSession(); if (isset($oauthSession['oauth_token'])) { $XeroOAuth->config['access_token'] = $oauthSession['oauth_token']; $XeroOAuth->config['access_token_secret'] = $oauthSession['oauth_token_secret']; // include 'tests/xero_api.php'; if ($event_type == "invoice_generated") { $response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array(), $xml); if ($XeroOAuth->response['code'] == 200) { $invoice = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']); $response = $XeroOAuth->request('PUT', $XeroOAuth->url('Payments', 'core'), array(), $xml_pymnts, 'JSON'); if ($XeroOAuth->response['code'] == 200) { $payment = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']); } else { outputError($XeroOAuth); } } else { outputError($XeroOAuth); } } } }