2023-03-22 14:57:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Extensions\PaymentGateways\PayPal;
|
|
|
|
|
2023-05-18 19:51:10 +00:00
|
|
|
use App\Events\CouponUsedEvent;
|
2023-03-22 14:57:39 +00:00
|
|
|
use App\Events\PaymentEvent;
|
|
|
|
use App\Events\UserUpdateCreditsEvent;
|
|
|
|
use App\Extensions\PaymentGateways\PayPal\PayPalSettings;
|
2023-05-26 14:30:23 +00:00
|
|
|
use App\Classes\PaymentExtension;
|
2023-05-28 00:34:59 +00:00
|
|
|
use App\Enums\PaymentStatus;
|
2023-03-22 14:57:39 +00:00
|
|
|
use App\Models\PartnerDiscount;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\ShopProduct;
|
|
|
|
use App\Models\User;
|
2023-05-17 16:17:51 +00:00
|
|
|
use App\Models\Coupon;
|
2023-05-18 01:13:10 +00:00
|
|
|
use App\Traits\Coupon as CouponTrait;
|
2023-03-22 14:57:39 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use PayPalCheckoutSdk\Core\PayPalHttpClient;
|
|
|
|
use PayPalCheckoutSdk\Core\ProductionEnvironment;
|
|
|
|
use PayPalCheckoutSdk\Core\SandboxEnvironment;
|
|
|
|
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
|
|
|
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
|
|
|
use PayPalHttp\HttpException;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Summary of PayPalExtension
|
|
|
|
*/
|
2023-05-26 14:30:23 +00:00
|
|
|
class PayPalExtension extends PaymentExtension
|
2023-03-22 14:57:39 +00:00
|
|
|
{
|
2023-05-18 01:13:10 +00:00
|
|
|
use CouponTrait;
|
|
|
|
|
2023-03-22 14:57:39 +00:00
|
|
|
public static function getConfig(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
"name" => "PayPal",
|
|
|
|
"RoutesIgnoreCsrf" => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:30:23 +00:00
|
|
|
public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string
|
2023-03-22 14:57:39 +00:00
|
|
|
{
|
|
|
|
$request = new OrdersCreateRequest();
|
|
|
|
$request->prefer('return=representation');
|
|
|
|
$request->body = [
|
|
|
|
"intent" => "CAPTURE",
|
|
|
|
"purchase_units" => [
|
|
|
|
[
|
|
|
|
"reference_id" => uniqid(),
|
2023-05-26 14:30:23 +00:00
|
|
|
"description" => $shopProduct->display,
|
2023-05-18 01:13:10 +00:00
|
|
|
"amount" => [
|
2023-05-26 14:30:23 +00:00
|
|
|
"value" => $totalPriceString,
|
2023-03-22 14:57:39 +00:00
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
|
'breakdown' => [
|
2023-05-18 01:13:10 +00:00
|
|
|
'item_total' => [
|
2023-03-22 14:57:39 +00:00
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
2023-05-26 14:30:23 +00:00
|
|
|
'value' => $totalPriceString
|
2023-03-22 14:57:39 +00:00
|
|
|
],
|
2023-05-18 01:13:10 +00:00
|
|
|
'tax_total' => [
|
2023-03-22 14:57:39 +00:00
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
|
'value' => $shopProduct->getTaxValue(),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"application_context" => [
|
|
|
|
"cancel_url" => route('payment.Cancel'),
|
2023-05-26 14:30:23 +00:00
|
|
|
"return_url" => route('payment.PayPalSuccess', ['payment' => $payment->id]),
|
2023-04-29 22:31:32 +00:00
|
|
|
'brand_name' => config('app.name', 'CtrlPanel.GG'),
|
2023-03-22 14:57:39 +00:00
|
|
|
'shipping_preference' => 'NO_SHIPPING'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Call API with your client and get a response for your call
|
|
|
|
$response = self::getPayPalClient()->execute($request);
|
|
|
|
|
|
|
|
// check for any errors in the response
|
|
|
|
if ($response->statusCode != 201) {
|
|
|
|
throw new \Exception($response->statusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the link is not empty
|
|
|
|
if (empty($response->result->links[1]->href)) {
|
|
|
|
throw new \Exception('No redirect link found');
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:30:23 +00:00
|
|
|
return $response->result->links[1]->href;
|
2023-03-22 14:57:39 +00:00
|
|
|
} catch (HttpException $ex) {
|
|
|
|
Log::error('PayPal Payment: ' . $ex->getMessage());
|
|
|
|
|
2023-05-26 14:30:23 +00:00
|
|
|
throw new \Exception('PayPal Payment: ' . $ex->getMessage());
|
2023-03-22 14:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:30:23 +00:00
|
|
|
|
2023-03-22 14:57:39 +00:00
|
|
|
static function PaypalSuccess(Request $laravelRequest): void
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
$user = User::findOrFail($user->id);
|
|
|
|
|
|
|
|
$payment = Payment::findOrFail($laravelRequest->payment);
|
|
|
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
|
|
|
|
|
|
|
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
|
|
|
$request->prefer('return=representation');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Call API with your client and get a response for your call
|
|
|
|
$response = self::getPayPalClient()->execute($request);
|
|
|
|
if ($response->statusCode == 201 || $response->statusCode == 200) {
|
|
|
|
//update payment
|
|
|
|
$payment->update([
|
2023-05-28 00:34:59 +00:00
|
|
|
'status' => PaymentStatus::PAID,
|
2023-03-22 14:57:39 +00:00
|
|
|
'payment_id' => $response->result->id,
|
|
|
|
]);
|
|
|
|
|
2023-05-17 16:17:51 +00:00
|
|
|
|
2023-03-22 14:57:39 +00:00
|
|
|
event(new UserUpdateCreditsEvent($user));
|
|
|
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
|
|
|
|
|
|
|
// redirect to the payment success page with success message
|
|
|
|
Redirect::route('home')->with('success', 'Payment successful')->send();
|
|
|
|
} elseif (env('APP_ENV') == 'local') {
|
|
|
|
// If call returns body in response, you can get the deserialized version from the result attribute of the response
|
|
|
|
$payment->delete();
|
|
|
|
dd($response);
|
|
|
|
} else {
|
|
|
|
$payment->update([
|
2023-05-28 00:34:59 +00:00
|
|
|
'status' => PaymentStatus::CANCELED,
|
2023-03-22 14:57:39 +00:00
|
|
|
'payment_id' => $response->result->id,
|
|
|
|
]);
|
|
|
|
abort(500);
|
|
|
|
}
|
|
|
|
} catch (HttpException $ex) {
|
|
|
|
if (env('APP_ENV') == 'local') {
|
|
|
|
echo $ex->statusCode;
|
|
|
|
$payment->delete();
|
|
|
|
dd($ex->getMessage());
|
|
|
|
} else {
|
|
|
|
$payment->update([
|
2023-05-28 00:34:59 +00:00
|
|
|
'status' => PaymentStatus::CANCELED,
|
2023-03-22 14:57:39 +00:00
|
|
|
'payment_id' => $response->result->id,
|
|
|
|
]);
|
|
|
|
abort(422);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getPayPalClient(): PayPalHttpClient
|
|
|
|
{
|
2023-05-26 14:30:23 +00:00
|
|
|
|
|
|
|
$environment = config('app.env') == 'local'
|
2023-03-22 14:57:39 +00:00
|
|
|
? new SandboxEnvironment(self::getPaypalClientId(), self::getPaypalClientSecret())
|
|
|
|
: new ProductionEnvironment(self::getPaypalClientId(), self::getPaypalClientSecret());
|
|
|
|
return new PayPalHttpClient($environment);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static function getPaypalClientId(): string
|
|
|
|
{
|
|
|
|
$settings = new PayPalSettings();
|
2023-05-26 14:30:23 +00:00
|
|
|
return config('app.env') == 'local' ? $settings->sandbox_client_id : $settings->client_id;
|
2023-03-22 14:57:39 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static function getPaypalClientSecret(): string
|
|
|
|
{
|
|
|
|
$settings = new PayPalSettings();
|
2023-05-26 14:30:23 +00:00
|
|
|
return config('app.env') == 'local' ? $settings->sandbox_client_secret : $settings->client_secret;
|
2023-03-22 14:57:39 +00:00
|
|
|
}
|
|
|
|
}
|