<?php

namespace App;

use Curl;
use DB;
use Mail;
use Carbon\Carbon;

class Bancard {
    public function __construct()
    {
        $this->public_key = env('BANCARD_PUBLIC_KEY');
        $this->private_key = env('BANCARD_PRIVATE_KEY');
        $this->environment = env('BANCARD_ENVIRONMENT');
        $this->currency = 'PYG';
        $this->single_buy_url = env('BANCARD_ENVIRONMENT').'/vpos/api/0.3/single_buy';
        $this->single_buy_confirmations_url = env('BANCARD_ENVIRONMENT').'/vpos/api/0.3/single_buy/confirmations';
        $this->rollback_url = env('BANCARD_ENVIRONMENT').'/vpos/api/0.3/single_buy/rollback';
    }

    public function singleBuy() {
        $token = md5($this->private_key.$this->shop_process_id.$this->amount.$this->currency);
        $data = [
            'public_key' => $this->public_key,
            'operation' => [
                'token' => $token,
                'shop_process_id' => $this->shop_process_id,
                'currency' => $this->currency,
                'amount' => $this->amount,
                'description' => options('title'),
                'return_url' => $this->return_url,
                'cancel_url' => $this->cancel_url,
            ]
        ];
        $response = Curl::to($this->single_buy_url)
            ->withData($data)
            ->asJson()
            ->post();

        return $response;
    }

    public function singleBuyConfirm() {
        $token = md5($this->private_key.$this->shop_process_id.'get_confirmation');
        $data = [
            'public_key' => $this->public_key,
            'operation' => [
                'token' => $token,
                'shop_process_id' => $this->shop_process_id,
            ]
        ];
        $response = Curl::to($this->single_buy_confirmations_url)
            ->withData($data)
            ->asJson()
            ->post();

        return $response;
    }

    public function rollback() {
        $token = md5($this->private_key.$this->shop_process_id.'rollback'.'0.00');
        $data = [
            'public_key' => $this->public_key,
            'operation' => [
                'token' => $token,
                'shop_process_id' => $this->shop_process_id,
            ]
        ];
        $response = Curl::to($this->rollback_url)
            ->withData($data)
            ->asJson()
            ->post();

        return $response;
    }
}
