#NEWLINE# def set_txn( basket , shipping_methods , currency , return_url , cancel_url , update_url = None , action = SALE , user = None , user_address = None , shipping_method = None , shipping_address = None , no_shipping = False , paypal_params = None ) : #NEWLINE# #INDENT# "\n    Register the transaction with PayPal to get a token which we use in the\n    redirect URL.  This is the 'SetExpressCheckout' from their documentation.\n\n    There are quite a few options that can be passed to PayPal to configure\n    this request - most are controlled by PAYPAL_* settings.\n    " #NEWLINE# _params = { #NEWLINE# #INDENT# 'CUSTOMERSERVICENUMBER' : getattr ( settings , 'PAYPAL_CUSTOMER_SERVICES_NUMBER' , None ) , #NEWLINE# 'SOLUTIONTYPE' : getattr ( settings , 'PAYPAL_SOLUTION_TYPE' , None ) , #NEWLINE# 'LANDINGPAGE' : getattr ( settings , 'PAYPAL_LANDING_PAGE' , None ) , #NEWLINE# 'BRANDNAME' : getattr ( settings , 'PAYPAL_BRAND_NAME' , None ) , #NEWLINE# 'PAGESTYLE' : getattr ( settings , 'PAYPAL_PAGESTYLE' , None ) , #NEWLINE# 'HDRIMG' : getattr ( settings , 'PAYPAL_HEADER_IMG' , None ) , #NEWLINE# 'PAYFLOWCOLOR' : getattr ( settings , 'PAYPAL_PAYFLOW_COLOR' , None ) , #NEWLINE# 'HDRBACKCOLOR' : getattr ( settings , 'PAYPAL_HEADER_BACK_COLOR' , None ) , #NEWLINE# 'HDRBORDERCOLOR' : getattr ( settings , 'PAYPAL_HEADER_BORDER_COLOR' , None ) , #NEWLINE# 'LOCALECODE' : getattr ( settings , 'PAYPAL_LOCALE' , None ) , #NEWLINE# 'ALLOWNOTE' : getattr ( settings , 'PAYPAL_ALLOW_NOTE' , True ) , #NEWLINE# 'CALLBACKTIMEOUT' : getattr ( settings , 'PAYPAL_CALLBACK_TIMEOUT' , 3 ) , #NEWLINE# #UNINDENT# } #NEWLINE# confirm_shipping_addr = getattr ( settings , 'PAYPAL_CONFIRM_SHIPPING' , None ) #NEWLINE# if ( confirm_shipping_addr and ( not no_shipping ) ) : #NEWLINE# #INDENT# _params [ 'REQCONFIRMSHIPPING' ] = 1 #NEWLINE# #UNINDENT# if paypal_params : #NEWLINE# #INDENT# _params . update ( paypal_params ) #NEWLINE# #UNINDENT# locale = _params . get ( 'LOCALECODE' , None ) #NEWLINE# if locale : #NEWLINE# #INDENT# valid_choices = ( 'AU' , 'DE' , 'FR' , 'GB' , 'IT' , 'ES' , 'JP' , 'US' ) #NEWLINE# if ( locale not in valid_choices ) : #NEWLINE# #INDENT# raise ImproperlyConfigured ( ( "'%s' is not a valid locale code" % locale ) ) #NEWLINE# #UNINDENT# #UNINDENT# _params . update ( ( ( k , int ( v ) ) for ( k , v ) in _params . items ( ) if isinstance ( v , bool ) ) ) #NEWLINE# params = dict ( ( ( k , v ) for ( k , v ) in _params . items ( ) if ( v is not None ) ) ) #NEWLINE# amount = basket . total_incl_tax #NEWLINE# if ( ( currency == 'USD' ) and ( amount > 10000 ) ) : #NEWLINE# #INDENT# msg = 'PayPal can only be used for orders up to 10000 USD' #NEWLINE# logger . error ( msg ) #NEWLINE# raise express_exceptions . InvalidBasket ( _ ( msg ) ) #NEWLINE# #UNINDENT# if ( amount <= 0 ) : #NEWLINE# #INDENT# msg = 'The basket total is zero so no payment is required' #NEWLINE# logger . error ( msg ) #NEWLINE# raise express_exceptions . InvalidBasket ( _ ( msg ) ) #NEWLINE# #UNINDENT# params . update ( { #NEWLINE# #INDENT# 'PAYMENTREQUEST_0_AMT' : amount , #NEWLINE# 'PAYMENTREQUEST_0_CURRENCYCODE' : currency , #NEWLINE# 'RETURNURL' : return_url , #NEWLINE# 'CANCELURL' : cancel_url , #NEWLINE# 'PAYMENTREQUEST_0_PAYMENTACTION' : action , #NEWLINE# #UNINDENT# } ) #NEWLINE# index = 0 #NEWLINE# for ( index , line ) in enumerate ( basket . all_lines ( ) ) : #NEWLINE# #INDENT# product = line . product #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_NAME%d' % index ) ] = product . get_title ( ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_NUMBER%d' % index ) ] = ( product . upc if product . upc else '' ) #NEWLINE# desc = '' #NEWLINE# if product . description : #NEWLINE# #INDENT# desc = _format_description ( product . description ) #NEWLINE# #UNINDENT# params [ ( 'L_PAYMENTREQUEST_0_DESC%d' % index ) ] = desc #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_AMT%d' % index ) ] = _format_currency ( line . unit_price_incl_tax ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_QTY%d' % index ) ] = line . quantity #NEWLINE# #UNINDENT# for discount in basket . offer_discounts : #NEWLINE# #INDENT# index += 1 #NEWLINE# name = ( _ ( 'Special Offer: %s' ) % discount [ 'name' ] ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_NAME%d' % index ) ] = name #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_DESC%d' % index ) ] = _format_description ( name ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_AMT%d' % index ) ] = _format_currency ( ( - discount [ 'discount' ] ) ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_QTY%d' % index ) ] = 1 #NEWLINE# #UNINDENT# for discount in basket . voucher_discounts : #NEWLINE# #INDENT# index += 1 #NEWLINE# name = ( '%s (%s)' % ( discount [ 'voucher' ] . name , discount [ 'voucher' ] . code ) ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_NAME%d' % index ) ] = name #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_DESC%d' % index ) ] = _format_description ( name ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_AMT%d' % index ) ] = _format_currency ( ( - discount [ 'discount' ] ) ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_QTY%d' % index ) ] = 1 #NEWLINE# #UNINDENT# for discount in basket . shipping_discounts : #NEWLINE# #INDENT# index += 1 #NEWLINE# name = ( _ ( 'Shipping Offer: %s' ) % discount [ 'name' ] ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_NAME%d' % index ) ] = name #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_DESC%d' % index ) ] = _format_description ( name ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_AMT%d' % index ) ] = _format_currency ( ( - discount [ 'discount' ] ) ) #NEWLINE# params [ ( 'L_PAYMENTREQUEST_0_QTY%d' % index ) ] = 1 #NEWLINE# #UNINDENT# params [ 'PAYMENTREQUEST_0_ITEMAMT' ] = _format_currency ( basket . total_incl_tax ) #NEWLINE# params [ 'PAYMENTREQUEST_0_TAXAMT' ] = _format_currency ( D ( '0.00' ) ) #NEWLINE# if update_url : #NEWLINE# #INDENT# params [ 'CALLBACK' ] = update_url #NEWLINE# #UNINDENT# if user : #NEWLINE# #INDENT# params [ 'EMAIL' ] = user . email #NEWLINE# #UNINDENT# if user_address : #NEWLINE# #INDENT# params [ 'SHIPTONAME' ] = user_address . name #NEWLINE# params [ 'SHIPTOSTREET' ] = user_address . line1 #NEWLINE# params [ 'SHIPTOSTREET2' ] = user_address . line2 #NEWLINE# params [ 'SHIPTOCITY' ] = user_address . line4 #NEWLINE# params [ 'SHIPTOSTATE' ] = user_address . state #NEWLINE# params [ 'SHIPTOZIP' ] = user_address . postcode #NEWLINE# params [ 'SHIPTOCOUNTRYCODE' ] = user_address . country . iso_3166_1_a2 #NEWLINE# #UNINDENT# if ( shipping_method and shipping_address ) : #NEWLINE# #INDENT# params [ 'ADDROVERRIDE' ] = 1 #NEWLINE# params [ 'REQCONFIRMSHIPPING' ] = 0 #NEWLINE# params [ 'SHIPTONAME' ] = shipping_address . name #NEWLINE# params [ 'SHIPTOSTREET' ] = shipping_address . line1 #NEWLINE# params [ 'SHIPTOSTREET2' ] = shipping_address . line2 #NEWLINE# params [ 'SHIPTOCITY' ] = shipping_address . line4 #NEWLINE# params [ 'SHIPTOSTATE' ] = shipping_address . state #NEWLINE# params [ 'SHIPTOZIP' ] = shipping_address . postcode #NEWLINE# params [ 'SHIPTOCOUNTRYCODE' ] = shipping_address . country . iso_3166_1_a2 #NEWLINE# if ( params [ 'SHIPTOCOUNTRYCODE' ] == 'US' ) : #NEWLINE# #INDENT# key = params [ 'SHIPTOSTATE' ] . lower ( ) . strip ( ) #NEWLINE# if ( key in us_states . STATES_NORMALIZED ) : #NEWLINE# #INDENT# params [ 'SHIPTOSTATE' ] = us_states . STATES_NORMALIZED [ key ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if no_shipping : #NEWLINE# #INDENT# params [ 'NOSHIPPING' ] = 1 #NEWLINE# #UNINDENT# #UNINDENT# params [ 'PAYMENTREQUEST_0_SHIPPINGAMT' ] = _format_currency ( D ( '0.00' ) ) #NEWLINE# max_charge = D ( '0.00' ) #NEWLINE# for ( index , method ) in enumerate ( shipping_methods ) : #NEWLINE# #INDENT# is_default = ( index == 0 ) #NEWLINE# params [ ( 'L_SHIPPINGOPTIONISDEFAULT%d' % index ) ] = ( 'true' if is_default else 'false' ) #NEWLINE# charge = method . calculate ( basket ) . incl_tax #NEWLINE# if ( charge > max_charge ) : #NEWLINE# #INDENT# max_charge = charge #NEWLINE# #UNINDENT# if is_default : #NEWLINE# #INDENT# params [ 'PAYMENTREQUEST_0_SHIPPINGAMT' ] = _format_currency ( charge ) #NEWLINE# params [ 'PAYMENTREQUEST_0_AMT' ] += charge #NEWLINE# #UNINDENT# params [ ( 'L_SHIPPINGOPTIONNAME%d' % index ) ] = six . text_type ( method . name ) #NEWLINE# params [ ( 'L_SHIPPINGOPTIONAMOUNT%d' % index ) ] = _format_currency ( charge ) #NEWLINE# #UNINDENT# if shipping_method : #NEWLINE# #INDENT# charge = shipping_method . calculate ( basket ) . incl_tax #NEWLINE# params [ 'PAYMENTREQUEST_0_SHIPPINGAMT' ] = _format_currency ( charge ) #NEWLINE# params [ 'PAYMENTREQUEST_0_AMT' ] += charge #NEWLINE# #UNINDENT# params [ 'PAYMENTREQUEST_0_MAXAMT' ] = _format_currency ( ( amount + max_charge ) ) #NEWLINE# params [ 'MAXAMT' ] = _format_currency ( ( amount + max_charge ) ) #NEWLINE# params [ 'PAYMENTREQUEST_0_HANDLINGAMT' ] = _format_currency ( D ( '0.00' ) ) #NEWLINE# params [ 'PAYMENTREQUEST_0_AMT' ] = _format_currency ( params [ 'PAYMENTREQUEST_0_AMT' ] ) #NEWLINE# txn = _fetch_response ( SET_EXPRESS_CHECKOUT , params ) #NEWLINE# if getattr ( settings , 'PAYPAL_SANDBOX_MODE' , True ) : #NEWLINE# #INDENT# url = 'https://www.sandbox.paypal.com/webscr' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# url = 'https://www.paypal.com/webscr' #NEWLINE# #UNINDENT# params = ( ( 'cmd' , '_express-checkout' ) , ( 'token' , txn . token ) ) #NEWLINE# return ( '%s?%s' % ( url , urlencode ( params ) ) )