class TestQPClient(object): def setup(self): self.client = QPClient('foobar') self.api = self.client.api self.api.perform = MagicMock() def test_api_instance(self): assert isinstance(self.client.api, QPApi) def test_get_delegation(self): self.client.get("/dummy") self.api.perform.assert_called_once_with("get", "/dummy") def test_post_delegation(self): self.client.post("/dummy") self.api.perform.assert_called_once_with("post", "/dummy") def test_delete_delegation(self): self.client.delete("/dummy") self.api.perform.assert_called_once_with("delete", "/dummy") def test_put_delegation(self): self.client.put("/dummy") self.api.perform.assert_called_once_with("put", "/dummy") def test_patch_delegation(self): self.client.patch("/dummy") self.api.perform.assert_called_once_with("patch", "/dummy") def test_non_http_method(self): def request_foobar(client, path): return self.client.foobar(path) assert_raises(AttributeError, request_foobar, self.client, "/dummy")
def get_link_url(self, return_url=""): if self.link_url == "": # request only if not already requested client = QPClient(f":{settings.QUICKPAY_API_KEY}") parent = self.payment.family.get_first_parent() address = { "name": parent.name, "street": parent.address(), "city": parent.city, "zip_code": parent.zipcode, "att": self.payment.family.email, "country_code": "DNK", } variables = address.copy() variables["family"] = self.payment.family.email if self.payment.person: variables["person_name"] = self.payment.person.name if self.payment.activity: variables[ "activity_department"] = self.payment.activity.department.name variables["activity_name"] = self.payment.activity.name try: if self.transaction_id is None: activity = client.post( "/payments", currency="DKK", order_id=self.order_id, variables=variables, invoice_address=address, shipping_address=address, ) self.transaction_id = activity["id"] self.save() if self.transaction_id is None: raise Exception("we did not get a transaction_id") # Enable auto-capture if the activity starts this year link = client.put( f"/payments/{self.transaction_id}/link", amount=self.payment.amount_ore, id=self.transaction_id, continueurl=return_url, cancelurl=return_url, customer_email=self.payment.family.email, autocapture=self.payment.activity.start_date.year <= timezone.now().year, ) self.link_url = link["url"] self.save() except Exception: # Something went wrong talking to quickpay - ask people to come back later return reverse("payment_gateway_error_view") return self.link_url
def get_link_url(self, return_url=""): if self.link_url == "": # request only if not already requested client = QPClient(":{0}".format(settings.QUICKPAY_API_KEY)) parent = self.payment.family.get_first_parent() address = { "name": parent.name, "street": parent.address(), "city": parent.city, "zip_code": parent.zipcode, "att": self.payment.family.email, "country_code": "DNK", } variables = address.copy() variables["family"] = self.payment.family.email if self.payment.person: variables["person_name"] = self.payment.person.name if self.payment.activity: variables["activity_department"] = self.payment.activity.department.name variables["activity_name"] = self.payment.activity.name try: if self.transaction_id is None: activity = client.post( "/payments", currency="DKK", order_id=self.order_id, variables=variables, invoice_address=address, shipping_address=address, ) self.transaction_id = activity["id"] self.save() if self.transaction_id is None: raise Exception("we did not get a transaction_id") link = client.put( "/payments/{0}/link".format(self.transaction_id), amount=self.payment.amount_ore, id=self.transaction_id, continueurl=return_url, cancelurl=return_url, customer_email=self.payment.family.email, autocapture=True, ) self.link_url = link["url"] self.save() except Exception: # Something went wrong talking to quickpay - ask people to come back later return reverse("payment_gateway_error_view") return self.link_url
def get_link_url(self, return_url=''): if (self.link_url == ''): #request only if not already requested client = QPClient(":{0}".format(settings.QUICKPAY_API_KEY)) parent = self.payment.family.get_first_parent() address = { 'name': parent.name, 'street': parent.address(), 'city': parent.city, 'zip_code': parent.zipcode, 'att': self.payment.family.email, 'country_code': 'DNK' } variables = address.copy() variables['family'] = self.payment.family.email if (self.payment.person): variables['person_name'] = self.payment.person.name if (self.payment.activity): variables[ 'activity_department'] = self.payment.activity.department.name variables['activity_name'] = self.payment.activity.name try: if (self.transaction_id is None): activity = client.post('/payments', currency='DKK', order_id=self.order_id, variables=variables, invoice_address=address, shipping_address=address) self.transaction_id = activity['id'] self.save() if (self.transaction_id is None): raise Exception('we did not get a transaction_id') link = client.put('/payments/{0}/link'.format( self.transaction_id), amount=self.payment.amount_ore, id=self.transaction_id, continueurl=return_url, cancelurl=return_url, customer_email=self.payment.family.email, autocapture=True) self.link_url = link['url'] self.save() except: # Something went wrong talking to quickpay - ask people to come back later return reverse('payment_gateway_error_view', kwargs={'unique': self.payment.family.unique}) return self.link_url
def capture(self): client = QPClient(f":{settings.QUICKPAY_API_KEY}") status, body, headers = client.post( f"/payments/{self.transaction_id}/capture", amount=self.payment.amount_ore, raw=True, ) if status == 202: self.payment.confirmed_dtm = timezone.now() self.payment.save()
def get_link_url(self, return_url=''): if(self.link_url == ''): #request only if not already requested client = QPClient(":{0}".format(settings.QUICKPAY_API_KEY)) parent = self.payment.family.get_first_parent() address = {'name' : parent.name, 'street' : parent.address(), 'city' : parent.city, 'zip_code' : parent.zipcode, 'att' : self.payment.family.email, 'country_code' : 'DNK' } variables = address.copy() variables['family'] = self.payment.family.email if(self.payment.person): variables['person_name'] = self.payment.person.name if(self.payment.activity): variables['activity_department'] = self.payment.activity.department.name variables['activity_name'] = self.payment.activity.name try: if(self.transaction_id == None): activity = client.post('/payments', currency='DKK', order_id=self.order_id, variables=variables, invoice_address=address, shipping_address=address) self.transaction_id = activity['id'] self.save() if(self.transaction_id == None): raise Exception('we did not get a transaction_id') link = client.put( '/payments/{0}/link'.format(self.transaction_id), amount=self.payment.amount_ore, id=self.transaction_id, continueurl=return_url, cancelurl=return_url, customer_email=self.payment.family.email, autocapture=True ) self.link_url = link['url'] self.save() except: # Something went wrong talking to quickpay - ask people to come back later return reverse('payment_gateway_error_view', kwargs={'unique':self.payment.family.unique}) return self.link_url