2023-01-14 20:51:20 +00:00
|
|
|
<?php
|
|
|
|
|
2023-01-14 21:38:13 +00:00
|
|
|
use App\Events\PaymentEvent;
|
2023-01-14 20:51:20 +00:00
|
|
|
use App\Events\UserUpdateCreditsEvent;
|
|
|
|
use App\Models\PartnerDiscount;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\ShopProduct;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-01-14 22:34:54 +00:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
2023-01-14 20:51:20 +00:00
|
|
|
use PayPalCheckoutSdk\Core\PayPalHttpClient;
|
|
|
|
use PayPalCheckoutSdk\Core\ProductionEnvironment;
|
|
|
|
use PayPalCheckoutSdk\Core\SandboxEnvironment;
|
|
|
|
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
|
|
|
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
|
|
|
use PayPalHttp\HttpException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param ShopProduct $shopProduct
|
|
|
|
*/
|
|
|
|
function PaypalPay(Request $request)
|
|
|
|
{
|
2023-01-14 22:34:54 +00:00
|
|
|
/** @var User $user */
|
|
|
|
$user = Auth::user();
|
2023-01-14 20:51:20 +00:00
|
|
|
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
2023-01-20 22:42:53 +00:00
|
|
|
$discount = PartnerDiscount::getDiscount();
|
2023-01-14 22:34:54 +00:00
|
|
|
|
2023-01-14 23:43:13 +00:00
|
|
|
// create a new payment
|
|
|
|
$payment = Payment::create([
|
2023-01-14 22:34:54 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'payment_id' => null,
|
|
|
|
'payment_method' => 'paypal',
|
|
|
|
'type' => $shopProduct->type,
|
|
|
|
'status' => 'open',
|
|
|
|
'amount' => $shopProduct->quantity,
|
2023-01-20 22:42:53 +00:00
|
|
|
'price' => $shopProduct->price - ($shopProduct->price * $discount / 100),
|
2023-01-14 22:34:54 +00:00
|
|
|
'tax_value' => $shopProduct->getTaxValue(),
|
|
|
|
'tax_percent' => $shopProduct->getTaxPercent(),
|
|
|
|
'total_price' => $shopProduct->getTotalPrice(),
|
|
|
|
'currency_code' => $shopProduct->currency_code,
|
|
|
|
'shop_item_product_id' => $shopProduct->id,
|
|
|
|
]);
|
|
|
|
|
2023-01-14 20:51:20 +00:00
|
|
|
$request = new OrdersCreateRequest();
|
|
|
|
$request->prefer('return=representation');
|
|
|
|
$request->body = [
|
2023-01-14 23:43:13 +00:00
|
|
|
"intent" => "CAPTURE",
|
|
|
|
"purchase_units" => [
|
|
|
|
[
|
|
|
|
"reference_id" => uniqid(),
|
2023-01-20 22:42:53 +00:00
|
|
|
"description" => $shopProduct->display . ($discount ? (" (" . __('Discount') . " " . $discount . '%)') : ""),
|
2023-01-14 23:43:13 +00:00
|
|
|
"amount" => [
|
|
|
|
"value" => $shopProduct->getTotalPrice(),
|
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
|
'breakdown' => [
|
|
|
|
'item_total' =>
|
|
|
|
[
|
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
|
'value' => $shopProduct->getPriceAfterDiscount(),
|
|
|
|
],
|
|
|
|
'tax_total' =>
|
|
|
|
[
|
|
|
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
|
|
|
'value' => $shopProduct->getTaxValue(),
|
2023-01-14 20:51:20 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2023-01-14 23:43:13 +00:00
|
|
|
],
|
|
|
|
"application_context" => [
|
|
|
|
"cancel_url" => route('payment.Cancel'),
|
|
|
|
"return_url" => route('payment.PayPalSuccess', ['payment' => $payment->id]),
|
|
|
|
'brand_name' => config('app.name', 'Laravel'),
|
|
|
|
'shipping_preference' => 'NO_SHIPPING'
|
|
|
|
]
|
2023-01-14 20:51:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
];
|
2023-01-14 22:34:54 +00:00
|
|
|
|
2023-01-14 20:51:20 +00:00
|
|
|
try {
|
|
|
|
// Call API with your client and get a response for your call
|
|
|
|
$response = getPayPalClient()->execute($request);
|
2023-01-14 22:34:54 +00:00
|
|
|
|
|
|
|
Redirect::away($response->result->links[1]->href)->send();
|
2023-01-14 23:43:13 +00:00
|
|
|
return;
|
2023-01-14 20:51:20 +00:00
|
|
|
} catch (HttpException $ex) {
|
2023-01-14 22:34:54 +00:00
|
|
|
error_log($ex->statusCode);
|
|
|
|
error_log($ex->getMessage());
|
|
|
|
|
|
|
|
$payment->delete();
|
2023-01-14 23:43:13 +00:00
|
|
|
Redirect::route('payment.Cancel');
|
|
|
|
return;
|
2023-01-14 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Request $laravelRequest
|
|
|
|
*/
|
|
|
|
function PaypalSuccess(Request $laravelRequest)
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
2023-01-14 23:43:13 +00:00
|
|
|
$user = User::findOrFail($user->id);
|
|
|
|
|
2023-01-14 22:34:54 +00:00
|
|
|
$payment = Payment::findOrFail($laravelRequest->payment);
|
|
|
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
2023-01-14 23:43:13 +00:00
|
|
|
|
2023-01-14 20:51:20 +00:00
|
|
|
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
|
|
|
$request->prefer('return=representation');
|
2023-01-14 22:34:54 +00:00
|
|
|
|
2023-01-14 20:51:20 +00:00
|
|
|
try {
|
|
|
|
// Call API with your client and get a response for your call
|
|
|
|
$response = getPayPalClient()->execute($request);
|
|
|
|
if ($response->statusCode == 201 || $response->statusCode == 200) {
|
2023-01-14 22:34:54 +00:00
|
|
|
//update payment
|
|
|
|
$payment->update([
|
2023-01-17 14:35:53 +00:00
|
|
|
'status' => 'paid',
|
2023-01-14 22:34:54 +00:00
|
|
|
'payment_id' => $response->result->id,
|
2023-01-14 20:51:20 +00:00
|
|
|
]);
|
2023-01-14 22:34:54 +00:00
|
|
|
|
2023-01-14 20:51:20 +00:00
|
|
|
event(new UserUpdateCreditsEvent($user));
|
2023-01-14 23:43:13 +00:00
|
|
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
2023-01-14 22:34:54 +00:00
|
|
|
|
|
|
|
// redirect to the payment success page with success message
|
|
|
|
Redirect::route('home')->with('success', 'Payment successful')->send();
|
2023-01-14 23:43:13 +00:00
|
|
|
} elseif (env('APP_ENV') == 'local') {
|
2023-01-14 22:34:54 +00:00
|
|
|
// If call returns body in response, you can get the deserialized version from the result attribute of the response
|
|
|
|
$payment->delete();
|
2023-01-14 20:51:20 +00:00
|
|
|
dd($response);
|
|
|
|
} else {
|
2023-01-14 22:34:54 +00:00
|
|
|
$payment->update([
|
2023-01-17 14:35:53 +00:00
|
|
|
'status' => 'cancelled',
|
2023-01-14 22:34:54 +00:00
|
|
|
'payment_id' => $response->result->id,
|
2023-01-14 23:43:13 +00:00
|
|
|
]);
|
2023-01-14 20:51:20 +00:00
|
|
|
abort(500);
|
|
|
|
}
|
|
|
|
} catch (HttpException $ex) {
|
|
|
|
if (env('APP_ENV') == 'local') {
|
|
|
|
echo $ex->statusCode;
|
2023-01-14 22:34:54 +00:00
|
|
|
$payment->delete();
|
2023-01-14 20:51:20 +00:00
|
|
|
dd($ex->getMessage());
|
|
|
|
} else {
|
2023-01-14 22:34:54 +00:00
|
|
|
$payment->update([
|
2023-01-17 14:35:53 +00:00
|
|
|
'status' => 'cancelled',
|
2023-01-14 22:34:54 +00:00
|
|
|
'payment_id' => $response->result->id,
|
2023-01-14 23:43:13 +00:00
|
|
|
]);
|
2023-01-14 20:51:20 +00:00
|
|
|
abort(422);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return PayPalHttpClient
|
|
|
|
*/
|
|
|
|
function getPayPalClient()
|
|
|
|
{
|
|
|
|
$environment = env('APP_ENV') == 'local'
|
|
|
|
? new SandboxEnvironment(getPaypalClientId(), getPaypalClientSecret())
|
|
|
|
: new ProductionEnvironment(getPaypalClientId(), getPaypalClientSecret());
|
|
|
|
return new PayPalHttpClient($environment);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getPaypalClientId()
|
|
|
|
{
|
|
|
|
return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID") : config("SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID");
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getPaypalClientSecret()
|
|
|
|
{
|
|
|
|
return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET") : config("SETTINGS::PAYMENTS:PAYPAL:SECRET");
|
|
|
|
}
|