<?php 

function paystack_cb() {
	global $post_meta;
	$post = new post;
	$user = new user;
	if(!($token = verify_token($_POST[SALT]))) {
        do_action("_display_message", ["msg" => '<i class="fa fa-warning"></i> Sorry, it seems that you submitted your request through invalid token. Please try again later.']);
        return;
    }
    $user_meta = $user->get_user_meta($_POST['user_id']);
	$invoice = new pm_invoice($_POST['user_id']);
    $invoice->set_invoice_by_number($_POST['in_ref']);
    $name_on_invoice = ($invoice->invoice->name == NULL || $invoice->invoice->name == "") ? $invoice->name : $invoice->invoice->name;
    $fee_meta = apply_filters("pm_fee_meta", $invoice->invoice);
	$paystack_live_mode = get_option('paystack_live_mode');
    $amount = (int) $invoice->total(true) * 100;
	$paystack_pk = ($paystack_live_mode) ? get_option('paystack_live_pk') : get_option('paystack_test_pk');
    $gateway_charge = paystack_gateway_charge($invoice->total(true), get_option("default_currency"));
    $deposits = $invoice->total_invoice_credit($_POST['in_ref'], ["confirmed", "approved"]);

	?>
    <div class="row">
	<div class="col-md-8 col-md-offset-2">
        <div class="panel panel-primary">
            <div class="panel-body">
                <form action="process-paystack" method="POST" >
                    <input type="hidden" name="<?php echo SALT; ?>" value="<?php echo $_POST[SALT]; ?>">
                    <input type="hidden" name="amount" value="<?php echo $invoice->total(true) ?>">
                    <input type="hidden" name="in_ref" value="<?php echo $_POST['in_ref']; ?>">
                    <input type="hidden" name="user_id" value="<?php echo $_POST['user_id']; ?>">
                  <script
                    src="https://js.paystack.co/v1/inline.js" 
                    data-key="<?php echo $paystack_pk ?>"
                    data-amount="<?php echo $amount ?>"
                    data-email="<?php echo $user_meta['email'] ?>"
                    data-currency="<?php echo get_option("default_currency") ?>"
                  >
                  </script>
                </form>
            	<div class="table-responsive">
                <table class="table table-striped mt-5">
                    <tbody>
                        <tr>
                            <td>Name</td>
                            <td><?php echo $name_on_invoice ?></td>
                        </tr>
                        <tr>
                            <td>Payment receipt will be sent</td>
                            <td><?php echo $user_meta['email'] ?></td>
                        </tr>
                        <tr>
                            <td>Description</td>
                            <td><?php echo $invoice->description(); ?></td>
                        </tr>
                        <tr>
                            <td>Amount</td>
                            <td><?php echo toMoney($invoice->total()) ?></td>
                        </tr>
                        <?php if ($deposits > 0): ?>
                            <tr>
                                <td>Deposits</td>
                                <td><?php echo toMoney($deposits) ?></td>
                            </tr>
                            <tr>
                                <td>Balance</td>
                                <td><?php echo toMoney($invoice->total(true)) ?></td>
                            </tr>
                        <?php endif ?>
                        <tr>
                            <td>Gateway Charge</td>
                            <td><?php echo toMoney($gateway_charge); ?></td>
                        </tr>
                        <tr>
                            <td>Total</td>
                            <td><?php echo toMoney(($amount/100) + $gateway_charge); ?></td>
                        </tr>
                        <tr>
                            <td>Status</td>
                            <td><?php echo $invoice->status(); ?>
                                <a class="pull-right btn btn-default" href="invoice?id=<?php echo (isset($_POST['page_template'])) ? $invoice->invoice->id.'&template='.$_POST['page_template'] : $invoice->invoice->id; ?>">View Invoice</a>
                            </td>
                        </tr>
                    </tbody>
                </table>
                </div>
                <hr class="mt-5">
                <div class="change-pm">
                    <h4 class="text-center">Change Payment Method</h4>
                    <?php 
                        if($invoice->status() !== "paid") :
                          $args = [
                            'token' => $token,
                            'amount' => $invoice->total(true),
                            'in_ref' => $invoice->invoice->invoice_number,
                            'user_id' => $invoice->user_id,
                            'btn_text' => "Submit and Pay ".toMoney((int) $invoice->total(true)),
                            'btn_class' => "btn btn-success btn-sm pull-right"
                          ];
                          do_action('_pm_make_payment_button', $args);
                        endif;
                    ?>
                </div>
            </div>
        </div>
    </div>
	</div>
	<?php
}