create payment table, update controller
This commit is contained in:
parent
6aebdb95be
commit
adc5e60f17
|
|
@ -3,19 +3,61 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\Payment;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Session;
|
||||||
use Srmklive\PayPal\Services\PayPal as PayPalClient;
|
use Srmklive\PayPal\Services\PayPal as PayPalClient;
|
||||||
|
|
||||||
class PayPalController extends Controller
|
class PayPalController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private $items = [
|
||||||
|
[
|
||||||
|
'id' => 1,
|
||||||
|
'item' => [
|
||||||
|
'name' => 'photo',
|
||||||
|
'sku' => 'photo001',
|
||||||
|
'quantity' => '3',
|
||||||
|
'unit_amount' => [
|
||||||
|
'currency_code' => 'USD',
|
||||||
|
'value' => '10',
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'id' => 2,
|
||||||
|
'item' => [
|
||||||
|
'name' => 'oto',
|
||||||
|
'sku' => 'oto001',
|
||||||
|
'quantity' => '2',
|
||||||
|
'unit_amount' => [
|
||||||
|
'currency_code' => 'USD',
|
||||||
|
'value' => '10',
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
private $paymentCurrency = 'USD';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write code on Method
|
* Write code on Method
|
||||||
*
|
*
|
||||||
* @return response()
|
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return view('paypal');
|
// $provider = new PayPalClient;
|
||||||
|
// $provider->setApiCredentials(config('paypal'));
|
||||||
|
// $provider->getAccessToken();
|
||||||
|
|
||||||
|
// $id_order = session('paypal_payment_id');
|
||||||
|
// $detail = $id_order ? json_encode($provider->showOrderDetails($id_order)) : null;
|
||||||
|
// if($id_order){
|
||||||
|
// dd(session('paypal_payment_id'));
|
||||||
|
// }
|
||||||
|
// dd($id_order);
|
||||||
|
return view('paypal', ['user' => $request->user()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -25,6 +67,13 @@ class PayPalController extends Controller
|
||||||
*/
|
*/
|
||||||
public function payment(Request $request)
|
public function payment(Request $request)
|
||||||
{
|
{
|
||||||
|
$id = $request->input('id');
|
||||||
|
$user_id = $request->input('user_id');
|
||||||
|
$item = collect($this->items)->filter(function ($item) use ($id) {
|
||||||
|
return $item['id'] == $id;
|
||||||
|
})->first();
|
||||||
|
|
||||||
|
$totalAmount = $item['item']['unit_amount']['value'] * $item['item']['quantity'];
|
||||||
$provider = new PayPalClient;
|
$provider = new PayPalClient;
|
||||||
$provider->setApiCredentials(config('paypal'));
|
$provider->setApiCredentials(config('paypal'));
|
||||||
$paypalToken = $provider->getAccessToken();
|
$paypalToken = $provider->getAccessToken();
|
||||||
|
|
@ -38,63 +87,47 @@ class PayPalController extends Controller
|
||||||
"purchase_units" => [
|
"purchase_units" => [
|
||||||
0 => [
|
0 => [
|
||||||
'amount' => [
|
'amount' => [
|
||||||
'currency_code' => 'EUR',
|
'currency_code' => $this->paymentCurrency,
|
||||||
'value' => '5',
|
'value' => $totalAmount,
|
||||||
"breakdown" => [
|
"breakdown" => [
|
||||||
"item_total" => [
|
"item_total" => [
|
||||||
"currency_code" => "EUR", "value" => "5",
|
"currency_code" => $this->paymentCurrency,
|
||||||
|
"value" => $totalAmount,
|
||||||
],
|
],
|
||||||
"shipping" => [
|
"shipping" => [
|
||||||
"currency_code" => "EUR", "value" => "0",
|
"currency_code" => $this->paymentCurrency,
|
||||||
|
"value" => "0",
|
||||||
],
|
],
|
||||||
"tax_total" => [
|
"tax_total" => [
|
||||||
"currency_code" => "EUR", "value" => "0",
|
"currency_code" => $this->paymentCurrency,
|
||||||
|
"value" => "0",
|
||||||
],
|
],
|
||||||
"discount" => [
|
"discount" => [
|
||||||
"currency_code" => "EUR", "value" => "0",
|
"currency_code" => $this->paymentCurrency,
|
||||||
],
|
"value" => "0",
|
||||||
],
|
|
||||||
],
|
|
||||||
'items' => [
|
|
||||||
[
|
|
||||||
'name' => 'photo',
|
|
||||||
'sku' => 'photo001',
|
|
||||||
'quantity' => '3',
|
|
||||||
'unit_amount' => [
|
|
||||||
'currency_code' => 'EUR',
|
|
||||||
'value' => '1.00',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'oto',
|
|
||||||
'sku' => 'oto001',
|
|
||||||
'quantity' => '2',
|
|
||||||
'unit_amount' => [
|
|
||||||
'currency_code' => 'EUR',
|
|
||||||
'value' => '1.00',
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
//items
|
||||||
|
'items' => [$item['item']]
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
dd($order);
|
|
||||||
|
// dd($order);
|
||||||
if (isset($order['id']) && null != $order['id']) {
|
if (isset($order['id']) && null != $order['id']) {
|
||||||
|
|
||||||
foreach ($order['links'] as $links) {
|
foreach ($order['links'] as $links) {
|
||||||
if ('approve' == $links['rel']) {
|
if ('approve' == $links['rel']) {
|
||||||
|
Payment::create(['user_id'=>$user_id, 'id_order'=>$order['id'], 'status'=>'pending']);
|
||||||
return redirect()->away($links['href']);
|
return redirect()->away($links['href']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect(route('cancel.payment'))->with('error', 'Something went wrong.');
|
return response()->redirectTo(route('paypal.payment/cancel', ["error" => 'Something went wrong.']));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return response([
|
return response()->redirectTo(route('paypal.payment/cancel', ["error" => $order['error']['message'] ?? 'Something went wrong.']));
|
||||||
$order['message'] ?? 'Something went wrong.',
|
|
||||||
]);
|
|
||||||
return redirect(route('create.payment'))
|
|
||||||
->with('error', $order['message'] ?? 'Something went wrong.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -104,10 +137,15 @@ class PayPalController extends Controller
|
||||||
*
|
*
|
||||||
* @return response()
|
* @return response()
|
||||||
*/
|
*/
|
||||||
public function paymentCancel()
|
public function paymentCancel(Request $request)
|
||||||
{
|
{
|
||||||
return redirect(route('paypal'))
|
$token = $request->input('token');
|
||||||
->with('error', 'You have canceled the transaction.');
|
$order = Payment::where('id_order', $token)->first();
|
||||||
|
$order['status'] = 'cancel';
|
||||||
|
$order->save();
|
||||||
|
|
||||||
|
$error = $request->input("error");
|
||||||
|
return redirect(route('paypal'))->with('error', $error ?? 'You have canceled the transaction.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -117,13 +155,20 @@ class PayPalController extends Controller
|
||||||
*/
|
*/
|
||||||
public function paymentSuccess(Request $request)
|
public function paymentSuccess(Request $request)
|
||||||
{
|
{
|
||||||
|
$token = $request->input('token');
|
||||||
|
|
||||||
$provider = new PayPalClient;
|
$provider = new PayPalClient;
|
||||||
$provider->setApiCredentials(config('paypal'));
|
$provider->setApiCredentials(config('paypal'));
|
||||||
$provider->getAccessToken();
|
$provider->getAccessToken();
|
||||||
$response = $provider->capturePaymentOrder($request['token']);
|
$response = $provider->capturePaymentOrder($token);
|
||||||
|
|
||||||
|
|
||||||
|
$order = Payment::where('id_order', $token)->first();
|
||||||
|
$order['status'] = 'success';
|
||||||
|
$order->save();
|
||||||
|
|
||||||
if (isset($response['status']) && 'COMPLETED' == $response['status']) {
|
if (isset($response['status']) && 'COMPLETED' == $response['status']) {
|
||||||
return redirect(route('paypal'))->with('success', 'Transaction complete.');
|
return redirect(route('paypal'))->with(['success', 'Transaction complete.']);
|
||||||
} else {
|
} else {
|
||||||
return redirect(route('paypal'))
|
return redirect(route('paypal'))
|
||||||
->with('error', $response['message'] ?? 'Something went wrong.');
|
->with('error', $response['message'] ?? 'Something went wrong.');
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Payment extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'id_order',
|
||||||
|
'status',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('payments', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->integer('user_id');
|
||||||
|
$table->string('id_order');
|
||||||
|
$table->string('status');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('payments');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -36,9 +36,8 @@
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
<center>
|
<center>
|
||||||
<a href="{{ route('paypal.payment') }}" class="btn btn-success">Pay with PayPal </a>
|
<a href="{{ route('paypal.payment', ['id'=>2, 'user_id'=>$user->id]) }}" class="btn btn-success">Pay with PayPal </a>
|
||||||
</center>
|
</center>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -44,3 +44,5 @@ Route::get('invoices', function () {
|
||||||
$inv
|
$inv
|
||||||
);
|
);
|
||||||
})->name('payment.invoices');
|
})->name('payment.invoices');
|
||||||
|
|
||||||
|
Route::get('/paypal/payment', [PayPalController::class, 'payment'])->name('paypal.payment');
|
||||||
|
|
@ -27,11 +27,11 @@ Route::middleware('auth')->group(function () {
|
||||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||||
|
Route::group(['prefix' => 'paypal'], function () {
|
||||||
|
Route::get('/', [PayPalController::class, 'index'])->name('paypal');
|
||||||
|
Route::get('/payment/success', [PayPalController::class, 'paymentSuccess'])->name('paypal.payment.success');
|
||||||
|
Route::get('/payment/cancel', [PayPalController::class, 'paymentCancel'])->name('paypal.payment/cancel');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::get('paypal', [PayPalController::class, 'index'])->name('paypal');
|
|
||||||
Route::get('paypal/payment', [PayPalController::class, 'payment'])->name('paypal.payment');
|
|
||||||
Route::get('paypal/payment/success', [PayPalController::class, 'paymentSuccess'])->name('paypal.payment.success');
|
|
||||||
Route::get('paypal/payment/cancel', [PayPalController::class, 'paymentCancel'])->name('paypal.payment/cancel');
|
|
||||||
|
|
||||||
require __DIR__ . '/auth.php';
|
require __DIR__ . '/auth.php';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue