<?php
/*
Template Name: Storeup Success
*/
get_header(); ?>
<div id="primary" class="pagemid">
	<div class="inner">
		<main class="content-area">
			<div class="entry-content-wrapper clearfix">
			<?php

			if ( have_posts() ) :
				while ( have_posts() ) :
					the_post();
					the_content();
				endwhile;
			endif;

			$storeup_payment_type = isset( $_GET['type'] ) ? $_GET['type'] : '';

			if (
				( ! empty( $_GET['tx'] ) && '' !== $_GET['tx'] ) ||
				( ! empty( $_POST['transaction_id'] ) && '' !== $_POST['transaction_id'] )
			) {

				if ( 'paypal' === $storeup_payment_type ) {
					// Get payment information from PayPal

					$item_number    = $_GET['item_number'];
					$item_name      = $_GET['item_name'];
					$txn_id         = $_GET['tx'];
					$payment_gross  = $_GET['amt'];
					$currency_code  = $_GET['cc'];
					$payment_status = $_GET['st'];
					$bk_id          = isset( $_GET['booking_id'] ) ? $_GET['booking_id'] : $_GET['cm'];
					if ( 'Completed' === $payment_status ) {
						$order_status = 'confirmed';
					} else {
						$order_status = 'unconfirmed';
					}
				}

				if ( 'voguepay' === $storeup_payment_type ) {
					// Get payment information from VoguePay
					$transaction_id = $_POST['transaction_id'];
					$args           = array( 'timeout' => 05 );
					$storeup_voguepay_merchant_id = get_option( 'storeup_voguepay_merchant_id' );
					if ( empty( $storeup_voguepay_merchant_id ) ) {
						$json = wp_remote_get( 'https://voguepay.com/?v_transaction_id=' . $transaction_id . '&type=json&demo=true', $args );
					} else {
						$json = wp_remote_get( 'https://voguepay.com/?v_transaction_id=' . $transaction_id . '&type=json', $args );
					}
					$transaction    = json_decode( $json['body'], true );
					$item_number    = $transaction['merchant_ref'];
					$item_name      = $transaction['memo'];
					$txn_id         = $transaction['transaction_id'];
					$payment_gross  = $transaction['total'];
					$currency_code  = $transaction['cur'];
					$payment_status = $transaction['status'];
					$bk_id          = $transaction['merchant_ref'];
					//$payer_email    = $transaction['email'];
					if ( 'Approved' === $payment_status ) {
						$order_status   = 'confirmed';
						$payment_status = 'Completed';
					} else {
						$order_status = 'unconfirmed';
					}
				}
			} elseif ( 'cash' === $storeup_payment_type ) {
				$storeup_cart_form = ! empty( $_POST ) ? $_POST : false;
				if ( $storeup_cart_form ) {

					// Generate Cash on site Transaction ID
					$storeup_bk_cos_date   = new DateTime();
					$storeup_bk_cos_tstamp = $storeup_bk_cos_date->getTimestamp();
					$storeup_bk_cos_pfx    = 'COS-';
					$txn_id                = $storeup_bk_cos_pfx . $storeup_bk_cos_tstamp;

					// Get booking order id from cart page.
					$bk_id          = isset( $storeup_cart_form['storeup_bk_order_id'] )
							? $storeup_cart_form['storeup_bk_order_id']
							: '';
					$order_status   = 'unconfirmed';
					$payment_status = 'Pending';
					$item_number    = isset( $storeup_cart_form['storeup_bk_item_id'] )
							? $storeup_cart_form['storeup_bk_item_id']
							: '';
					$item_name      = isset( $storeup_cart_form['storeup_bk_item_name'] )
							? $storeup_cart_form['storeup_bk_item_name']
							: '';
					$currency_code  = '';
					$payment_gross  = isset( $storeup_cart_form['storeup_bk_item_price'] )
							? $storeup_cart_form['storeup_bk_item_price']
							: '';

				}
			}

				$args = array(
					'post_type'  => 'booking',
					'meta_query' => array(
						array(
							'key'     => 'storeup_bk_txnid',
							'value'   => $txn_id,
							'compare' => '=',
						),
					),
				);

				$storeup_bk_user_ip = sanitize_text_field( $_SERVER['REMOVE_ADDR'] );

				update_post_meta( $bk_id, 'storeup_bk_ip', $storeup_bk_user_ip, true );
				update_post_meta( $bk_id, 'storeup_bk_txnid', $txn_id, true );
				update_post_meta( $bk_id, 'storeup_bk_order_status', $payment_status, true );
				update_post_meta( $bk_id, 'storeup_bk_status', $order_status, true );

				$storeup_bk_name     = get_post_meta( $bk_id, 'storeup_bk_name', true );
				$storeup_bk_phoneno  = get_post_meta( $bk_id, 'storeup_bk_phoneno', true );
				$storeup_bk_email    = get_post_meta( $bk_id, 'storeup_bk_email', true );
				$storeup_bk_address  = get_post_meta( $bk_id, 'storeup_bk_address', true );
				$storeup_bk_zipcode  = get_post_meta( $bk_id, 'storeup_bk_zipcode', true );
				$storeup_bk_location = get_post_meta( $bk_id, 'storeup_bk_location', true );
				$storeup_bk_price    = get_post_meta( $bk_id, 'storeup_bk_price', true );
				$storeup_bk_date     = get_post_meta( $bk_id, 'storeup_bk_date', true );
				$storeup_bk_message  = get_post_meta( $bk_id, 'storeup_bk_message', true );

				//Storeup Email placeholders Keys
				$storeup_email_ph_key = array(
					'[first_name]',
					'[phone_no]',
					'[email]',
					'[address]',
					'[zipcode]',
					'[location]',
					'[price]',
					'[bk_date]',
					'[message]',
					'[bk_id]',
					'[order_status]',
					'[txn_id]',
					'[payment_type]',
					'[payment_status]',
				);

				//Storeup Email placeholders Values
				$storeup_email_ph_values = array(
					$storeup_bk_name,
					$storeup_bk_phoneno,
					$storeup_bk_email,
					$storeup_bk_address,
					$storeup_bk_zipcode,
					$storeup_bk_location,
					$storeup_bk_price,
					date( 'm/d/Y', $storeup_bk_date ),
					$storeup_bk_message,
					$bk_id,
					$order_status,
					$txn_id,
					$storeup_payment_type,
					$payment_status,
				);

				/* Setting Email */
				$storeup_admin_email  = get_option( 'storeup_bk_admin_emailid' )
					? get_option( 'storeup_bk_admin_emailid' )
					: get_option( 'admin_email' );
				$storeup_client_email = $storeup_bk_email;

				/* Setting Notification Subject */
				$storeup_admin_notify_sub  = get_option( 'storeup_bk_admin_notify_subject' );
				$storeup_client_notify_sub = get_option( 'storeup_bk_client_notify_subject' );

				/* Setting Notification Message */
				$storeup_admin_notify_msg     = get_option( 'storeup_bk_admin_notification_msg' );
				$storeup_client_notify_msg    = get_option( 'storeup_bk_client_notify_msg' );
				$storeup_admin_notify_em_msg  = str_replace( $storeup_email_ph_key, $storeup_email_ph_values, $storeup_admin_notify_msg ); // Replace the placeholders
				$storeup_client_notify_em_msg = str_replace( $storeup_email_ph_key, $storeup_email_ph_values, $storeup_client_notify_msg ); // Replace the placeholders

				/* Setting Headers*/
				$storeup_admin_headers_msg  = get_option( 'storeup_bk_headers_msg' ) ? get_option( 'storeup_bk_headers_msg' ) : get_option( 'blogname' );
				$storeup_client_headers_msg = get_option( 'storeup_bk_client_headers_msg' ) ? get_option( 'storeup_bk_client_headers_msg' ) : get_option( 'blogname' );

				$storeup_admin_headers  = esc_html__( 'From: ', 'storeup' ) . $storeup_client_headers_msg . ' <' . $storeup_client_email . '>' . "\r\n\\";
				$storeup_client_headers = esc_html__( 'From: ', 'storeup' ) . $storeup_admin_headers_msg . ' <' . $storeup_admin_email . '>' . "\r\n\\";


				/**
				 * Sends mail to admin email
				 */
				storeup_email_template_group( $storeup_admin_email, $storeup_admin_notify_sub, $storeup_admin_notify_em_msg, $storeup_admin_headers );

				/**
				* Sends mail to Client email
				*/
				storeup_email_template_group( $storeup_client_email, $storeup_client_notify_sub, $storeup_client_notify_em_msg, $storeup_client_headers );

				// fetches thank you message from theme options
				$storeup_bk_msg = get_option( 'storeup_bk_thankyou_msg' )
						? get_option( 'storeup_bk_thankyou_msg' )
						: esc_html__( 'Thank you for booking appointment', 'storeup' );

				$storeup_bk_placeholders   = array( '[first_name]' );
				$storeup_bk_values         = array( $storeup_bk_name );
				$storeup_bk_thanku_message = str_replace( $storeup_bk_placeholders, $storeup_bk_values, $storeup_bk_msg ); // Replace the placeholders

				$success = '<div id="storeup-bk-msg" class="iva_message_box success iva-box-solid iva-box-normal clearfix"><div class="iva_message_box_content">' . $storeup_bk_thanku_message . '</div></div>';

				$success .= '<div class="at-checkout-wrapper success">';
				$success .= '<div class="at-checkout-group">';
				$success .= '<span class="checkout-label">' . esc_html__( 'Transaction ID:', 'storeup' ) . '</span>';
				$success .= '<span class="checkout-value">' . $txn_id . '</span>';
				$success .= '</div>'; // at-checkout-group
				if ( '' !== $item_number ) {
					$success .= '<div class="at-checkout-group">';
					$success .= '<span class="checkout-label">' . esc_html__( 'Item Number:', 'storeup' ) . '</span>';
					$success .= '<span class="checkout-value">' . $item_number . '</span>';
					$success .= '</div>';// at-checkout-group
				}
				$success .= '<div class="at-checkout-group">';
				$success .= '<span class="checkout-label">' . esc_html__( 'Order ID:', 'storeup' ) . '</span>';
				$success .= '<span class="checkout-value">' . $bk_id . '</span>';
				$success .= '</div>'; // at-checkout-group
				if ( '' !== $item_name ) {
					$success .= '<div class="at-checkout-group">';
					$success .= '<span class="checkout-label">' . esc_html__( 'Storage Name:', 'storeup' ) . '</span>';
					$success .= '<span class="checkout-value">' . $item_name . '</span>';
					$success .= '</div>'; // at-checkout-group
				}
				if ( '' !== $currency_code || '' !== $payment_gross ) {
					$success .= '<div class="at-checkout-group">';
					$success .= '<span class="checkout-label">' . esc_html__( 'Amount:', 'storeup' ) . '</span>';
					$success .= '<span class="checkout-value">' . $currency_code . $payment_gross . '</span>';
					$success .= '</div>'; // at-checkout-group
				}
				$success .= '<div class="at-checkout-group">';
				$success .= '<span class="checkout-label">' . esc_html__( 'Payment Status:', 'storeup' ) . '</span>';
				$success .= '<span class="checkout-value">' . $payment_status . '</span>';
				$success .= '</div>'; // at-checkout-group
				$success .= '</div>'; // at-checkout-wrapper

				if ( have_posts() ) :
					while ( have_posts() ) :
						the_post();
						if ( isset( $success ) ) {
							$allowed_tags = wp_kses_allowed_html( 'post' );
							echo wp_kses( $success, $allowed_tags );
						}
					endwhile;
				endif;

				comments_template( '', true );
				?>

			</div><!-- .entry-content-wrapper -->
		</main><!-- .content-area -->

		<?php
		if ( storeup_generator( 'storeup_sidebar_option', $post->ID ) !== 'fullwidth' ) {
			get_sidebar(); }
		?>

		<div class="clear"></div>
	</div><!-- .inner -->
</div><!-- .pagemid -->
<?php
get_footer();
