refactor: ♻️ Extracted Payment logic to events
This commit is contained in:
parent
f00a11da0e
commit
45aaac4189
|
@ -31,15 +31,15 @@ function PaypalPay(Request $request)
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
||||||
|
|
||||||
// create a new payment
|
// create a new payment
|
||||||
$payment = Payment::create([
|
$payment = Payment::create([
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
'payment_id' => null,
|
'payment_id' => null,
|
||||||
'payment_method' => 'paypal',
|
'payment_method' => 'paypal',
|
||||||
'type' => $shopProduct->type,
|
'type' => $shopProduct->type,
|
||||||
'status' => 'open',
|
'status' => 'open',
|
||||||
'amount' => $shopProduct->quantity,
|
'amount' => $shopProduct->quantity,
|
||||||
'price' => $shopProduct->price - ($shopProduct->price*PartnerDiscount::getDiscount()/100),
|
'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
|
||||||
'tax_value' => $shopProduct->getTaxValue(),
|
'tax_value' => $shopProduct->getTaxValue(),
|
||||||
'tax_percent' => $shopProduct->getTaxPercent(),
|
'tax_percent' => $shopProduct->getTaxPercent(),
|
||||||
'total_price' => $shopProduct->getTotalPrice(),
|
'total_price' => $shopProduct->getTotalPrice(),
|
||||||
|
@ -50,52 +50,52 @@ function PaypalPay(Request $request)
|
||||||
$request = new OrdersCreateRequest();
|
$request = new OrdersCreateRequest();
|
||||||
$request->prefer('return=representation');
|
$request->prefer('return=representation');
|
||||||
$request->body = [
|
$request->body = [
|
||||||
"intent" => "CAPTURE",
|
"intent" => "CAPTURE",
|
||||||
"purchase_units" => [
|
"purchase_units" => [
|
||||||
[
|
[
|
||||||
"reference_id" => uniqid(),
|
"reference_id" => uniqid(),
|
||||||
"description" => $shopProduct->display . (PartnerDiscount::getDiscount()?(" (" . __('Discount') . " " . PartnerDiscount::getDiscount() . '%)'):""),
|
"description" => $shopProduct->display . (PartnerDiscount::getDiscount() ? (" (" . __('Discount') . " " . PartnerDiscount::getDiscount() . '%)') : ""),
|
||||||
"amount" => [
|
"amount" => [
|
||||||
"value" => $shopProduct->getTotalPrice(),
|
"value" => $shopProduct->getTotalPrice(),
|
||||||
'currency_code' => strtoupper($shopProduct->currency_code),
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
||||||
'breakdown' => [
|
'breakdown' => [
|
||||||
'item_total' =>
|
'item_total' =>
|
||||||
[
|
[
|
||||||
'currency_code' => strtoupper($shopProduct->currency_code),
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
||||||
'value' => $shopProduct->getPriceAfterDiscount(),
|
'value' => $shopProduct->getPriceAfterDiscount(),
|
||||||
],
|
],
|
||||||
'tax_total' =>
|
'tax_total' =>
|
||||||
[
|
[
|
||||||
'currency_code' => strtoupper($shopProduct->currency_code),
|
'currency_code' => strtoupper($shopProduct->currency_code),
|
||||||
'value' => $shopProduct->getTaxValue(),
|
'value' => $shopProduct->getTaxValue(),
|
||||||
]
|
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
],
|
|
||||||
"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'
|
|
||||||
]
|
]
|
||||||
|
],
|
||||||
|
"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'
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Call API with your client and get a response for your call
|
// Call API with your client and get a response for your call
|
||||||
$response = getPayPalClient()->execute($request);
|
$response = getPayPalClient()->execute($request);
|
||||||
|
|
||||||
Redirect::away($response->result->links[1]->href)->send();
|
Redirect::away($response->result->links[1]->href)->send();
|
||||||
|
return;
|
||||||
} catch (HttpException $ex) {
|
} catch (HttpException $ex) {
|
||||||
error_log($ex->statusCode);
|
error_log($ex->statusCode);
|
||||||
error_log($ex->getMessage());
|
error_log($ex->getMessage());
|
||||||
|
|
||||||
$payment->delete();
|
$payment->delete();
|
||||||
return Redirect::route('payment.Cancel');
|
Redirect::route('payment.Cancel');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -104,6 +104,8 @@ function PaypalPay(Request $request)
|
||||||
function PaypalSuccess(Request $laravelRequest)
|
function PaypalSuccess(Request $laravelRequest)
|
||||||
{
|
{
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
$user = User::findOrFail($user->id);
|
||||||
|
|
||||||
$payment = Payment::findOrFail($laravelRequest->payment);
|
$payment = Payment::findOrFail($laravelRequest->payment);
|
||||||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||||
|
|
||||||
|
@ -114,55 +116,6 @@ function PaypalSuccess(Request $laravelRequest)
|
||||||
// Call API with your client and get a response for your call
|
// Call API with your client and get a response for your call
|
||||||
$response = getPayPalClient()->execute($request);
|
$response = getPayPalClient()->execute($request);
|
||||||
if ($response->statusCode == 201 || $response->statusCode == 200) {
|
if ($response->statusCode == 201 || $response->statusCode == 200) {
|
||||||
//update server limit
|
|
||||||
if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
|
|
||||||
if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
|
|
||||||
$user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//update User with bought item
|
|
||||||
if ($shopProduct->type=="Credits") {
|
|
||||||
$user->increment('credits', $shopProduct->quantity);
|
|
||||||
}elseif ($shopProduct->type=="Server slots"){
|
|
||||||
$user->increment('server_limit', $shopProduct->quantity);
|
|
||||||
}
|
|
||||||
//give referral commission always
|
|
||||||
if((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type=="Credits" && config("SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION") == "true"){
|
|
||||||
if($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()){
|
|
||||||
$ref_user = User::findOrFail($ref_user->referral_id);
|
|
||||||
$increment = number_format($shopProduct->quantity*(PartnerDiscount::getCommission($ref_user->id))/100,0,"","");
|
|
||||||
$ref_user->increment('credits', $increment);
|
|
||||||
|
|
||||||
//LOGS REFERRALS IN THE ACTIVITY LOG
|
|
||||||
activity()
|
|
||||||
->performedOn($user)
|
|
||||||
->causedBy($ref_user)
|
|
||||||
->log('gained '. $increment.' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//update role give Referral-reward
|
|
||||||
if ($user->role == 'member') {
|
|
||||||
$user->update(['role' => 'client']);
|
|
||||||
|
|
||||||
//give referral commission only on first purchase
|
|
||||||
if((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type=="Credits" && config("SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION") == "false"){
|
|
||||||
if($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()){
|
|
||||||
$ref_user = User::findOrFail($ref_user->referral_id);
|
|
||||||
$increment = number_format($shopProduct->quantity*(PartnerDiscount::getCommission($ref_user->id))/100,0,"","");
|
|
||||||
$ref_user->increment('credits', $increment);
|
|
||||||
|
|
||||||
//LOGS REFERRALS IN THE ACTIVITY LOG
|
|
||||||
activity()
|
|
||||||
->performedOn($user)
|
|
||||||
->causedBy($ref_user)
|
|
||||||
->log('gained '. $increment.' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//update payment
|
//update payment
|
||||||
$payment->update([
|
$payment->update([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
|
@ -170,13 +123,11 @@ function PaypalSuccess(Request $laravelRequest)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
event(new UserUpdateCreditsEvent($user));
|
event(new UserUpdateCreditsEvent($user));
|
||||||
event(new PaymentEvent($payment));
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||||
|
|
||||||
error_log("Payment successfull");
|
|
||||||
|
|
||||||
// redirect to the payment success page with success message
|
// redirect to the payment success page with success message
|
||||||
Redirect::route('home')->with('success', 'Payment successful')->send();
|
Redirect::route('home')->with('success', 'Payment successful')->send();
|
||||||
} elseif(env('APP_ENV') == 'local') {
|
} elseif (env('APP_ENV') == 'local') {
|
||||||
// If call returns body in response, you can get the deserialized version from the result attribute of the response
|
// If call returns body in response, you can get the deserialized version from the result attribute of the response
|
||||||
$payment->delete();
|
$payment->delete();
|
||||||
dd($response);
|
dd($response);
|
||||||
|
|
Loading…
Reference in a new issue