-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
446 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\CreatePaymentRequest; | ||
use App\Models\Cart; | ||
use App\Models\Payment; | ||
use App\Models\Product; | ||
use Illuminate\Http\Request; | ||
use Srmklive\PayPal\Services\PayPal as PayPalClient; | ||
|
||
class PaypalController extends Controller | ||
{ | ||
private $paymentModel; | ||
private $productModel; | ||
private $cartModel; | ||
|
||
public function __construct(Payment $paymentModel, Product $productModel, Cart $cartModel) | ||
{ | ||
$this->paymentModel = $paymentModel; | ||
$this->productModel = $productModel; | ||
$this->cartModel = $cartModel; | ||
} | ||
|
||
public function handlePayment(CreatePaymentRequest $request) | ||
{ | ||
$provider = new PayPalClient; | ||
$provider->setApiCredentials(config('paypal')); | ||
$paypalToken = $provider->getAccessToken(); | ||
|
||
$cartId = $request['cart_id']; | ||
$userId = optional(auth()->user())->id; | ||
if ($userId == null) { | ||
return response()->json(['message' => 'Unauthorized'], 403); | ||
} | ||
|
||
$result = $this->paymentModel->pay($cartId, $userId, 'Paypal'); | ||
if ($result == null) { | ||
return response()->json(['message' => 'Cart not found'], 404); | ||
} | ||
|
||
if ($result == 'Cart was checked out'){ | ||
return response()->json(['message' => $result], 400); | ||
} | ||
|
||
$response = $provider->createOrder([ | ||
"intent" => "CAPTURE", | ||
"application_context" => [ | ||
"return_url" => route('success.payment'), | ||
"cancel_url" => route('cancel.payment'), | ||
], | ||
"purchase_units" => [ | ||
0 => [ | ||
"amount" => [ | ||
"currency_code" => "USD", | ||
"value" => sprintf("%.2f", $result['total_price']) | ||
] | ||
] | ||
] | ||
]); | ||
|
||
if (isset($response['id']) && $response['id'] != null) { | ||
foreach ($response['links'] as $links) { | ||
if ($links['rel'] == 'approve') { | ||
$res = $this->paymentModel->updateStatusPayment($result['payment_id'], "SUCCESS"); | ||
if ($res == 0) { | ||
return response()->json(['message' => 'error update payment success'], 500); | ||
} | ||
|
||
$this->cartModel->updateStatusCart($cartId, 'SUCCESS'); | ||
|
||
foreach ($result['products'][0] as $key => $value) { | ||
$this->productModel->decrease($key, $value); | ||
} | ||
|
||
redirect()->away($links['href']); | ||
return response()->json(['result' => $result], 200); | ||
} | ||
} | ||
|
||
$res = $this->paymentModel->updateStatusPayment($result['payment_id'], "FAILED"); | ||
if ($res == 0) { | ||
return response()->json(['message' => 'error update payment failed'], 500); | ||
} | ||
|
||
$this->cartModel->updateStatusCart($cartId, 'FAILED'); | ||
|
||
return redirect() | ||
->route('cancel.payment') | ||
->with('error', 'Something went wrong.'); | ||
} else { | ||
$res = $this->paymentModel->updateStatusPayment($result['payment_id'], "FAILED"); | ||
if ($res == 0) { | ||
return response()->json(['message' => 'error update payment failed'], 500); | ||
} | ||
|
||
$this->cartModel->updateStatusCart($cartId, 'FAILED'); | ||
|
||
return redirect() | ||
->route('create.payment') | ||
->with('error', $response['message'] ?? 'Something went wrong.'); | ||
} | ||
} | ||
|
||
public function paymentCancel() | ||
{ | ||
return redirect() | ||
->route('create.payment') | ||
->with('error', $response['message'] ?? 'You have canceled the transaction.'); | ||
} | ||
|
||
public function paymentSuccess(Request $request) | ||
{ | ||
$provider = new PayPalClient; | ||
$provider->setApiCredentials(config('paypal')); | ||
$provider->getAccessToken(); | ||
$response = $provider->capturePaymentOrder($request['token']); | ||
if (isset($response['status']) && $response['status'] == 'COMPLETED') { | ||
return redirect() | ||
->route('create.payment') | ||
->with('success', 'Transaction complete.'); | ||
} else { | ||
return redirect() | ||
->route('create.payment') | ||
->with('error', $response['message'] ?? 'Something went wrong.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class CreatePaymentRequest extends FormRequest | ||
{ | ||
public function rules(){ | ||
return [ | ||
'cart_id' => 'required', | ||
]; | ||
} | ||
|
||
protected function failedValidation(Validator $validator): void | ||
{ | ||
throw new ValidationException($validator, response()->json([ | ||
'success' => false, | ||
'message' => $validator->messages()->first(), | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class Payment extends Model | ||
{ | ||
public function pay($cartId, $userId, $payMethod) | ||
{ | ||
DB::beginTransaction(); | ||
try { | ||
$timeNow = Carbon::now()->format('Y-m-d H:i:s'); | ||
$carts = DB::table('carts')->where('id', $cartId)->where('user_id', $userId)->get(); | ||
if (count($carts) == 0) { | ||
return null; | ||
} | ||
|
||
$amount = 0; | ||
$productIds = array(); | ||
foreach ($carts as $cart){ | ||
if ($cart->status == 'SUCCESS'){ | ||
continue; | ||
} | ||
$amount += $cart->quantity; | ||
array_push($productIds, $cart->product_id); | ||
} | ||
|
||
if ($amount == 0){ | ||
return 'Cart was checked out'; | ||
} | ||
|
||
$products = DB::table('products')->whereIn('id', $productIds)->get(); | ||
$totalPrice = 0.00; | ||
$productsResult = []; | ||
|
||
foreach ($products as $product){ | ||
foreach ($carts as $cart){ | ||
if ($product->id == $cart->product_id){ | ||
$price = $product->price * $cart->quantity; | ||
$totalPrice += $price; | ||
array_push($productsResult, [ | ||
$product->id => $cart->quantity, | ||
]); | ||
} | ||
} | ||
} | ||
|
||
$paymentId = DB::table('payments')->insertGetId([ | ||
'payment_date' => $timeNow, | ||
'payment_method' => $payMethod, | ||
'amount' => $amount, | ||
'user_id' => $userId, | ||
'created_at' => $timeNow, | ||
]); | ||
|
||
$orderId = DB::table('orders')->insertGetId([ | ||
'order_date' => $timeNow, | ||
'total_price' => $totalPrice, | ||
'user_id' => $userId, | ||
'payment_id' => $paymentId, | ||
'created_at' => $timeNow, | ||
]); | ||
|
||
foreach ($products as $product){ | ||
foreach ($carts as $cart){ | ||
if ($product->id == $cart->product_id){ | ||
$price = $product->price * $cart->quantity; | ||
DB::table('order_item')->insertGetId([ | ||
'quantity' => $cart->quantity, | ||
'price' => $price, | ||
'product_id' => $product->id, | ||
'order_id' => $orderId, | ||
'created_at' => $timeNow, | ||
]); | ||
} | ||
} | ||
} | ||
|
||
DB::commit(); | ||
|
||
return [ | ||
'payment_id' => $paymentId, | ||
'total_price' => $totalPrice, | ||
'order_id' => $orderId, | ||
'products' => $productsResult, | ||
]; | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
} | ||
} | ||
|
||
public function updateStatusPayment($paymentId, $status){ | ||
return DB::table('payments')->where('id', $paymentId)->update([ 'status' => $status]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.