def _validate_redirect_url_and_throw_error(payment_method: str, return_url: str): """Check and Throw if the return_url is not a valid url.""" is_validity_check_needed = payment_method in ( PaymentMethod.CC.value, PaymentMethod.DIRECT_PAY.value) if is_validity_check_needed and not is_valid_redirect_url(return_url): raise BusinessException(Error.INVALID_REDIRECT_URI)
def create(payment_identifier: str, request_json: Dict): """Create transaction record.""" current_app.logger.debug('<create transaction') # Lookup payment record payment: Payment = Payment.find_by_id(payment_identifier, skip_auth_check=True) # Check if return url is valid return_url = request_json.get('clientSystemUrl') if payment.payment_system_code == PaymentSystem.PAYBC.value and not is_valid_redirect_url( return_url): raise BusinessException(Error.PAY013) if not payment.id: raise BusinessException(Error.PAY005) # Cannot start transaction on completed payment if payment.payment_status_code in (Status.COMPLETED.value, Status.DELETED.value, Status.DELETE_ACCEPTED.value): raise BusinessException(Error.PAY006) # If there are active transactions (status=CREATED), then invalidate all of them and create a new one. existing_transactions = PaymentTransactionModel.find_by_payment_id( payment.id) if existing_transactions: for existing_transaction in existing_transactions: if existing_transaction.status_code != Status.CANCELLED.value: existing_transaction.status_code = Status.CANCELLED.value existing_transaction.transaction_end_time = datetime.now() existing_transaction.save() transaction = PaymentTransaction() transaction.payment_id = payment.id transaction.client_system_url = return_url transaction.status_code = Status.CREATED.value transaction_dao = transaction.flush() transaction._dao = transaction_dao # pylint: disable=protected-access transaction.pay_system_url = transaction.build_pay_system_url( payment, transaction.id, request_json.get('payReturnUrl')) transaction_dao = transaction.save() transaction = PaymentTransaction() transaction._dao = transaction_dao # pylint: disable=protected-access current_app.logger.debug('>create transaction') return transaction