Exemple #1
0
    def dispatch(self, request, *args, **kwargs):
        order = self.get_object()

        if hasattr(order, 'coinifyapiinvoice'):
            # we already have a coinifyinvoice for this order,
            # check if it expired
            if parse_datetime(order.coinifyapiinvoice.
                              invoicejson['expire_time']) < timezone.now():
                # this coinifyinvoice expired, delete it
                logger.warning("deleting expired coinifyinvoice id %s" %
                               order.coinifyapiinvoice.invoicejson['id'])
                order.coinifyapiinvoice.delete()
                order.refresh_from_db()

        # create a new coinify invoice if needed
        if not hasattr(order, 'coinifyapiinvoice'):
            # Initiate coinify API
            coinifyapi = CoinifyAPI(settings.COINIFY_API_KEY,
                                    settings.COINIFY_API_SECRET)

            # create coinify API
            response = coinifyapi.invoice_create(
                float(order.total),
                'DKK',
                plugin_name='BornHack webshop',
                plugin_version='1.0',
                description='BornHack order id #%s' % order.id,
                callback_url=order.get_coinify_callback_url(request),
                return_url=order.get_coinify_thanks_url(request),
                cancel_url=order.get_cancel_url(request),
            )

            # Parse response
            if not response['success']:
                api_error = response['error']
                logger.error("API error: %s (%s)" %
                             (api_error['message'], api_error['code']))
                messages.error(
                    request,
                    "There was a problem with the payment provider. Please try again later"
                )
                return HttpResponseRedirect(
                    reverse_lazy('shop:order_detail',
                                 kwargs={'pk': self.get_object().pk}))
            else:
                # save this coinify invoice
                coinifyinvoice = CoinifyAPIInvoice.objects.create(
                    invoicejson=response['data'],
                    order=order,
                )
                logger.info("created new coinifyinvoice id %s" %
                            coinifyinvoice.invoicejson['id'])
        return super(CoinifyRedirectView,
                     self).dispatch(request, *args, **kwargs)
Exemple #2
0
def coinify_api_request(api_method, order, **kwargs):
    # Initiate coinify API
    coinifyapi = CoinifyAPI(settings.COINIFY_API_KEY,
                            settings.COINIFY_API_SECRET)

    # is this a supported method?
    if not hasattr(coinifyapi, api_method):
        logger.error("coinify api method not supported" % api_method)
        return False

    # get and run the API call using the SDK
    method = getattr(coinifyapi, api_method)

    # catch requests exceptions as described in https://github.com/CoinifySoftware/python-sdk#catching-errors and
    # http://docs.python-requests.org/en/latest/user/quickstart/#errors-and-exceptions
    try:
        response = method(**kwargs)
    except requests.exceptions.RequestException as E:
        logger.error("requests exception during coinify api request: %s" % E)
        return False

    # save this API request to the database
    req = CoinifyAPIRequest.objects.create(order=order,
                                           method=api_method,
                                           payload=kwargs,
                                           response=response)
    logger.debug("saved coinify api request %s in db" % req.id)

    return req