diff --git a/app/Http/Controllers/User/UserTransactionController.php b/app/Http/Controllers/User/UserTransactionController.php index ae86f7a..564f54d 100644 --- a/app/Http/Controllers/User/UserTransactionController.php +++ b/app/Http/Controllers/User/UserTransactionController.php @@ -192,10 +192,14 @@ class UserTransactionController extends Controller 'callbacks' => [ 'finish' => route('user-pembeli.index'), ], - 'enabled_payments' => ['credit_card', 'gopay', 'qris'], + 'enabled_payments' => [ + 'credit_card', 'shopeepay', 'gopay', 'other_qris' + ] ]; - $client = new Client(); + $client = new Client([ + 'verify' => false + ]); $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); @@ -208,7 +212,7 @@ class UserTransactionController extends Controller ], ]); - $result = $response->getBody(); + $result = json_decode($response->getBody(), true); Transaction::where('id', $query->id)->update([ 'token' => $result['token'], @@ -415,16 +419,18 @@ class UserTransactionController extends Controller $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); - $response = Http::withHeaders([ + $response = Http::withOptions([ + 'verify' => false + ])->withHeaders([ 'Content-Type' => 'application/json', 'Authorization' => "Basic $auth", ])->get('https://api.sandbox.midtrans.com/v2/' . $request->id . '/status'); $result = json_decode($response->body(), true); - if ($result['transaction'] == 'settlement') { + if ($result['transaction_status'] == 'settlement') { $transaction = 'success'; - } elseif ($result['transaction'] == 'capture') { + } elseif ($result['transaction_status'] == 'capture') { if ($result['fraud_status'] == 'accept') { $transaction = 'success'; } elseif ($result['fraud_status'] == 'challenge') { @@ -436,11 +442,10 @@ class UserTransactionController extends Controller $query1 = Transaction::where('id', $request->id)->update([ 'metode_pembayaran' => $result['payment_type'], - 'tanggal_transaksi' => now(), + 'tanggal_transaksi' => $result['transaction_time'], 'status_transaksi' => $transaction, 'status_pembayaran' => $result['transaction_status'], 'fraud_status' => $result['fraud_status'], - 'reference_id' => $result['reference_id'], 'signature_key' => $result['signature_key'], ]); @@ -516,7 +521,9 @@ class UserTransactionController extends Controller { $auth = base64_encode(env('MIDTRANS_SERVER_KEY')); - $response = Http::withHeaders([ + $response = Http::withOptions([ + 'verify' => false + ])->withHeaders([ 'Content-Type' => 'application/json', 'Authorization' => "Basic $auth", ])->post('https://api.sandbox.midtrans.com/v2/' . $request->id . '/cancel'); @@ -526,10 +533,17 @@ class UserTransactionController extends Controller try { DB::beginTransaction(); - Transaction::where('id', $request->id)->update([ - 'status_transaksi' => 'failure', - 'status_pembayaran' => $result['transaction_status'], - ]); + if(in_array($result['status_code'],['412','401'])){ + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'failure', + 'status_pembayaran' => 'cancel', + ]); + }else{ + Transaction::where('id', $request->id)->update([ + 'status_transaksi' => 'failure', + 'status_pembayaran' => $result['transaction_status'], + ]); + } TransactionDescription::create([ 'transaction_id' => $request->id, @@ -554,6 +568,7 @@ class UserTransactionController extends Controller return response()->json([ 'status' => false, 'message' => 'Transaksi gagal dibatalkan', + 'data' => $result ]); } } diff --git a/database/migrations/2023_08_01_073859_create_transactions_table.php b/database/migrations/2023_08_01_073859_create_transactions_table.php index 0e81c28..fb52e88 100644 --- a/database/migrations/2023_08_01_073859_create_transactions_table.php +++ b/database/migrations/2023_08_01_073859_create_transactions_table.php @@ -26,7 +26,6 @@ return new class extends Migration $table->double('total_harga'); // gross amount $table->double('total_bayar'); $table->string('signature_key')->nullable(); - $table->string('reference_id')->nullable(); $table->string('token')->nullable(); $table->string('metode_pembayaran')->nullable(); $table->string('fraud_status')->nullable(); diff --git a/resources/views/User/transaction/Pembeli/index.blade.php b/resources/views/User/transaction/Pembeli/index.blade.php index 2014cc2..d71fb41 100644 --- a/resources/views/User/transaction/Pembeli/index.blade.php +++ b/resources/views/User/transaction/Pembeli/index.blade.php @@ -140,162 +140,129 @@ const token = $(this).data('token'); const csrfToken = $('meta[name="csrf-token"]').attr('content'); - // snap.pay(token, { - // onSuccess: function(result) { - // // $.ajaxSetup({ - // // headers: { - // // 'X-CSRF-TOKEN': csrfToken - // // } - // // }); - - // // $.ajax({ - // // url: "{{ route('user-pembeli.pay', ':id') }}" - // // .replace(':id', id), - // // type: "POST", - // // contentType: false, - // // processData: false, - // // success: function(response) { - // // Swal.fire({ - // // title: response.status ? 'Berhasil' : - // // 'Gagal', - // // text: response.message, - // // icon: response.status ? 'success' : - // // 'error', - // // confirmButtonText: 'OK' - // // }).then(function() { - // // location.reload(); - // // }); - // // console.log(response); - // // }, - // // error: function(error) { - // // Swal.fire({ - // // title: 'Gagal', - // // text: 'Gagal mengupdate pembayaran ke database', - // // icon: 'error' - // // }); - // // console.log(result); - // // } - // // }); - // console.log(result); - // }, - // onPending: function(result) { - // // $.ajaxSetup({ - // // headers: { - // // 'X-CSRF-TOKEN': csrfToken - // // } - // // }); - - // // $.ajax({ - // // url: "{{ route('user-pembeli.pay', ':id') }}" - // // .replace(':id', id), - // // type: "POST", - // // contentType: false, - // // processData: false, - // // success: function(response) { - // // Swal.fire({ - // // title: 'Berhasil', - // // text: response.message, - // // icon: 'info', - // // confirmButtonText: 'OK' - // // }); - // // console.log(response); - // // }, - // // error: function(error) { - // // Swal.fire({ - // // title: 'Gagal', - // // text: 'Gagal mengupdate pembayaran ke database', - // // icon: 'error' - // // }); - // // console.log(error); - // // } - // // }); - // console.log(result); - // }, - // onError: function(result) { - // // $.ajaxSetup({ - // // headers: { - // // 'X-CSRF-TOKEN': csrfToken - // // } - // // }); - - // // $.ajax({ - // // url: "{{ route('user-pembeli.pay', ':id') }}" - // // .replace(':id', id), - // // type: "POST", - // // contentType: false, - // // processData: false, - // // success: function(response) { - // // Swal.fire({ - // // title: 'Gagal', - // // text: response.message, - // // icon: 'error', - // // }); - // // console.log(response); - // // }, - // // error: function(error) { - // // Swal.fire({ - // // title: 'Gagal', - // // text: 'Gagal mengupdate pembayaran ke database', - // // icon: 'error' - // // }); - // // console.log(error); - // // } - // // }); - // // Swal.fire({ - // // title: 'Gagal', - // // text: 'Terjadi kesalahan karena ' + result, - // // icon: 'error' - // // }); - // console.log(result); - // }, - // onClose: function(error) { - // Swal.fire({ - // title: 'Ditunda', - // text: 'Kamu menutup halaman pembayaran. Silahkan lakukan pembayaran nanti', - // icon: 'info' - // }); - // console.log(error); - // } - // }); - - Swal.fire({ - html: '

Form Anda sedang diproses!

Mohon tunggu...

', - allowEscapeKey: false, - allowOutsideClick: false, - didOpen: () => { - Swal.showLoading(); - } - }); - - $.ajaxSetup({ - headers: { - 'X-CSRF-TOKEN': csrfToken - } - }); - - $.ajax({ - url: "{{ route('user-pembeli.pay') }}", - type: 'PUT', - data: { - id: id, - }, - success: function(response) { + snap.pay(token, { + onSuccess: function(result) { Swal.fire({ - title: response.status ? 'Berhasil' : 'Gagal', - text: response.message, - icon: response.status ? 'success' : 'error' - }).then(function() { - if (response.status) { - location.reload(); + html: '

Form Anda sedang diproses!

Mohon tunggu...

', + allowEscapeKey: false, + allowOutsideClick: false, + didOpen: () => { + Swal.showLoading(); } }); - console.log(response); + + $.ajaxSetup({ + headers: { + 'X-CSRF-TOKEN': csrfToken + } + }); + + $.ajax({ + url: "{{ route('user-pembeli.pay') }}", + type: 'PUT', + data: { + id: id, + }, + success: function(response) { + Swal.fire({ + title: response.status ? 'Berhasil' : + 'Gagal', + text: response.message, + icon: response.status ? 'success' : + 'error' + }).then(function() { + if (response.status) { + location.reload(); + } + }); + console.log(response); + }, + error: function(error) { + Swal.fire({ + title: 'Gagal', + text: 'Pembayaran gagal', + icon: 'error' + }); + console.log(error); + } + }); + console.log(result); }, - error: function(error) { + onPending: function(result) { + // $.ajaxSetup({ + // headers: { + // 'X-CSRF-TOKEN': csrfToken + // } + // }); + + // $.ajax({ + // url: "{{ route('user-pembeli.pay', ':id') }}" + // .replace(':id', id), + // type: "POST", + // contentType: false, + // processData: false, + // success: function(response) { + // Swal.fire({ + // title: 'Berhasil', + // text: response.message, + // icon: 'info', + // confirmButtonText: 'OK' + // }); + // console.log(response); + // }, + // error: function(error) { + // Swal.fire({ + // title: 'Gagal', + // text: 'Gagal mengupdate pembayaran ke database', + // icon: 'error' + // }); + // console.log(error); + // } + // }); + console.log(result); + }, + onError: function(result) { + // $.ajaxSetup({ + // headers: { + // 'X-CSRF-TOKEN': csrfToken + // } + // }); + + // $.ajax({ + // url: "{{ route('user-pembeli.pay', ':id') }}" + // .replace(':id', id), + // type: "POST", + // contentType: false, + // processData: false, + // success: function(response) { + // Swal.fire({ + // title: 'Gagal', + // text: response.message, + // icon: 'error', + // }); + // console.log(response); + // }, + // error: function(error) { + // Swal.fire({ + // title: 'Gagal', + // text: 'Gagal mengupdate pembayaran ke database', + // icon: 'error' + // }); + // console.log(error); + // } + // }); + // Swal.fire({ + // title: 'Gagal', + // text: 'Terjadi kesalahan karena ' + result, + // icon: 'error' + // }); + console.log(result); + }, + onClose: function(error) { Swal.fire({ - title: 'Gagal', - text: 'Pembayaran gagal', - icon: 'error' + title: 'Ditunda', + text: 'Kamu menutup halaman pembayaran. Silahkan lakukan pembayaran nanti', + icon: 'info' }); console.log(error); } @@ -332,7 +299,7 @@ }); $.ajax({ - url: "{{ route('user-pembeli.cancel', ':id') }}", + url: "{{ route('user-pembeli.cancel') }}", type: "PUT", data: { id: id