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

define('PAYPAL_API_CLIENT_ID', 'AeqOW6k5aM5SHGY1weeCumbabXbCs3njKFM247D4tdr96hr_yornYU4M5QZ78PYWVKGv9AbZr4srb6FK');
define('PAYPAL_API_SECRET', 'EFhDwbzCzviy1hQZ_0MvdYKjAbAFbtoGX98HvNu3d-U2jfQ0LOnw0n3l9n29jYOao4kOVJRJq_MBODQl');
define('PAYPAL_SANDBOX', true);

class PaypalExpress
{
    public $paypalEnv       = PAYPAL_SANDBOX ? 'sandbox' : 'production';
    public $paypalURL       = PAYPAL_SANDBOX ? 'https://api.sandbox.paypal.com/v1/' : 'https://api.paypal.com/v1/';
    public $paypalClientID  = PAYPAL_API_CLIENT_ID;
    private $paypalSecret   = PAYPAL_API_SECRET;

    public function validate($paymentID, $paymentToken, $payerID, $productID)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->paypalURL . 'oauth2/token');
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD, $this->paypalClientID . ":" . $this->paypalSecret);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
        $response = curl_exec($ch);
        curl_close($ch);

        if (empty($response)) {
            return false;
        } else {
            $jsonData = json_decode($response);
            $curl = curl_init($this->paypalURL . 'payments/payment/' . $paymentID);
            curl_setopt($curl, CURLOPT_POST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer ' . $jsonData->access_token,
                'Accept: application/json',
                'Content-Type: application/xml'
            ));
            $response = curl_exec($curl);
            curl_close($curl);

            // Transaction data 
            $result = json_decode($response);

            return $result;
        }
    }
}
