Пример #1
0
def get_checkout_url(order_obj, paypal_cfg, shipping=0, notes=0):
    interface = PayPalInterface(PayPalConfig(**paypal_cfg.CONFIG))
    resp = interface.set_express_checkout(**{
        "returnurl": paypal_cfg.RETURN_URL,
        "cancelurl": paypal_cfg.CANCEL_URL,
        "paymentaction": "sale",
        "amt": "%.2f" % (order_obj.nett_price),
        "currencycode": paypal_cfg.CURRENCY_CODE,
        "desc": paypal_cfg.ORDER_DESCRIPTION,
        "useraction": "commit",
        "noshipping": shipping,
        "allownote": 0
    })
    token = resp.token
    redir_url = interface.generate_express_checkout_redirect_url(token)
    return redir_url
Пример #2
0
class PayPalGateway:
    """ Specific Impementation for PayPal WPP"""
    
    def __init__(self, app):
        # Need to catch value error and throw as config error
        try:
            self._init_API(app)
        except KeyError:
            raise PaymentsConfigurationError
            
    def _init_API(self ,app):
        """ initialises any stuff needed for the payment gateway API and should
        fail if anything is invalid or missing
        """
        config = PayPalConfig(
            API_ENVIRONMENT=app.config.get('PAYMENT_API_ENVIRONMENT', 'sandbox'),
            API_USERNAME=app.config.get('PAYPAL_API_USER'),
            API_PASSWORD=app.config.get('PAYPAL_API_PWD'),
            API_SIGNATURE=app.config.get('PAYPAL_API_SIGNATURE')
        )
        self.interface = PayPalInterface(config)

    def setupRedirect(self, trans):
        """ this is for WPP only"""
        if trans.type == 'Express':
            return self._setupExpressTransfer(trans)
        else:
            raise PaymentTransactionValidationError()

    # why is this two methods surely this could be easier?

    def _setupExpressTransfer(self, trans):
        """ add details to transaction to allow it to be forwarded to the 
        third party gateway 
        """
        def keycase(key):
            return key.replace('_','').upper()
        params = dict([(keycase(k), v,) for k, v in trans.__dict__.iteritems()])
        r = self.SetExpressCheckout(**params)
        trans.token = r.token
        trans.next = self.interface.generate_express_checkout_redirect_url(
                r.token)
        return trans

    # Public methods of gateway 'interface'
    def authorise(self, trans):
        """Examines the type of transaction passed in and delegates to either
        the express payments flow or the direct payments flow, where further
        validation can take place.

        If its not a type of transaction which this gateway can process then it
        will throw its dummy out of the pram.
        """
        if trans.type == 'Express':
            return self._authoriseExpress(trans)
        elif trans.type == 'Direct':
            pass # not implemented yet
        else: raise PaymentTransactionValidationError()

    def _authoriseExpress(self, trans, action='Sale'):
        """ calls authorise on payment setup via redirect to paypal
        """
        r = self.DoExpressCheckoutPayment(token=trans.token,
                PAYMENTACTION=action, PAYERID=trans.payerid, AMT=trans.amt,
                CURRENCYCODE='JPY')
        trans.transactionid = r.TRANSACTIONID
        trans.raw = r
        trans.authorised = True
        return trans

    # API METHODS

    # PayPal python NVP API wrapper class.
    # This is a sample to help others get started on working
    # with the PayPal NVP API in Python. 
    # This is not a complete reference! Be sure to understand
    # what this class is doing before you try it on production servers!
    # ...use at your own peril.

    ## see https://www.paypal.com/IntegrationCenter/ic_nvp.html
    ## and
    ## https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/index.html
    ## for more information.

    # by Mike Atlas / LowSingle.com / MassWrestling.com, September 2007
    # No License Expressed. Feel free to distribute, modify, 
    # and use in any open or closed source project without credit to the author

    def SetExpressCheckout(self, **kwargs):
        return self.interface.set_express_checkout(**kwargs)
    
    def DoExpressCheckoutPayment(self, token, **kwargs):
        return self.interface.do_express_checkout_payment(token, **kwargs)

    # Get info on transaction
    def GetTransactionDetails(self, **kwargs):
        return self.interface.get_transaction_details(**kwargs)

    # Direct payment
    def DoDirectPayment(self, **kwargs):
        return self.interface.do_direct_payment(**kwargs)