def process_order(request): date = datetime.now() if not request.user.is_authenticated: return render(request, 'myapp/login_error.html') else: print request.user tracking_id = request.GET.get('pesapal_transaction_tracking_id', '') reference = request.GET.get('pesapal_merchant_reference', '') errors = '' msg = '' if tracking_id and reference: params = { 'pesapal_merchant_reference': reference, 'pesapal_transaction_tracking_id': tracking_id } client = pesapal.PesaPal(consumer_key, consumer_secret, True) pesapal_request = client.queryPaymentStatus(params) url = pesapal_request.to_url() #print url pesapal_response = requests.get(url) pesapal_response_data = pesapal_response.text #print pesapal_response_data pesapal_status = pesapal_response_data.split('=')[1] #email = request.user #email = email.email confirmation_code = random.randint(1, 1000) if pesapal_status == 'COMPLETED': response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'inline; filename="barcode.pdf"' buffer = BytesIO() p = canvas.Canvas(buffer) p.setLineWidth(.3) p.setFont('Helvetica', 12) p.drawString(30, 750, 'KaribuPay Ticket') p.drawString(30, 735, 'Event Ticket') p.drawString(350, 750, str(date)) barcode = code39.Extended39(str(confirmation_code), barWidth=0.5 * mm, barHeight=20 * mm) barcode.drawOn(p, 30, 600) p.showPage() p.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) print confirmation_code msg = 'Transaction was successful' print 'Transaction was successful' else: msg = 'Transaction status is %s' % (pesapal_status) print 'Transaction status is %s' % (pesapal_status) p_ref = Pesapal(tracking_id=tracking_id, reference=reference, status=pesapal_status) #p_ref.save() else: errors = 'Please Try again' return response
def process_checkout(request): if request.method == "POST": cart = Cart(request) delivery_fee = request.GET.get('delivery_fee') first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') email = request.POST.get('email') address1 = request.POST.get('add1') address2 = request.POST.get('add2') phone1 = request.POST.get('phone1') phone2 = request.POST.get('phone2') billing_details = BillingDetails(first_name=first_name, last_name=last_name, email=email, phone_number=phone1, phone_number2=phone2, address_line1=address1, address_line2=address2) billing_details.save() cart.billing_details = billing_details client = pesapal.PesaPal("Au93fiwr5A/NhPZqesxbjVNDqzFBdMI+", "d00fVQICYG8f/3kxueNRKQkfXnk=", False) if delivery_fee: total_cost = cart.summary() + 5000 else: total_cost = cart.summary() request_data = { 'FirstName': first_name, 'LastName': last_name, 'Email': email, 'Amount': str(total_cost), 'Description': 'Buying food from Brown foods', 'Type': 'MERCHANT', 'Reference': str(datetime.datetime.now()), 'Currency': 'UGX', } post_params = {'oauth_callback': 'http://brown.co.ug/process-order'} pesapal_request = client.postDirectOrder(post_params, request_data) return render(request, 'core/payment.html', { 'iframe_url': pesapal_request.to_url(), })
def request_payment(request): context = RequestContext(request) if request.method == 'POST': amount = request.POST['amount'] client = pesapal.PesaPal(consumer_key, consumer_secret, True) request_data = { 'Amount': str(1000), 'Description': 'Purchase of tickets from karibupay', 'Type': 'MERCHANT', 'Reference': str(datetime.now()), 'Email': '*****@*****.**' } post_params = { 'oauth_callback': 'http://f79cbce8.ngrok.io/process_orders' } pesapal_request = client.postDirectOrder(post_params, request_data) return render_to_response('pay.html', {'iframe_url': pesapal_request.to_url()}, context_instance=context)
def process_order(request): """ Handle the callback from pesapal """ tracking_id = request.GET.get('pesapal_transaction_tracking_id', '') reference = request.GET.get('pesapal_merchant_reference', '') #save this data to a model errors = '' msg = '' if tracking_id and reference: params = { 'pesapal_merchant_reference': reference, 'pesapal_transaction_tracking_id': tracking_id } client = pesapal.PesaPal('uvzyNdMvjn6Ir4id+zwcUNT7bKOsp+wY', 'fXFK6owbt2B00Yq6JscpvKmDm6o=', True) pesapal_request = client.queryPaymentStatus(params) url = pesapal_request.to_url() print url pesapal_response = requests.get(url) pesapal_response_data = pesapal_response.text print pesapal_response_data pesapal_status = pesapal_response_data.split('=')[1] if pesapal_status == 'COMPLETED': msg = 'Transaction was successful' else: msg = 'Transaction status is %s' % (pesapal_status) p_ref = Pesapal(tracking_id=tracking_id, reference=reference, status=pesapal_status) p_ref.save() else: errors = 'You broke our servers :-(' return render_to_response('process-order.html', { 'errors': errors, 'msg': msg }, context_instance=RequestContext(request))
def process_order(request): ''' Handle the callback from PesaPal ''' cart = Cart(request) tracking_id = request.GET.get('pesapal_transaction_tracking_id', '') reference = request.GET.get('pesapal_merchant_reference', '') if tracking_id and reference: params = { 'pesapal_merchant_reference': reference, 'pesapal_transaction_tracking_id': tracking_id } client = pesapal.PesaPal("Au93fiwr5A/NhPZqesxbjVNDqzFBdMI+", "d00fVQICYG8f/3kxueNRKQkfXnk=", False) pesapal_request = client.queryPaymentStatus(params) url = pesapal_request.to_url() print url pesapal_response = requests.get(url) pesapal_response_data = pesapal_response.text pesapal_status = pesapal_response_data.split("=")[1] if pesapal_status == 'COMPLETED': state = "Transaction was successful" cart.checked_out = True cart.save() else: state = "Transaction is %s" % pesapal_status p_ref = PesaPal(tracking_id=tracking_id, reference=reference, status=pesapal_status, cart=cart) p_ref.save() return render(request, 'core/process-order.html', { 'state': state, })
def checkout(request): """ """ delivery_fee = 0 if request.method == 'POST': product_id = request.POST.get('product_id', '') temp_cost = request.POST.get('cost', 0) cost = int(temp_cost.split('.')[0]) delivery_option = request.POST.get('delivery', '') if delivery_option == 'd': delivery_fee = 300 total_cost = cost + delivery_fee else: total_cost = cost client = pesapal.PesaPal('uvzyNdMvjn6Ir4id+zwcUNT7bKOsp+wY', 'fXFK6owbt2B00Yq6JscpvKmDm6o=', True) request_data = { 'Amount': str(total_cost), 'Description': 'Purchase of jewelry from shopsoko.com', 'Type': 'MERCHANT', 'Reference': str(datetime.datetime.now()), 'Email': '*****@*****.**' } post_params = { 'oauth_callback': 'http://staging.shopsoko.com/pesapal/process-order' } pesapal_request = client.postDirectOrder(post_params, request_data) return render_to_response('checkout.html', { 'total_cost': total_cost, 'cost': cost, 'delivery_fee': delivery_fee, 'iframe_url': pesapal_request.to_url() }, context_instance=RequestContext(request))
def setUp(self): self.client = pesapal.PesaPal(KEY, SECRET)