def post(self): models.save_communication(self.request.body) if self.request.headers.get('Authorization'): #get the Authorization string from the Google POST header auth_string = self.request.headers.get('Authorization') #decode the Authorization string and remove Basic portion plain_string = base64.b64decode(auth_string.lstrip('Basic ')) #split the decoded string at the ':' split_string = plain_string.split(':') merchant_id = split_string[0] merchant_key = split_string[1] if merchant_id == settings.MERCHANT_ID and merchant_key == settings.MERCHANT_KEY and self.request.body: notification = self.request.body notification_dict = parse_google_response(notification) notification_processed = False if notification_dict['type'] == 'new-order-notification': notification_processed = new_order_notification(notification_dict) elif notification_dict['type'] == 'risk-information-notification': notification_processed = True elif notification_dict['type'] == 'order-state-change-notification': notification_processed = True elif notification_dict['type'] == 'charge-amount-notification': notification_processed = amount_notification(notification_dict) elif notification_dict['type'] == 'authorization-amount-notification': notification_processed = True else: pass #all other notifications are ignored if notification_processed: self.handshake(notification_dict['serial-number']) else: self.error(401) else: self.error(401) return
def handshake(self, serial_number): "Acknowledge receipt of notification." doc = minidom.Document() #minidom object creation notification_acknowledgment = doc.createElement("notification-acknowledgment") notification_acknowledgment.setAttribute("xmlns", "http://checkout.google.com/schema/2") notification_acknowledgment.setAttribute("serial-number", serial_number) doc.appendChild(notification_acknowledgment) acknowledgment = doc.toxml(encoding="utf-8") models.save_communication(acknowledgment) self.response.headers['Content-Type'] = 'text/xml' self.response.out.write(acknowledgment) return