From 6aebdb95bef69a150bc77bf36ef72cdea3866a10 Mon Sep 17 00:00:00 2001 From: "kai.t" Date: Wed, 6 Dec 2023 11:05:56 +0700 Subject: [PATCH] update order --- app/Http/Controllers/PayPalController.php | 67 +++++++++++++++++------ routes/api.php | 26 ++++++++- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/PayPalController.php b/app/Http/Controllers/PayPalController.php index 4cfabee..e520d96 100644 --- a/app/Http/Controllers/PayPalController.php +++ b/app/Http/Controllers/PayPalController.php @@ -2,10 +2,8 @@ namespace App\Http\Controllers; -use Auth; -use Illuminate\Http\Request; use App\Http\Controllers\Controller; -use App\Services\PayPalService as PayPalSvc; +use Illuminate\Http\Request; use Srmklive\PayPal\Services\PayPal as PayPalClient; class PayPalController extends Controller @@ -31,7 +29,7 @@ class PayPalController extends Controller $provider->setApiCredentials(config('paypal')); $paypalToken = $provider->getAccessToken(); - $response = $provider->createOrder([ + $order = $provider->createOrder([ "intent" => "CAPTURE", "application_context" => [ "return_url" => route('paypal.payment.success'), @@ -39,18 +37,52 @@ class PayPalController extends Controller ], "purchase_units" => [ 0 => [ - "amount" => [ - "currency_code" => "USD", - "value" => "100.00" - ] - ] - ] + 'amount' => [ + 'currency_code' => 'EUR', + 'value' => '5', + "breakdown" => [ + "item_total" => [ + "currency_code" => "EUR", "value" => "5", + ], + "shipping" => [ + "currency_code" => "EUR", "value" => "0", + ], + "tax_total" => [ + "currency_code" => "EUR", "value" => "0", + ], + "discount" => [ + "currency_code" => "EUR", "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', + ], + ], + ], + ], + ], ]); + dd($order); + if (isset($order['id']) && null != $order['id']) { - if (isset($response['id']) && $response['id'] != null) { - - foreach ($response['links'] as $links) { - if ($links['rel'] == 'approve') { + foreach ($order['links'] as $links) { + if ('approve' == $links['rel']) { return redirect()->away($links['href']); } } @@ -58,8 +90,11 @@ class PayPalController extends Controller return redirect(route('cancel.payment'))->with('error', 'Something went wrong.'); } else { + return response([ + $order['message'] ?? 'Something went wrong.', + ]); return redirect(route('create.payment')) - ->with('error', $response['message'] ?? 'Something went wrong.'); + ->with('error', $order['message'] ?? 'Something went wrong.'); } } @@ -87,7 +122,7 @@ class PayPalController extends Controller $provider->getAccessToken(); $response = $provider->capturePaymentOrder($request['token']); - if (isset($response['status']) && $response['status'] == 'COMPLETED') { + if (isset($response['status']) && 'COMPLETED' == $response['status']) { return redirect(route('paypal'))->with('success', 'Transaction complete.'); } else { return redirect(route('paypal')) diff --git a/routes/api.php b/routes/api.php index f668bd5..cd5c826 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use App\Http\Controllers\PayPalController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; +use Srmklive\PayPal\Services\PayPal as PayPalClient; /* |-------------------------------------------------------------------------- @@ -13,10 +14,33 @@ use Illuminate\Support\Facades\Route; | routes are loaded by the RouteServiceProvider and all of them will | be assigned to the "api" middleware group. Make something great! | -*/ + */ Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); Route::post('create-payment', [PayPalController::class, 'index'])->name('payment.create'); +Route::get('invoices', function () { + $provider = new PayPalClient; + $provider->setApiCredentials(config('paypal')); + $provider->getAccessToken(); + + $data = json_decode('{ + "name": "Video Streaming Service", + "description": "Video streaming service", + "type": "SERVICE", + "category": "SOFTWARE", + "image_url": "https://example.com/streaming.jpg", + "home_url": "https://example.com/home" + }', true); + + $product = $provider->setRequestHeader('PayPal-Request-Id', 'create-product-' . time())->createProduct($data); + + $inv = $provider->showOrderDetails('0KX38537YL610435R'); + // $invoice_no = $provider->generateInvoiceNumber(); + + dd( + $inv + ); +})->name('payment.invoices');