<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Webhooks extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->library('shop_service', array(), 'shop');
	}


	public function index(){
		$raw_data = file_get_contents('php://input');

		$this->load->model('Pay_model','pay');
		$info = $this->pay->get();
	
		$pub_key_contents = $info['publickey'];

		$signature = $this->input->get_request_header('x-pingplusplus-signature');
/*
		if(!openssl_verify($raw_data, base64_decode($signature), $pub_key_contents, 'sha256') == 1){
			echo 'error';
			exit;
		}
		*/
		$charge = json_decode($raw_data);

		if($charge->type != 'charge.succeeded'){
			echo 'not success';
			exit;
		}

		$ordernumber = $charge->data->object->order_no;

		$this->load->model('cardorder_model', 'order');

		$orderifno = $this->order->getByOrdernumber($ordernumber);

		if(empty($orderifno)){
			echo 'no order';
			exit;
		}

		$this->load->library('user_service', array(), 'user');
		$this->user->buy($orderifno['duration'], $ordernumber, $orderifno['uid'], $orderifno['type']);

	}


}
