Skip to content

Commit 7c5868d

Browse files
committed
Init Midtrans
1 parent 18ae227 commit 7c5868d

8 files changed

+252
-12
lines changed

app/Http/Controllers/User/CheckoutController.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77
use Illuminate\Http\Request;
88
use App\Http\Controllers\Controller;
99
use App\Http\Requests\User\Checkout\Store;
10+
use App\Mail\Checkout\AfterCheckout;
1011
use Auth;
1112
use Mail;
12-
use App\Mail\Checkout\AfterCheckout;
13+
use Str;
14+
use Midtrans;
1315

1416
class CheckoutController extends Controller
1517
{
18+
public function __construct()
19+
{
20+
Midtrans\Config::$serverKey = env('MIDTRANS_SERVERKEY');
21+
Midtrans\Config::$isProduction = env('MIDTRANS_IS_PRODUCTION');
22+
Midtrans\Config::$isSanitized = env('MIDTRANS_IS_SANITIZED');
23+
Midtrans\Config::$is3ds = env('MIDTRANS_IS_3DS');
24+
}
25+
1626
/**
1727
* Display a listing of the resource.
1828
*
@@ -63,6 +73,7 @@ public function store(Store $request, Camp $camp)
6373

6474
// create checkout
6575
$checkout = Checkout::create($data);
76+
$this->getSnapRedirect($checkout);
6677

6778
// Setting Email
6879
Mail::to(Auth::user()->email)->send(new AfterCheckout($checkout));
@@ -126,4 +137,62 @@ public function success(Checkout $checkout)
126137
return view('checkout.success');
127138
}
128139

140+
/**
141+
* Midtrans handler
142+
*/
143+
public function getSnapRedirect(Checkout $checkout)
144+
{
145+
$orderId = $checkout->id.'-'.Str::random(5);
146+
$price = $checkout->Camp->price = 1000;
147+
$checkout->midtrans_booking_code = $orderId;
148+
149+
$transaction_details = [
150+
'order_id' => $orderId,
151+
'gross_amount' => $price
152+
];
153+
154+
$item_details[] = [
155+
'id' => $orderId,
156+
'price' => $price,
157+
'quantity' => 1,
158+
'name' => 'Payment for {$checkout->Camp->title}'
159+
];
160+
161+
$userData = [
162+
'first_name' => $checkout->User->name,
163+
'last_name' => '',
164+
'address' => '',
165+
'city' => '',
166+
'postal_code' => '',
167+
'phone' => '$checkout->User->phone',
168+
'country_code' => "IDN"
169+
];
170+
171+
$customer_details = [
172+
"first_name" => $checkout->User->name,
173+
"last_name" => "",
174+
"email" => $checkout->User->email,
175+
"phone" => $checkout->User->phone,
176+
"billing_address" => $userData,
177+
"shipping_address" => $userData
178+
];
179+
180+
$midtrans_params = [
181+
'transaction_details' => $transaction_details,
182+
'customer_details' => $customer_details,
183+
'item_details' => $item_details
184+
];
185+
186+
try {
187+
//Get Snap Payment Page URL
188+
$paymentUrl = \Midtrans\Snap::createTransaction($params)->redirect_url;
189+
$checkout->midtrans_url = $paymentUrl;
190+
$checkout->save();
191+
192+
return $paymentUrl;
193+
; } catch (Exception $e) {
194+
return false;
195+
}
196+
}
197+
129198
}

app/Models/Checkout.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ class Checkout extends Model
1313

1414
protected $fillable = [
1515
'user_id',
16-
'camp_id',
17-
'card_number',
18-
'expired',
19-
'cvc',
20-
'is_paid'
16+
'camp_id',
17+
'payment_status',
18+
'midtrans_url',
19+
'midtrans_booking_code'
2120
];
2221

23-
public function setExpiredAttribute($value)
24-
{
25-
$this->attributes['expired'] =date('Y-m-t', strtotime($value));
26-
}
22+
// Dah Kaga dipake
23+
// public function setExpiredAttribute($value)
24+
// {
25+
// $this->attributes['expired'] =date('Y-m-t', strtotime($value));
26+
// }
2727

2828
/**
2929
* Get the Camp that owns the Checkout

app/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class User extends Authenticatable
2424
'password',
2525
'avatar',
2626
'occupation',
27+
'phone',
2728
'is_admin',
2829
'email_verified_at'
2930
];

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"laravel/framework": "^8.75",
1313
"laravel/sanctum": "^2.11",
1414
"laravel/socialite": "^5.6",
15-
"laravel/tinker": "^2.5"
15+
"laravel/tinker": "^2.5",
16+
"midtrans/midtrans-php": "^2.5"
1617
},
1718
"require-dev": {
1819
"facade/ignition": "^2.5",

composer.lock

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class RemoveCardNumberAndExpiredAndCvcAndIsPaidInCheckoutsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('checkouts', function (Blueprint $table) {
17+
$table->dropColumn(
18+
[
19+
'card_number',
20+
'expired',
21+
'cvc',
22+
'is_paid'
23+
]);
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::table('checkouts', function (Blueprint $table) {
35+
$table->string('card_number', 20)->nullable();
36+
$table->date('expired')->nullable();
37+
$table->string('cvc', 3)->nullable();
38+
// is_paid default false karena setelah checkout tidak langsung bayar
39+
$table->boolean('is_paid')->default(false);
40+
});
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddPaymentStatusAndMidtransUrlAndMidtransBookingCodeInCheckoutsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('checkouts', function (Blueprint $table) {
17+
$table->string('payment_status', 100)->default('waiting')->after('camp_id');
18+
$table->string('midtrans_url')->nullable()->after('payment_status');
19+
$table->string('midtrans_booking_code')->nullable()->after('midtrans_url');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('checkouts', function (Blueprint $table) {
31+
$table->dropColumn(
32+
[
33+
'payment_status',
34+
'midtrans_url',
35+
'midtrans_booking_code'
36+
]
37+
);
38+
});
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddPhoneNumber extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->string('phone', 1)->nullable()->after('occupation');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('users', function (Blueprint $table) {
29+
$table->dropColumn('phone');
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)