def post(self): # initialize api client object api_client = midtransclient.CoreApi( is_production=False, server_key='SB-Mid-server-1tkGPlpmQKBQ8HC-iax-nI_U', client_key='SB-Mid-client-c-k-uXK4AgXKTYd2') req_data = request.get_json() status_response = api_client.transactions.notification(req_data) order_id = status_response['order_id'] transaction_status = status_response['transaction_status'] fraud_status = status_response['fraud_status'] status_code = status_response['status_code'] laporan = Laporan.query.get(order_id) if status_code == "200": laporan.status_pembayaran = True db.session.commit() objek_pajak = ObjekPajak.query.get(laporan.objek_pajak_id) payer = Payer.query.get(objek_pajak.payer_id) nomor_sspd = objek_pajak.nopd + laporan.nomor_skpd bukti_pembayaran = BuktiPembayaran(laporan.id, payer.daerah_id, nomor_sspd, objek_pajak.jumlah) db.session.add(bukti_pembayaran) db.session.commit() return {'message': 'status pembayaran diterima'}, 200 elif transaction_status == "expire": laporan.pembatalan_laporan = True db.session.commit() return {'message': 'status pembayaran diterima'}, 200
def notification(): try: core = midtransclient.CoreApi( is_production=False, server_key='SB-Mid-server-UKoo4wuXPkpkoOx8dFWla_BS', client_key='SB-Mid-client-BfoyzFWFCAh_9V25') request_json = request.get_json() transaction_status_dict = core.transactions.notification(request_json) order_id = request_json['order_id'] trans = Transaction.query.filter_by(id=order_id).first() CheckTransactionStatus(trans) except: pass return jsonify("NOPE")
def post(self): # initialize api client object api_client = midtransclient.CoreApi(is_production=STATE, server_key=username, client_key=client_key) req_data = request.get_json() mock_notification = { "transaction_time": "2020-02-04 22:03:44", "gross_amount": "10950.00", "currency": "IDR", "order_id": "TUKULSA-TUKULSAORDER2-17", "payment_type": "gopay", "signature_key": "8f83c0dce379431bfdda36f038f5c39383e74ae948207c55b9a29c4ea4e41d74bc393a6628f89d5d4530c5798378625d9850cf6f5ce16107686793eded349c84", "status_code": "200", "transaction_id": "9bd2c6e3-f1de-4980-b1b5-8ccf9eae2b61", "transaction_status": "settlement", "fraud_status": "accept", "settlement_time": "2020-02-04 22:03:46", "status_message": "Success, transaction is found", "merchant_id": "G018994973" } # handle notification JSON sent by Midtrans, it auto verify it by doing get status # parameter can be Dictionary or String of JSON status_response = api_client.transactions.notification(req_data) order_id = status_response['order_id'] transaction_status = status_response['transaction_status'] fraud_status = status_response['fraud_status'] status_code = status_response['status_code'] print( 'Transaction notification received. Order ID: {0}. Transaction status: {1}. Fraud status: {2}' .format(order_id, transaction_status, fraud_status)) # Format ORDERID from midtrans to match with transactions table formatted_orderID = order_id.replace('TUKULSA-', '') # Query table transactions selected_trx = Transactions.query.filter_by( order_id=formatted_orderID).first() pulsa_code = Product.query.filter_by( id=selected_trx.product_id).first().code # Sample transaction_status handling logic if status_code == '200': # Ubah payment status di transaksi jadi PAID selected_trx.payment_status = 'LUNAS' selected_trx.order_status = 'PROSES' db.session.commit() # Nembak mobile pulsa buying_pulsa(order_id, selected_trx.phone_number, pulsa_code) print('LUNAS') elif status_code == '201': # Ubah payment status di transaksi jadi PENDING selected_trx.payment_status = 'TERTUNDA' db.session.commit() print('TERTUNDA') elif status_code == '202': # Ubah payment status di transaksi jadi DENIED selected_trx.payment_status = 'DITOLAK' db.session.commit() print('DITOLAK') elif status_code == '407': # Ubah payment status di transaksi jadi EXPIRED selected_trx.payment_status = 'KADALUWARSA' db.session.commit() print('KADALUWARSA') else: print('ERROR') return 200, {'Content-Type': 'application/json'}
import midtransclient # initialize core api client object core = midtransclient.CoreApi(is_production=False, server_key='YOUR_SERVER_KEY', client_key='YOUR_CLIENT_KEY') # Alternative way to initialize CoreApi client object: # core = midtransclient.CoreApi() # core.api_config.set( # is_production=False, # server_key='YOUR_SERVER_KEY', # client_key='YOUR_CLIENT_KEY' # ) # Another alternative way to initialize CoreApi client object: # core = midtransclient.CoreApi() # core.api_config.is_production=False # core.api_config.server_key='YOUR_SERVER_KEY' # core.api_config.client_key='YOUR_CLIENT_KEY' # IMPORTANT NOTE: You should do credit card get token via frontend using `midtrans.min.js`, to avoid card data breach risks on your backend # ( refer to: https://api-docs.midtrans.com ) # prepare CORE API parameter to get credit card token params = { 'card_number': '5264 2210 3887 4659', 'card_exp_month': '12', 'card_exp_year': '2025', 'card_cvv': '123', 'client_key': core.api_config.client_key, }
def room_book(id): try: selected_date = session['date'] except: return redirect(url_for('rooms.room', id=id)) room = Room.query.filter_by(id=id).first_or_404() form = BookForm() price = room.price * len(selected_date) price_formatted = f'Rp {price:,}'.replace(",", ".") + ',00' if (form.validate_on_submit()): name = form.name.data email = form.email.data phone = form.number.data event = form.event_name.data organizer = form.event_organizer.data payment = form.payment.data print(payment) try: transID = "tr-" + str(uuid4().hex)[:16] trans = Transaction(id=transID, price=price, user_id=current_user.id, room_id=room.id, status="pending") db.session.add(trans) for date in selected_date: dateobj = datetime.strptime(date, "%Y/%m/%d").date() isBooked = Booked.query.filter_by(room_booked=room, date=dateobj).first() if (isBooked): flash( f"Ruangan {room.name} sudah terbooking pada tanggal {dateobj}", "danger") return redirect(url_for('rooms.room', id=id)) booked = Booked(booked_by=current_user, room_booked=room, date=dateobj, event=event, organization=organizer, name=name, email=email, phone=phone, transaction_id=transID) print(booked.id, booked.transaction_id) db.session.add(booked) core_api = midtransclient.CoreApi( is_production=False, server_key='SB-Mid-server-UKoo4wuXPkpkoOx8dFWla_BS', client_key='SB-Mid-client-BfoyzFWFCAh_9V25') param = PaymentParameters(payment, name, email, phone, price, transID) charge_response = core_api.charge(param) trans.data = json.dumps(charge_response) trans.payment_type = payment print(charge_response) db.session.commit() return redirect( url_for('rooms.room_book_transaction', id=id, transID=transID)) except Error as e: print(e) pass return render_template("fill-data.html", room=room, current_user=current_user, form=form, selected_date=selected_date, price=price_formatted)
import uuid import midtransclient from django.conf import settings from django.http import Http404 from django.shortcuts import render from payments.constants import DUMMY_PRODUCTS, PAYMENT_STATUS MIDTRANS_CORE = midtransclient.CoreApi( is_production=not settings.DEBUG, server_key=settings.MIDTRANS['SERVER_KEY'], client_key=settings.MIDTRANS['CLIENT_KEY'], ) MIDTRANS_SNAP = midtransclient.Snap( is_production=not settings.DEBUG, server_key=settings.MIDTRANS['SERVER_KEY'], client_key=settings.MIDTRANS['CLIENT_KEY'], ) def product_view(request): ctx = {'products': DUMMY_PRODUCTS} return render(request, 'index.html', ctx) def checkout_view(request, product_id): try: product = [x for x in DUMMY_PRODUCTS if x.get('id') == product_id][0]