<?php
include_once './libs/paymorrow/paymorrowProxyUtil.php';

$data = $HTTP_RAW_POST_DATA;

$response = preparePaymorrowResponse($data);

$fout = @fopen($_SERVER['DOCUMENT_ROOT'] . '/service/' . 'response_xml.log', 'a+');

$order_number = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderId;

$customer_name = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerPersonalDetails->customerGivenName . ' ' . $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerPersonalDetails->customerSurname;

@fwrite($fout, "called                 " . date('Y-m-d  H:i:s') . "\n\r");
@fwrite($fout, "for                    " . $customer_name . "\n\r");
@fwrite($fout, "order number           " . $order_number . "\n\r");

if (isset($response->responsePaymorrowOrder->paymorrowOrderRequestModified) && $response->responsePaymorrowOrder->paymorrowOrderRequestModified == 'true') {
	@fwrite($fout, "was modified\n\r");
	
	$order_data['customer_email'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerPersonalDetails->customerEmail;
	$order_data['customer_phone'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerPersonalDetails->customerPhoneNo;
	$order_data['customer_mobile'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerPersonalDetails->customerMobileNo;
	
	$order_data['billing_company'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerAddress->addressOrganizationName;
	$order_data['billing_street'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerAddress->addressStreet;
	$order_data['billing_house_number'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerAddress->addressHouseNo;
	$order_data['billing_zip_code'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerAddress->addressPostalCode;
	$order_data['billing_city'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderCustomer->customerAddress->addressLocality;
	
	$order_data['shipping_company'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderShippingAddress->addressOrganizationName;
	$order_data['shipping_street'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderShippingAddress->addressStreet;
	$order_data['shipping_house_number'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderShippingAddress->addressHouseNo;
	$order_data['shipping_zip_code'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderShippingAddress->addressPostalCode;
	$order_data['shipping_city'] = $response->responsePaymorrowOrder->paymorrowOrderRequest->order->orderShippingAddress->addressLocality;
	
	if (isset($response->responseResultCode)) {
		$order_data['paymorrow_response_result_code'] = $response->responseResultCode;
//		if ($response->responseResultCode == 'ACCEPTED') {
//			$order_data['order_status'] = 1;
//		}
	}
	
	if (isset($response->responseStatusCode)) {
		$order_data['paymorrow_response_status_code'] = $response->responseStatusCode;
	}
	
	$set_line = "";
	$first = true;
	foreach($order_data as $key => $value) {
		if ($first) {
			$first = false;
		} else {
			$set_line .= ", ";
		}
		
		$set_line .= $base_object->db->real_escape_string($key) . "='" . $base_object->db->real_escape_string($value) . "'";
	}
	
	$sql = "SELECT
				id
			FROM
				orders
			WHERE
				order_number=" . $base_object->db->real_escape_string($order_number) . "
			AND
				order_addendum!='N'
			ORDER BY
				id DESC
			LIMIT 1";
	
	$result = $base_object->db->query($sql);
	
	if ($result->num_rows > 0) {
		$order_id = $result->fetch_object()->id;
	} else {
		@fwrite($out, "can't get order id by order number from db\n\r");
	}
	
	$sql = "UPDATE orders ";
	$sql .= "SET " . $set_line . " ";
	$sql .= 'WHERE id=' . $base_object->db->real_escape_string($order_id);
	
	$base_object->db->query($sql);	
	@fwrite($fout, "sql                    " . $sql . "\n\r");
} else {
	@fwrite($fout, "was not modified\n\r");
}

@fwrite($fout, "raw                    " . $data . "\n\r");
@fwrite($fout, "------------------------- EOL ---------------------------------\n\r");

/* EOF */