<s> import mock <EOL> import datetime <EOL> import decimal <EOL> from bitcoinrpc . data import TransactionInfo <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . http import HttpResponseRedirect <EOL> from billing import CreditCard , get_gateway , get_integration <EOL> from billing . gateway import CardNotSupported <EOL> from app . forms import CreditCardForm <EOL> from app . urls import ( authorize_net_obj , world_pay_obj , pay_pal_obj , <EOL> amazon_fps_obj , fps_recur_obj , braintree_obj , <EOL> stripe_obj , ogone_obj ) <EOL> from app . utils import randomword <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import RequestSite <EOL> from billing . utils . paylane import PaylanePaymentCustomer , PaylanePaymentCustomerAddress <EOL> from app . conf import GATEWAY_INITIAL , INTEGRATION_INITIAL <EOL> BTC_TEST_AMOUNT = decimal . Decimal ( '<STR_LIT>' ) <EOL> BTC_TEST_ADDRESS = '<STR_LIT>' <EOL> BTC_TEST_SUCCESSFUL_TXNS = [ TransactionInfo ( address = BTC_TEST_ADDRESS , amount = BTC_TEST_AMOUNT ) ] <EOL> def render ( request , template , template_vars = { } ) : <EOL> return render_to_response ( template , template_vars , RequestContext ( request ) ) <EOL> def index ( request , gateway = None ) : <EOL> return authorize ( request ) <EOL> def authorize ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> try : <EOL> merchant . validate_card ( credit_card ) <EOL> except CardNotSupported : <EOL> response = "<STR_LIT>" <EOL> response = merchant . purchase ( amount , credit_card ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def paypal ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> try : <EOL> merchant . validate_card ( credit_card ) <EOL> except CardNotSupported : <EOL> response = "<STR_LIT>" <EOL> response = merchant . recurring ( amount , credit_card , options = { '<STR_LIT>' : request } ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def eway ( request ) : <EOL> amount = <NUM_LIT:100> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> try : <EOL> merchant . validate_card ( credit_card ) <EOL> except CardNotSupported : <EOL> response = "<STR_LIT>" <EOL> billing_address = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> } <EOL> response = merchant . purchase ( amount , credit_card , options = { '<STR_LIT>' : request , '<STR_LIT>' : billing_address } ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def braintree ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> try : <EOL> merchant . validate_card ( credit_card ) <EOL> except CardNotSupported : <EOL> response = "<STR_LIT>" <EOL> response = merchant . purchase ( amount , credit_card ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def stripe ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> response = merchant . purchase ( amount , credit_card ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def paylane ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> customer = PaylanePaymentCustomer ( ) <EOL> customer . name = "<STR_LIT>" % ( data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ) <EOL> customer . email = "<STR_LIT>" <EOL> customer . ip_address = "<STR_LIT:127.0.0.1>" <EOL> options = { } <EOL> address = PaylanePaymentCustomerAddress ( ) <EOL> address . street_house = '<STR_LIT>' <EOL> address . city = '<STR_LIT>' <EOL> address . zip_code = '<STR_LIT>' <EOL> address . country_code = '<STR_LIT>' <EOL> customer . address = address <EOL> options [ '<STR_LIT>' ] = customer <EOL> options [ '<STR_LIT>' ] = { } <EOL> response = merchant . purchase ( amount , credit_card , options = options ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def we_pay ( request ) : <EOL> wp = get_gateway ( "<STR_LIT>" ) <EOL> form = None <EOL> amount = <NUM_LIT:10> <EOL> response = wp . purchase ( <NUM_LIT:10> , None , { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : request . build_absolute_uri ( reverse ( '<STR_LIT>' ) ) <EOL> } ) <EOL> if response [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> return HttpResponseRedirect ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def we_pay_redirect ( request ) : <EOL> checkout_id = request . GET . get ( "<STR_LIT>" , None ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : checkout_id } ) <EOL> def we_pay_ipn ( request ) : <EOL> return render ( request , '<STR_LIT>' , { } ) <EOL> def beanstream ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> response = merchant . purchase ( amount , credit_card , <EOL> { "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" % ( data [ "<STR_LIT>" ] , data [ "<STR_LIT>" ] ) , <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def chargebee ( request ) : <EOL> amount = <NUM_LIT:1> <EOL> response = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = CreditCardForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = form . cleaned_data <EOL> credit_card = CreditCard ( ** data ) <EOL> merchant = get_gateway ( "<STR_LIT>" ) <EOL> response = merchant . purchase ( amount , credit_card , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" } ) <EOL> else : <EOL> form = CreditCardForm ( initial = GATEWAY_INITIAL [ '<STR_LIT>' ] ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : amount , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT:title>' : '<STR_LIT>' } ) <EOL> def offsite_authorize_net ( request ) : <EOL> params = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : datetime . datetime . now ( ) . strftime ( '<STR_LIT:%s>' ) , <EOL> '<STR_LIT>' : '<STR_LIT:F>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> authorize_net_obj . add_fields ( params ) <EOL> template_vars = { "<STR_LIT>" : authorize_net_obj , '<STR_LIT:title>' : authorize_net_obj . display_name } <EOL> return render ( request , '<STR_LIT>' , template_vars ) <EOL> def offsite_paypal ( request ) : <EOL> invoice_id = datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> return_url = request . build_absolute_uri ( reverse ( '<STR_LIT>' ) ) <EOL> cancel_return = request . build_absolute_uri ( request . META [ '<STR_LIT>' ] ) <EOL> notify_url = request . build_absolute_uri ( reverse ( '<STR_LIT>' ) ) <EOL> paypal_params = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : invoice_id , <EOL> '<STR_LIT>' : notify_url , <EOL> '<STR_LIT>' : return_url , <EOL> '<STR_LIT>' : cancel_return , <EOL> } <EOL> pay_pal_obj . add_fields ( paypal_params ) <EOL> template_vars = { "<STR_LIT>" : pay_pal_obj , '<STR_LIT:title>' : '<STR_LIT>' } <EOL> return render ( request , '<STR_LIT>' , template_vars ) <EOL> def offsite_world_pay ( request ) : <EOL> fields = { "<STR_LIT>" : settings . MERCHANT_SETTINGS [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , } <EOL> world_pay_obj . add_fields ( fields ) <EOL> template_vars = { '<STR_LIT:title>' : '<STR_LIT>' , "<STR_LIT>" : world_pay_obj } <EOL> return render ( request , '<STR_LIT>' , template_vars ) <EOL> def offsite_amazon_fps ( request ) : <EOL> url_scheme = "<STR_LIT:http>" <EOL> if request . is_secure ( ) : <EOL> url_scheme = "<STR_LIT>" <EOL> fields = { "<STR_LIT>" : "<STR_LIT:100>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : request . build_absolute_uri ( ) , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( url_scheme , <EOL> RequestSite ( request ) . domain , <EOL> reverse ( "<STR_LIT>" ) ) <EOL> } <EOL> amazon_fps_obj . add_fields ( fields ) <EOL> fields . update ( { "<STR_LIT>" : "<STR_LIT:100>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> fps_recur_obj . add_fields ( fields ) <EOL> template_vars = { '<STR_LIT:title>' : '<STR_LIT>' , <EOL> "<STR_LIT>" : fps_recur_obj , <EOL> "<STR_LIT>" : amazon_fps_obj } <EOL> return render ( request , '<STR_LIT>' , template_vars ) <EOL> def offsite_braintree ( request ) : <EOL> fields = { "<STR_LIT>" : { <EOL> "<STR_LIT>" : datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : True <EOL> } , <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( "<STR_LIT>" if request . is_secure ( ) else "<STR_LIT:http>" , <EOL> RequestSite ( request ) . domain ) <EOL> } <EOL> braintree_obj . add_fields ( fields ) <EOL> template_vars = { '<STR_LIT:title>' : '<STR_LIT>' , <EOL> "<STR_LIT>" : braintree_obj } <EOL> return render ( request , "<STR_LIT>" , template_vars ) <EOL> def offsite_stripe ( request ) : <EOL> status = request . GET . get ( "<STR_LIT:status>" ) <EOL> stripe_obj . add_field ( "<STR_LIT>" , <NUM_LIT:100> ) <EOL> template_vars = { '<STR_LIT:title>' : '<STR_LIT>' , <EOL> "<STR_LIT>" : stripe_obj , <EOL> "<STR_LIT:status>" : status } <EOL> return render ( request , "<STR_LIT>" , template_vars ) <EOL> def offsite_eway ( request ) : <EOL> return_url = request . build_absolute_uri ( reverse ( offsite_eway_done ) ) <EOL> eway_obj = get_integration ( "<STR_LIT>" ) <EOL> customer = eway_obj . request_access_code ( <EOL> return_url = return_url , customer = { } , <EOL> payment = { "<STR_LIT>" : <NUM_LIT:100> } ) <EOL> request . session [ "<STR_LIT>" ] = eway_obj . access_code <EOL> template_vars = { "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : eway_obj } <EOL> return render ( request , "<STR_LIT>" , template_vars ) <EOL> def offsite_eway_done ( request ) : <EOL> access_code = request . session [ "<STR_LIT>" ] <EOL> eway_obj = get_integration ( "<STR_LIT>" , access_code = access_code ) <EOL> result = eway_obj . check_transaction ( ) <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT:result>" : result } ) <EOL> def bitcoin ( request ) : <EOL> with mock . patch ( '<STR_LIT>' ) as MockBitcoinConnection : <EOL> connection = MockBitcoinConnection ( ) <EOL> connection . getnewaddress . return_value = BTC_TEST_ADDRESS <EOL> connection . listtransactions . return_value = BTC_TEST_SUCCESSFUL_TXNS <EOL> amount = <NUM_LIT> <EOL> bitcoin_obj = get_gateway ( "<STR_LIT>" ) <EOL> address = request . session . get ( "<STR_LIT>" , None ) <EOL> if not address : <EOL> address = bitcoin_obj . get_new_address ( ) <EOL> request . session [ "<STR_LIT>" ] = address <EOL> return render ( request , "<STR_LIT>" , { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : amount , <EOL> "<STR_LIT:address>" : address , <EOL> "<STR_LIT>" : settings <EOL> } ) <EOL> def bitcoin_done ( request ) : <EOL> with mock . patch ( '<STR_LIT>' ) as MockBitcoinConnection : <EOL> connection = MockBitcoinConnection ( ) <EOL> connection . getnewaddress . return_value = BTC_TEST_ADDRESS <EOL> connection . listtransactions . return_value = BTC_TEST_SUCCESSFUL_TXNS <EOL> amount = <NUM_LIT> <EOL> bitcoin_obj = get_gateway ( "<STR_LIT>" ) <EOL> address = request . session . get ( "<STR_LIT>" , None ) <EOL> if not address : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> result = bitcoin_obj . purchase ( amount , address ) <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> del request . session [ "<STR_LIT>" ] <EOL> return render ( request , "<STR_LIT>" , { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : amount , <EOL> "<STR_LIT:address>" : address , <EOL> "<STR_LIT:result>" : result <EOL> } ) <EOL> def offsite_ogone ( request ) : <EOL> fields = { <EOL> '<STR_LIT>' : randomword ( <NUM_LIT:6> ) , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . build_absolute_uri ( reverse ( "<STR_LIT>" ) ) , <EOL> '<STR_LIT>' : request . build_absolute_uri ( reverse ( "<STR_LIT>" ) ) , <EOL> '<STR_LIT>' : request . build_absolute_uri ( reverse ( "<STR_LIT>" ) ) , <EOL> '<STR_LIT>' : request . build_absolute_uri ( reverse ( "<STR_LIT>" ) ) , <EOL> } <EOL> ogone_obj . add_fields ( fields ) <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT>" : ogone_obj } ) </s>
