def query_transactions(): logger.info("tasks.query_transactions: Starting Execution") africastalking_username = os.environ['AFRICASTALKING_USERNAME'] africastalking_api_key = os.environ['AFRICASTALKING_API_KEY'] # Initialize the SDK africastalking.initialize(africastalking_username, africastalking_api_key) payment = africastalking.Payment # Set the name of your Africa's Talking payment product africastalking_product_name = os.environ['AFRICASTALKING_PRODUCT_NAME'] filters = { "category": "MobileC2B", "status": "Success", } response = None # Fetch the product transactions try: response = payment.product_transactions(africastalking_product_name, filters) logger.info("tasks.query_transactions: Response: {}".format( response['responses'])) return response['responses'] except Exception as e: logger.error("tasks.query_transactions: Received error response:%s" % str(e))
def send_message(sender, instance, **kwargs): logger.info('signals.send_message: Sending product to {}'.format( instance.email)) customer_data = {'Name': instance.name, 'email': instance.email} sendpulse_url = os.environ['SENDPULSE_URL'] requests.post(sendpulse_url, data=customer_data)
def index(request): assert isinstance(request, HttpRequest) logger.info('views.index') transactioncode_form = TransactionCodeForm(request.POST or None) if transactioncode_form.is_valid(): logger.info('views.index: Transaction code is valid') transaction_code = transactioncode_form.cleaned_data.get( 'transaction_code') user_has_paid = validate_transaction(transaction_code) if user_has_paid: logger.info('views.index: User payment was successful') request.session['transaction_code'] = transaction_code return redirect(details) else: logger.info('views.index: That transaction code does not exist') transactioncode_form.add_error( 'transaction_code', 'Sorry, that transaction code is incorrect. Check your confirmation message and try again' # noqa ) return render(request, 'index.html', { 'title': 'Get Your Ebook', 'form': transactioncode_form })
def success(request): assert isinstance(request, HttpRequest) logger.info('views.success') if 'email' not in request.session: logger.info( 'views.success: No email found. Redirecting to details view' ) # noqa return redirect(details) email = request.session['email'] return render(request, 'success.html', { 'title': 'Success!', 'email': email })
def validate_transaction(transaction_code): logger.info('tasks.validate_transaction: Start execution') successful_payment_status = False transactions = query_transactions() for payment in transactions: if payment['providerRefId'] == transaction_code: amount_paid = payment[ 'value'] # payments['value'] is in string format """Change string to float. Example: 'KES 100.0' to 100.0""" amount_paid = float(amount_paid.lstrip('KES ')) product_price = float(os.environ['PRODUCT_PRICE']) if amount_paid >= product_price: successful_payment_status = True return successful_payment_status
def details(request): assert isinstance(request, HttpRequest) logger.info('views.details') if 'transaction_code' not in request.session: logger.info( 'views.details: No transaction_code found. Redirecting to index view' ) # noqa return redirect(index) details_form = DetailsForm(request.POST or None) if details_form.is_valid(): logger.info('views.details: User details are valid') name = details_form.cleaned_data.get('name') email = details_form.cleaned_data.get('email') transaction_code = request.session['transaction_code'] """Save email to session""" request.session['email'] = email Customers.objects.create(name=name, email=email, transaction_code=transaction_code) return redirect(success) return render(request, 'details.html', { 'title': 'Almost done!', 'form': details_form })