def order_status_update(request, order=None): ''' Updates the order status with ogone data. There are two ways of reaching this flow - payment redirect (user gets redirected through this flow) - ogone server side call (in case of problems ogone will post to our server with an updated version ofo the payment status) ''' log.debug('Attempting to update status information', extra={'request': request}) params = request.GET or request.POST if params.get('orderID', False): # Get Ogone settings from Satchmo ogone = Ogone(params, settings=get_ogone_settings()) # Make sure we check the data, and raise an exception if its wrong ogone.is_valid() # Fetch parsed params parsed_params = ogone.parse_params() log.debug('We have found a valid status feedback message.', extra={'data':{'parsed_params': parsed_params}}) # Get the order payment_id = ogone.get_order_id() try: ogone_payment = OrderPayment.objects.get(pk=payment_id) except OrderPayment.DoesNotExist: log.warning('Payment with payment_id=%d not found.', payment_id) return HttpResponse('') ogone_order = ogone_payment.order assert not order or (ogone_order.pk == order.pk), \ 'Ogone\'s order and my order are different objects.' log.debug('Found order %s for payment %s in processing feedback.', ogone_order, ogone_payment) # Do order processing and status comparisons here processor = get_processor_by_key('PAYMENT_OGONE') status_code = parsed_params['STATUS'] status_num = int(status_code) assert status_num, 'No status number.' log.debug('Recording status: %s (%s)', status_codes.STATUS_DESCRIPTIONS[status_num], status_code) # Prepare parameters for recorder params = {'amount': Decimal(parsed_params['AMOUNT']), 'order': ogone_order, 'transaction_id': parsed_params['PAYID'], 'reason_code': status_code } if status_num in (9, 91): # Payment was captured try: authorization = OrderAuthorization.objects.get(order=ogone_order, \ transaction_id=parsed_params['PAYID'], \ complete=False) params.update({'authorization': authorization}) except OrderAuthorization.DoesNotExist: pass processor.record_payment(**params) # Only change the status when it was empty or 'New' before. def _latest_status(order): try: curr_status = order.orderstatus_set.latest() return curr_status.status except OrderStatus.DoesNotExist: return '' if _latest_status(ogone_order) in ('', 'New'): ogone_order.add_status(status='Billed', notes=_("Payment accepted by Ogone.")) elif status_num in (5, 51): # Record authorization processor.record_authorization(**params) elif status_num in (4, 41): # We're still waiting ogone_order.add_status(status='New', notes=_("Payment is being processed by Ogone.")) else: # Record failure processor.record_failure(**params) if status_num in (1,): # Order cancelled ogone_order.add_status(status='Cancelled', notes=_("Order cancelled through Ogone.")) elif status_num in (2, 93): # Payment declined ogone_order.add_status(status='Blocked', notes=_("Payment declined by Ogone.")) elif status_num in (52, 92): log.warning('Payment of order %s (ID: %d) uncertain. Status: %s (%d)', ogone_order, ogone_order.pk, status_codes.STATUS_DESCRIPTIONS[status_num], status_num) else: log.warning('Uknown status code %d found for order %s.', status_num, ogone_order, exc_info=sys.exc_info() ) else: log.warning('This response does not look valid, orderID not found.', extra={'request': request}) # Return an empty HttpResponse return HttpResponse('')