mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-10-22 09:03:25 -04:00
208 lines
7.1 KiB
PHP
208 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\WTIALaCarte\Services;
|
|
use Illuminate\Support\Str;
|
|
use Carbon\Carbon;
|
|
|
|
class PaypalService
|
|
{
|
|
protected $apiBaseUrl;
|
|
protected $accessToken;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->apiBaseUrl = 'https://api-m.paypal.com'; // live
|
|
//$this->apiBaseUrl = 'https://api-m.sandbox.paypal.com'; // sandbox
|
|
}
|
|
|
|
private function makeAccessToken() {
|
|
|
|
$credential = getPaymentInfoViaSellerId(1, 3);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/oauth2/token');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
|
|
curl_setopt($ch, CURLOPT_USERPWD, $credential->perameter_2 . ':' . $credential->perameter_3);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($result);
|
|
if (!empty($result->access_token)) {
|
|
$this->accessToken = $result->access_token;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public function order($items,$coupon_data)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$purchase_units = [];
|
|
foreach ($items as $key => $item) {
|
|
|
|
if($coupon_data != null && in_array($item['id'], $coupon_data->alacarte_id)){
|
|
|
|
$discount = 0;
|
|
if($coupon_data->type == 'Amount'){
|
|
$discount = $coupon_data->discount;
|
|
} else {
|
|
$discount = ($coupon_data->discount / 100 * $item['final_price']);
|
|
}
|
|
|
|
array_push($purchase_units,[
|
|
'reference_id' => $item['id'],
|
|
'custom_id' => json_encode([
|
|
'user_id' => auth()->user()->id,
|
|
'alacarte_id' => $item['id'],
|
|
'quantity' => $item['qty'],
|
|
'discount' => $discount,
|
|
'coupon_id' => $coupon_data->id
|
|
]),
|
|
'items' => [
|
|
[
|
|
'name' => $item['name'],
|
|
'quantity' => $item['qty'],
|
|
'unit_amount' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $item['final_price'],
|
|
]
|
|
]
|
|
|
|
],
|
|
'amount' => [
|
|
'currency_code' => 'USD',
|
|
'value' => ($item['final_price'] * $item['qty']) - ($discount * $item['qty']),
|
|
'breakdown' => [
|
|
'item_total' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $item['final_price'] * $item['qty']
|
|
],
|
|
'discount' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $discount * $item['qty']
|
|
]
|
|
]
|
|
]
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
array_push($purchase_units,[
|
|
'reference_id' => $item['id'],
|
|
'custom_id' => json_encode([
|
|
'user_id' => auth()->user()->id,
|
|
'alacarte_id' => $item['id'],
|
|
'quantity' => $item['qty'],
|
|
'discount' => 0,
|
|
]),
|
|
'items' => [
|
|
[
|
|
'name' => $item['name'],
|
|
'quantity' => $item['qty'],
|
|
'unit_amount' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $item['final_price'],
|
|
]
|
|
]
|
|
|
|
],
|
|
'amount' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $item['final_price'] * $item['qty'],
|
|
'breakdown' => [
|
|
'item_total' => [
|
|
'currency_code' => 'USD',
|
|
'value' => $item['final_price'] * $item['qty']
|
|
]
|
|
]
|
|
]
|
|
|
|
]);
|
|
}
|
|
}
|
|
|
|
$post_data = [
|
|
'intent' => 'CAPTURE',
|
|
'purchase_units' => $purchase_units,
|
|
'payment_source' => [
|
|
'paypal' => [
|
|
'email' => auth()->user()->email,
|
|
'experience_context' => [
|
|
'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED',
|
|
'brand_name' => 'WTI',
|
|
'locale' => 'en-US',
|
|
'landing_page' => 'LOGIN',
|
|
'shipping_preference' => 'NO_SHIPPING',
|
|
'user_action' => 'PAY_NOW',
|
|
'return_url' => url('/a-la-carte'),
|
|
'cancel_url' => url('/a-la-carte')
|
|
],
|
|
]
|
|
]
|
|
];
|
|
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v2/checkout/orders');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data,true));
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function capturePayment($order_id)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v2/checkout/orders/'.$order_id.'/capture');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
//curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['reason' => 'Approve Subscription']));
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
}
|