Пример #1
0
    def refund(self, id=None, amount=None):
        """
        Refund a certain amount of a transaction

        :param id: A transaction ID
        :type id: string
        :param amount: The amount to be refunded
        :type amount: int|float

        :return: A single transaction object
        :rtype: :doc:`../models/transaction`

        :raise InvalidRequestDataException: Raised if **id** is not provided
            and if **amount** is not provided

        """

        if not isinstance(id, str) or id is None:
            msg = "An id of type string has to be passed in with the request."

            raise InvalidRequestDataException(msg)

        if not isinstance(amount, (int, float)) or amount is None:
            msg = "An amount of type int or float has to be passed " \
                "in with the request."

            raise InvalidRequestDataException(msg)

        endpoint = "transaction/{}/refund".format(id)
        payload = {"total": amount}

        response = self.request.post(endpoint=endpoint, payload=payload)

        return Transaction(json.loads(response))
Пример #2
0
    def create(self, id=None, meta=None, total=None, pre_auth=None):
        """
        Creates a charge for a merchant through the API

        :param id: A payment method ID
        :type id: string
        :param meta: Any meta data for the payment *i.e. tax*
        :type meta: dict
        :param total: The total amount to charge
        :type total: float
        :param pre_auth: Whether or not to confirm funds exist
        :type pre_auth: boolean

        :return: A single transaction object
        :rtype: :doc:`../models/transaction`

        :raise InvalidRequestDataException: Raised if **id** is not
            provided or **meta** is not provided or **total** is not provided

        """

        if id is None or not isinstance(id, str):
            msg = "An id of type string is required to complete the request."

            raise InvalidRequestDataException(msg)

        if meta is None or not isinstance(meta, dict):
            msg = "A meta object of type dict is required to complete " \
                "the request."

            raise InvalidRequestDataException(msg)

        if total is None or not isinstance(total, (int, float)):
            msg = "A total of type int or float is required to complete " \
                "the request."

            raise InvalidRequestDataException(msg)

        endpoint = "charge"

        payload = {"payment_method_id": id, "meta": meta, "total": total}

        if pre_auth is not None:
            payload["pre_auth"] = pre_auth

        response = json.loads(
            self.request.post(endpoint=endpoint, payload=payload))

        return Transaction(response)
Пример #3
0
    def payment_methods(self, id=None):
        """
        Gets a list of payment methods for a customer from the API

        :param id: A customer's ID
        :type id: string

        :return: A list of payment method objects
        :rtype: :doc:`../models/payment_method`

        :raise InvalidRequestDataException: Raised if **id** is not provided

        """

        if not isinstance(id, str) or id is None:
            msg = "An id of type string has to be passed in with the request."

            raise InvalidRequestDataException(msg)

        endpoint = "customer/{}/payment-method".format(id)

        response = json.loads(self.request.get(endpoint=endpoint))

        payment_methods = []

        for payment_method_data in response:
            payment_methods.append(PaymentMethod(payment_method_data))

        return payment_methods
Пример #4
0
    def __make_request(self,
                       api_type,
                       method,
                       endpoint,
                       options={},
                       payload=None):
        url = "{api}/{endpoint}{query_string}".format(
            api=self.api[api_type],
            endpoint=endpoint,
            query_string=self.__build_query_string(options))

        headers = self.__build_headers()

        # Get the correct "requests" class method by passing
        # in the request method that we want
        req = getattr(requests, method, 'get')

        if payload is not None:
            res = req(url, headers=headers, data=json.dumps(payload))
        else:
            res = req(url, headers=headers)

        if res.status_code == 200:
            return res.text

        msg = "ERROR occured while trying to send a {} request: " \
            "URL: {}, HEADERS: {}, PAYLOAD: {} " \
            "STATUS CODE: {}, RESPONSE: {}"

        logger.error(
            msg.format(method.upper(), url, headers, payload, res.status_code,
                       res.text))

        if res.status_code == 401:
            raise InvalidTokenException(message=res.text)
        elif res.status_code == 404:
            raise ResourceDoesNotExistException(message=res.text)
        elif res.status_code == 409:
            raise DuplicateResourceException(message=res.text)
        elif res.status_code == 400:
            raise InvalidRequestDataException(status_code=400,
                                              message=res.text)
        elif res.status_code == 422:
            raise InvalidRequestDataException(message=res.text)
        else:
            raise FattmerchantException(message=res.text)
Пример #5
0
    def create(self, payload):
        """
        Creates a new customer for a merchant through the API

        :param payload: A dictionary with all data necessary to create a customer

            .. note:: **firstname**, **lastname**, **company**, and **email** are required
            .. code-block:: json

                {
                    "firstname": "John",
                    "lastname": "Smith",
                    "company": "ABC INC",
                    "email": "*****@*****.**",
                    "files": [],
                    "phone": "1234567898",
                    "merchant_id": "12345678",
                    "address_1": "123 Rite Way",
                    "address_2": "Unit 12",
                    "address_city": "Orlando",
                    "address_state": "FL",
                    "address_zip": "32801",
                    "address_country": "USA",
                    "reference": "",
                    "cc_emails": [],
                    "cc_sms": [],
                    "notes": "",
                    "options": {},
                    "allow_invoice_credit_card_payments": true
                }

        :type payload: dict

        :return: A single customer object
        :rtype: :doc:`../models/customer`

        :raise InvalidRequestDataException: Raised if **firstname**,
            **lastname**, **company**, and **email** are not provided

        """  # NOQA: E501

        endpoint = 'customer'
        required_fields = {"firstname", "lastname", "company", "email"}

        if not all(field in payload and isinstance(payload[field], str)
                   for field in required_fields):
            msg = "A dict with at least a firstname, lastname, email, and " \
                "company is required to perform this request."

            raise InvalidRequestDataException(msg)

        response = json.loads(
            self.request.post(endpoint=endpoint, payload=payload))

        return Customer(response)
