<?php
/**
 * Template Name: PayPal Return Page
 *
 * @package WordPress
 * @subpackage Charity
 * @since Charity 1.0
 */
?>
<?php
	$wplook_page_header = (isset($wplook_page_meta['wpl_page_header'][0]) && $wplook_page_meta['wpl_page_header'][0] != '') ? $wplook_page_meta['wpl_page_header'][0] : false;
?>
<?php get_header(); ?>

	<div class="main">
		<div class="main-head">
			<div class="main-head-image fullscreener">
				<?php
					if ( $wplook_page_header ) {
						echo '<img class="hide" src="'. esc_url( $wplook_page_header ).'">';
					}?>
			</div><!-- /.main-head-image fullscreener -->

			<div class="main-head-content">
				<div class="grid-container">
					<div class="grid-x grid-margin-x">
						<div class="cell small-12">
							<h1 class="main-title main-title-alt"><?php the_title(); ?></h1><!-- /.main-title main-title-alt -->
							<?php if (ot_get_option('wpl_breadcrumbs_status') == 'on') : ?>
								<ul class="breadcrumbs">
									<?php wplook_breadcrumbs(); ?>
								</ul><!-- /.breadcrumbs -->
							<?php endif; ?>
						</div><!-- /.cell small-12 -->
					</div><!-- /.grid-x grid-margin-x -->
				</div><!-- /.grid-container -->
			</div><!-- /.main-head-content -->
		</div><!-- /.main-head -->
		<div class="main-body">
			<div class="grid-container">
				<div class="grid-x grid-margin-x">
					<div class="cell small-12">
						<div class="content">
							<article id="post-<?php the_ID(); ?>" <?php post_class('post post-single post-event'); ?>>
								<div class="post-body">
									<div class="entry" itemprop="articleBody">
										<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
											<?php the_content(); ?>
										<?php endwhile; // end of the loop. ?>

										<?php
											//Use this form for production server
											$r = new PayPal(true);
											//Use this form for sandbox tests
											//$r = new PayPal();
											$final = $r->doPayment();
											if ($final['ACK'] == 'Success') {
												//echo 'Succeed!';
												$oToken   = $r->getCheckoutDetails($final['TOKEN']);
												$txnID    = $oToken['TOKEN'];
												$firstName   = $oToken['FIRSTNAME'];
												$lastName   = $oToken['LASTNAME'];
												$addressCountry = $oToken['COUNTRYCODE'];
												$payerEmail  = $oToken['EMAIL'];
												$bani = explode("|", $oToken['CUSTOM']);
												$payment_gross = $bani[0];
												$valuta= $bani[1];
												$donCause= $bani[2];
												$my_post = array(
													'post_title'    => $txnID,
													'post_status'   => 'publish',
													'post_author'   => 1,
													'comment_status' => 'closed',
													'ping_status' => 'closed',
													'post_type'      => 'post_pledges'
												);
												$post_id = wp_insert_post( $my_post );
												add_post_meta($post_id, "wpl_pledge_cause", $donCause);
												add_post_meta($post_id, "wpl_pledge_transaction_id", $txnID);
												add_post_meta($post_id, "wpl_pledge_first_name", $firstName);
												add_post_meta($post_id, "wpl_pledge_last_name", $lastName);
												add_post_meta($post_id, "wpl_pledge_country", $addressCountry);
												add_post_meta($post_id, "wpl_pledge_email", $payerEmail);
												add_post_meta($post_id, "wpl_pledge_donation_amount", $payment_gross);
												add_post_meta($post_id, "wpl_pledge_payment_source", 'paypal');
												add_post_meta($post_id, "wpl_pledge_payment_Status", 'Completed');

												$wplook_email_var = array();
												$wplook_email_var['wpl_first_name'] = $firstName;
												$wplook_email_var['wpl_last_name'] = $lastName;
												$wplook_email_var['wpl_total'] = $payment_gross;
												$wplook_email_var['wpl_curency_code'] = ot_get_option('wpl_curency_code');
												$wplook_email_var['wpl_url_cause'] = get_permalink($donCause);
												$wplook_email_var['wpl_title_cause'] = get_the_title($donCause);
												$wplook_email_var['wpl_customer'] = $payerEmail;
												$wplook_email_var['wpl_charge_id'] = $txnID;
												$wplook_email_var['wpl_blog_name'] = get_bloginfo('name');

												$wpl_user_subject = ot_get_option('wpl_email_title_to_user');
												$wpl_user_template = ot_get_option('wpl_email_message_to_user');

												$wpl_admin_subject = ot_get_option('wpl_email_title_to_admin');
												$wpl_admin_template = ot_get_option('wpl_email_message_to_admin');

												foreach($wplook_email_var as $key => $value) {
													$wpl_user_template = str_replace('{{ '.$key.' }}', $value, $wpl_user_template);
													$wpl_admin_template = str_replace('{{ '.$key.' }}', $value, $wpl_admin_template);
												}


												//send mail to user
												$to = $payerEmail;
												$headers = array('Content-Type: text/html; charset=UTF-8');
												$headers[] = 'From:'. get_bloginfo('admin_email');
												wp_mail( $to, $wpl_user_subject, $wpl_user_template, $headers );


												//send mail to admin
												$to_admin = get_bloginfo('admin_email');
												$headers_admin = array('Content-Type: text/html; charset=UTF-8');
												$headers_admin[] = 'From:'. get_bloginfo('admin_email');

												wp_mail( $to_admin, $subject_admin, $wpl_admin_template, $headers_admin );


											} else {
												//print_r($final);
											}
										?>
									</div>
								</div>
							</article>
						</div><!-- /.content -->
					</div><!-- /.cell large-12 -->
				</div><!-- /.grid-x grid-margin-x -->
			</div><!-- /.grid-container -->
		</div><!-- /.main-body -->
	</div><!-- /.main -->
<?php get_footer(); ?>
