def create(cls, shopping_cart: ShoppingCart, channel = Channel.WEB, external_unique_number = None, options = None): if (channel != None and channel == Channel.APP and not onepay.app_scheme): raise TransactionCreateError("You need to set an app_scheme if you want to use the APP channel") if (channel != None and channel == Channel.MOBILE and not onepay.callback_url): raise TransactionCreateError("You need to set valid callback if you want to use the MOBILE channel") if not hasattr(shopping_cart, 'items') or (hasattr(shopping_cart, 'items') and not shopping_cart.items): raise ValueError("Shopping cart must not be null or empty") path = cls.__TRANSACTION_BASE_PATH + cls.__SEND_TRANSACTION api_base = onepay.integration_type.value.api_base external_unique_number_req = external_unique_number or int(datetime.now().timestamp() * 1000) options = Options.build(options) req = TransactionCreateRequest(external_unique_number_req, shopping_cart.total, shopping_cart.item_quantity, int(datetime.now().timestamp()), shopping_cart.items, onepay.callback_url, (channel or Channel.WEB).value , onepay.app_scheme, options) data_response = requests.post(api_base + path, data = TransactionCreateRequestSchema().dumps(req).data).text transaction_response = SendTransactionResponseSchema().loads(data_response).data if transaction_response['response_code'] != "OK": raise TransactionCreateError("%s : %s" % (transaction_response['response_code'], transaction_response['description'])) result = TransactionCreateResponse(**transaction_response['result']) if not result.is_valid_signature((options or onepay).shared_secret, result.signature): raise TransactionCreateError("The response signature is not valid.", -1) return result
def test_create_transaction_given_options(self): onepay.api_key = None onepay.shared_secret = None options = Options(self.api_key_mock, self.shared_secret_mock) response = Transaction.create(self.get_valid_cart(), options=options) self.assertIsNotNone(response) self.assertIsNotNone(response.occ) self.assertIsNotNone(response.ott) self.assertIsNotNone(response.signature) self.assertIsNotNone(response.external_unique_number) self.assertIsNotNone(response.qr_code_as_base64)
def test_create_refund_given_options(self): onepay.api_key = None onepay.shared_secret = None options = Options(self.api_key_mock, self.shared_secret_mock) response = Refund.create(27500, self.occ_commit_test, self.external_unique_number_test, self.auth_code_test, options) self.assertIsNotNone(response) self.assertIsNotNone(response.occ) self.assertIsNotNone(response.signature) self.assertIsNotNone(response.reverse_code) self.assertIsNotNone(response.external_unique_number) self.assertIsNotNone(response.issued_at)
def test_create_transaction_skip_none_params(self): external_unique_number_req = 123 options = Options() shopping_cart = self.get_valid_cart() req = TransactionCreateRequest(external_unique_number_req, shopping_cart.total, shopping_cart.item_quantity, 5473782781, shopping_cart.items, onepay.callback_url, Channel.WEB.value, onepay.app_scheme, options) request_string = TransactionCreateRequestSchema().dumps(req).data request_json = json.loads(request_string) self.assertFalse('commerceLogoUrl' in request_json) self.assertFalse('widthHeight' in request_json)
def test_commit_transaction_given_options(self): onepay.api_key = None onepay.shared_secret = None options = Options(self.api_key_mock, self.shared_secret_mock) response = Transaction.commit(self.occ_commit_test, self.external_unique_number_test, options) self.assertIsNotNone(response) self.assertIsNotNone(response.authorization_code) self.assertIsNotNone(response.occ) self.assertIsNotNone(response.signature) self.assertIsNotNone(response.transaction_desc) self.assertIsNotNone(response.buy_order) self.assertIsNotNone(response.issued_at) self.assertIsNotNone(response.amount) self.assertIsNotNone(response.installments_amount) self.assertIsNotNone(response.installments_number)
def test_create_options(self): options = Options("api_key", "shared_secret") self.assertEqual(options.api_key, "api_key") self.assertEqual(options.shared_secret, "shared_secret")