<?php 

function pm_top_up_cb() {
	if(!isset($_GET['amount'])) return_method();
	if(!is_numeric($_GET['amount'])) return_method();
	$session = Session::getInstance();
	$post = new post;
	$amount = $_GET['amount'];
	if($res = db_post_unique(['type' => "pm_wallet_top_up",
		'user_id' => $session->id,
		'status' => 'pending'])) {
		$invoice = new pm_invoice;
		$inv_meta = $invoice->item_in_another_invoice("e-wallet-top-up-".$res['id']);
		if($inv_meta) {
			$invoice->set_invoice_by_number($inv_meta->invoice_number, $inv_meta);
		} else {
			$post->drop_post($res['id']);
			echo "Something went wrong, please refresh this page to continue.";
			return;
		}
	} else {
		$ref = $post->add_post([
			'type' => "pm_wallet_top_up",
			'amount' => $amount,
			'user_id' => $session->id,
			'status' => 'pending'
		]);
		$narration = "e-Wallet top up ".date(_DATE_FORMAT_, time());
		$success = "_pm_wallet_topup_success";
		$error = "_pm_wallet_topup_error";
		$invoice = new pm_invoice;
		$invoice->due_date = time();
		$invoice->add_item($amount, $narration, $ref, 1, "e-wallet-top-up-".$ref, $success, $error);
		$invoice->save();
	}
	
	?>
	<div class="row">
		<div class="col-md-12">
			<?php echo $invoice->receipt_template($invoice->items); ?>
		</div>
		<div class="col-md-12 mt-4">
			<?php 
	          if($invoice->status() !== "paid") :
	            $args = [
	              'token' => new_token(),
	              'amount' => $invoice->total(true),
	              'in_ref' => $invoice->invoice->invoice_number,
	              'user_id' => $invoice->user_id,
	              'btn_text' => "Submit and Pay ".toMoney($invoice->total(true)),
	              'btn_class' => "btn btn-success btn-sm pull-right"
	            ];
	            do_action('_pm_make_payment_button', $args);
	          endif;
            ?>
		</div>
	</div>
	<?php
}