308 lines
8.1 KiB
PHP
308 lines
8.1 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: root
|
|
* Date: 10/16/18
|
|
* Time: 8:45 AM
|
|
*/
|
|
|
|
namespace App\Libraries;
|
|
|
|
use Carbon\Carbon;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
use GuzzleHttp\Psr7\Uri;
|
|
use GuzzleHttp\Psr7;
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
class Paypal
|
|
{
|
|
protected $url;
|
|
|
|
protected $grant_type;
|
|
protected $client_id;
|
|
protected $secret;
|
|
protected $access_token;
|
|
|
|
protected $return_url;
|
|
protected $cancel_url;
|
|
protected $total;
|
|
protected $paypal_fee;
|
|
protected $custom;
|
|
protected $invoice_number;
|
|
protected $trans_num;
|
|
|
|
protected $paypal_id;
|
|
protected $self_url;
|
|
protected $approval_url;
|
|
protected $execute_url;
|
|
protected $response;
|
|
protected $payer_id;
|
|
protected $error_message;
|
|
|
|
protected $payer_firstname;
|
|
protected $payer_lastname;
|
|
protected $payer_email;
|
|
protected $receiver_email;
|
|
protected $paid_at;
|
|
|
|
public function __construct()
|
|
{
|
|
if(env("PAYPAL_ENV", '') == "SANDBOX")
|
|
{
|
|
$this->url = new Uri(env("PAYPAL_SANDBOX_URL", ''));
|
|
$this->client_id = env("PAYPAL_SANDBOX_CLIENTID", '');
|
|
$this->secret = env("PAYPAL_SANDBOX_SECRET", '');
|
|
}
|
|
else
|
|
{
|
|
$this->url = new Uri(env("PAYPAL_LIVE_URL", ''));
|
|
$this->client_id = env("PAYPAL_LIVE_CLIENTID", '');
|
|
$this->secret = env("PAYPAL_LIVE_SECRET", '');
|
|
}
|
|
|
|
$this->return_url = env("FRONT_END_URL", '').'/topup-success-page';
|
|
$this->cancel_url = env("FRONT_END_URL", '').'/topup-error-page';
|
|
|
|
}
|
|
|
|
public function set_amount($var){ $this->amount = $var; }
|
|
|
|
public function set_invoice_number($var){ $this->invoice_number = $var; }
|
|
public function get_invoice_number(){ return $this->invoice_number; }
|
|
|
|
public function set_trans_num($var){ $this->trans_num = $var; }
|
|
public function get_trans_num(){ return $this->trans_num; }
|
|
|
|
public function set_paypal_id($var){ $this->paypal_id = $var; }
|
|
public function get_paypal_id(){ return $this->paypal_id; }
|
|
|
|
public function set_self_url($var){ $this->self_url = $var; }
|
|
public function get_self_url(){ return $this->self_url; }
|
|
|
|
public function set_approval_url($var){ $this->approval_url = $var; }
|
|
public function get_approval_url(){ return $this->approval_url; }
|
|
|
|
public function set_execute_url($var){ $this->execute_url = $var; }
|
|
public function get_execute_url(){ return $this->execute_url; }
|
|
|
|
public function set_payer_id($var){ $this->payer_id = $var; }
|
|
public function get_payer_id(){ return $this->payer_id; }
|
|
|
|
public function set_response($var){ $this->response = $var; }
|
|
public function get_response(){ return $this->response; }
|
|
|
|
public function set_error_message($var){ $this->error_message = $var; }
|
|
public function get_error_message(){ return $this->error_message; }
|
|
|
|
public function set_payer_firstname($var){ $this->payer_firstname = $var; }
|
|
public function get_payer_firstname(){ return $this->payer_firstname; }
|
|
|
|
public function set_payer_lastname($var){ $this->payer_lastname = $var; }
|
|
public function get_payer_lastname(){ return $this->payer_lastname; }
|
|
|
|
public function set_payer_email($var){ $this->payer_email = $var; }
|
|
public function get_payer_email(){ return $this->payer_email; }
|
|
|
|
public function set_receiver_email($var){ $this->receiver_email = $var; }
|
|
public function get_receiver_email(){ return $this->receiver_email; }
|
|
|
|
public function set_paid_at($var){ $this->paid_at = $var; }
|
|
public function get_paid_at(){ return $this->paid_at; }
|
|
|
|
public function set_total($var){ $this->total = $var; }
|
|
public function get_total(){ return $this->total; }
|
|
|
|
public function set_paypal_fee($var){ $this->paypal_fee = $var; }
|
|
public function get_paypal_fee(){ return $this->paypal_fee; }
|
|
|
|
|
|
|
|
|
|
private function auth()
|
|
{
|
|
$client = new Client();
|
|
$response = $client->request('POST', $this->url.'/oauth2/token', [
|
|
'auth' =>[
|
|
$this->client_id,
|
|
$this->secret
|
|
],
|
|
'headers' => [
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
],
|
|
'form_params' => [
|
|
'grant_type' => 'client_credentials'
|
|
]
|
|
]);
|
|
|
|
$response = $response->getBody()->getContents();
|
|
|
|
if($response !== null)
|
|
{
|
|
$decoded = json_decode($response);
|
|
|
|
if(isset($decoded->access_token))
|
|
{
|
|
$this->access_token = $decoded->access_token;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function exec_client($path, $body, $method = 'POST')
|
|
{
|
|
$client = new Client();
|
|
try
|
|
{
|
|
|
|
$response = $client->request($method, $this->url.$path, [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer '.$this->access_token,
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
'json' => $body
|
|
]);
|
|
|
|
return $response->getBody()->getContents();
|
|
}
|
|
catch (RequestException $e)
|
|
{
|
|
if ($e->hasResponse())
|
|
{
|
|
return $e->getResponse()->getBody()->getContents();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function generate_paypal_url()
|
|
{
|
|
if($this->auth())
|
|
{
|
|
$body = [
|
|
'intent' => 'sale',
|
|
'redirect_urls' => [
|
|
'return_url' => $this->return_url,
|
|
'cancel_url' => $this->return_url,
|
|
],
|
|
'payer' =>[
|
|
'payment_method' => 'paypal'
|
|
],
|
|
'transactions' => [
|
|
0 => [
|
|
'amount' => [
|
|
'total' => $this->amount,
|
|
'currency' => 'PHP',
|
|
],
|
|
'description' => 'Unioil Top Up Points',
|
|
'custom' => $this->get_invoice_number(),
|
|
'invoice_number' => $this->get_invoice_number(),
|
|
'payment_options' => [
|
|
'allowed_payment_method' => 'INSTANT_FUNDING_SOURCE'
|
|
],
|
|
'item_list' => [
|
|
'items' => [
|
|
0 => [
|
|
'name' => 'Unioil Top Up Points',
|
|
'description' => 'Unioil Prepaid Load',
|
|
'quantity' => 1,
|
|
'price' => $this->amount,
|
|
'tax' => 0,
|
|
'sku' => 'PREPAID',
|
|
'currency' => 'PHP'
|
|
]
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'note_to_payer' => 'Contact us for any questions on your order'
|
|
];
|
|
|
|
$response = $this->exec_client('/payments/payment', $body);
|
|
$response = json_decode($response);
|
|
|
|
if(isset($response->id))
|
|
{
|
|
$this->set_paypal_id($response->id);
|
|
$this->set_self_url($response->links[0]->href);
|
|
$this->set_approval_url($response->links[1]->href);
|
|
$this->set_execute_url($response->links[2]->href);
|
|
$this->set_response($response);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function paypal_verify()
|
|
{
|
|
if($this->auth())
|
|
{
|
|
$response = $this->exec_client('/payments/payment/'.$this->get_paypal_id(),[],'GET');
|
|
$response = json_decode($response);
|
|
|
|
if(isset($response->payer->status) == "VERIFIED")
|
|
{
|
|
$this->set_payer_id($response->payer->payer_info->payer_id);
|
|
return true;
|
|
|
|
}
|
|
elseif (isset($response->payer->status) == "UNVERIFIED")
|
|
{
|
|
$this->set_error_message('Paypal user unverified');
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
$this->set_error_message('Paypal transaction incomplete');
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function paypal_execute()
|
|
{
|
|
if($this->auth())
|
|
{
|
|
$body = ['payer_id' => $this->get_payer_id()];
|
|
|
|
$response = $this->exec_client('/payments/payment/'.$this->get_paypal_id().'/execute',$body);
|
|
$response = json_decode($response);
|
|
|
|
if(isset($response->payer->status) && $response->payer->status == "VERIFIED")
|
|
{
|
|
$this->set_invoice_number($response->transactions[0]->invoice_number);
|
|
$this->set_trans_num($response->transactions[0]->related_resources[0]->sale->id);
|
|
$this->set_payer_firstname($response->payer->payer_info->first_name);
|
|
$this->set_payer_lastname($response->payer->payer_info->last_name);
|
|
$this->set_payer_email($response->payer->payer_info->email);
|
|
$this->set_receiver_email($response->transactions[0]->payee->email);
|
|
$this->set_paid_at($response->create_time);
|
|
$this->set_total($response->transactions[0]->amount->total);
|
|
$this->set_paypal_fee($response->transactions[0]->related_resources[0]->sale->transaction_fee->value);
|
|
$this->set_response($response);
|
|
return true;
|
|
}
|
|
elseif(isset($response->debug_id))
|
|
{
|
|
$this->set_error_message($response->message);
|
|
$this->set_response($response);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
$this->set_response($response);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|