Пример #6
0
    def get(self, id=None):
        """
        Gets a single customer's details from the API

        :param id: A customer ID
        :type id: string

        :return: A single customer object
        :rtype: :doc:`../models/customer`

        :raise InvalidRequestDataException: Raised if **id** is not provided

        """

        if not isinstance(id, str) or id is None:
            msg = "An id of type string has to be passed in with the request."

            raise InvalidRequestDataException(msg)

        endpoint = "customer/{}".format(id)
        response = self.request.get(endpoint=endpoint)

        return Customer(json.loads(response))
Пример #7
0
    def get(self, id=None):
        """
        Gets a single payment methods's details from the API by id

        :param id: A payment-method ID
        :type id: string

        :return: A single payment-method object
        :rtype: :doc:`../models/paymentmethod`

        :raise InvalidRequestDataException: Raised if **id** is not provided

        """

        if not isinstance(id, str) or id is None:
            msg = "An id of type string has to be passed in with the request."

            raise InvalidRequestDataException(msg)

        endpoint = "payment-method/{}".format(id)
        response = self.request.get(endpoint=endpoint)

        return PaymentMethod(json.loads(response))
Пример #8
0
    def get(self, options):
        """
        Gets a list of deposit batch details within a certain date range from \
        the API

        :param options: A dictionary with any optional query params

            .. note:: **start_date** or **end_date** is required
            .. code-block:: json

                {
                    "start_date": "1970-01-01",
                    "end_date": "1970-01-01",
                    "timezone": "EST",
                    "timezoneOffset": 0
                }

        :type options: dict

        :return: A list of deposit details objects
        :rtype: :doc:`../models/deposit_details`

        :raise InvalidRequestDataException: Raised if **start_date** is not
            provided or **end_date** is not provided

        """

        # Check if any of the dates are part of the options
        if ("start_date" not in options and "end_date" not in options):
            # yapf: enable
            msg = "At least a start_date or end_date is required to " \
                "make this request."

            raise InvalidRequestDataException(msg)

        # Make sure that the start date is of either type string or unicode
        if ("start_date" in options):
            if (not isinstance(options["start_date"], str)):
                msg = "The start date needs to be of type string. "

                raise InvalidRequestDataException(msg)
            else:
                # Change the date keys to keys the API accepts
                options["startDate"] = options.pop("start_date")

        # Make sure that the end date is of either type string or unicode
        if ("end_date" in options):
            if (not isinstance(options["end_date"], str)):
                msg = "The end date needs to be of type string."

                raise InvalidRequestDataException(msg)
            else:
                # Change the date keys to keys the API accepts
                options["endDate"] = options.pop("end_date")

        endpoint = "depositDetail"
        response = json.loads(self.request.get(self.api, endpoint, options))

        deposits = []

        for deposit_data in response["data"]:
            deposits.append(DepositDetails(deposit_data))

        return deposits
Пример #9
0
    def create(self, payload):
        """
        Creates a new team through the API

        :param payload: A dictionary with all data necessary to create a team

            .. note:: **company_name** and **company_email** are required
            .. code-block:: JSON

                {
                    "company_name": "Fattmerchant",
                    "comtact_name": "Fattmerchant",
                    "contact_email": "*****@*****.**",
                    "contact_phone": "8555503288",
                    "display_name": "Fattmerchant",
                    "address_1": "25 Wall Street",
                    "address_2": "Suite 1",
                    "address_city": "Orlando",
                    "address_state": "FL",
                    "address_zip": "32801",
                    "options": {}
                }

        :type payload: dict

        :return: A tuple with a team object and said team's api key
        :rtype: (:doc:`../models/team`, string)

        :raise InvalidRequestDataException: Raised if **company_name** is not
            provided or **contact_email** is not provided

        :raise ResourceNotCreatedException: Raised if the team could not be
            created within the API

        """

        if "company_name" not in payload or not isinstance(
                payload["company_name"], str):
            msg = "A company name of type string is required to complete the \
                request."

            raise InvalidRequestDataException(msg)

        if "contact_email" not in payload or not isinstance(
                payload["contact_email"], str):
            msg = "A contact email of type string is required to complete the \
                request."

            raise InvalidRequestDataException(msg)

        token, user_data, team_data = self._create_team(payload)

        endpoint = "team/apikey"
        payload = {
            "team_role": user_data["team_role"],
            "name": "{} API Key".format(team_data["company_name"]),
        }

        response = json.loads(
            self.request.post(endpoint=endpoint, payload=payload))

        return (response["api_key"], Team(team_data))