예제 #1
0
    def add_tfl_receipt(self, transaction, list_of_fares):
        # Using a random receipt ID we generate as external ID
        receipt_id = uuid.uuid4().hex
        example_items = []
        for fare in list_of_fares:
            example_items.append(
                receipt_types.Item(fare[0], 1, "", int(fare[1]), "GBP", 20,
                                   []))
        example_payments = []
        example_taxes = []

        example_receipt = receipt_types.Receipt("", receipt_id,
                                                transaction["id"],
                                                abs(transaction["amount"]),
                                                "GBP", example_payments,
                                                example_taxes, example_items)
        example_receipt_marshaled = example_receipt.marshal()
        #print("Uploading receipt data to API: ", json.dumps(example_receipt_marshaled, indent=4, sort_keys=True))
        #print("")

        success, response = self._api_client.api_put(
            "transaction-receipts/", example_receipt_marshaled)
        if not success:
            error("Failed to upload receipt: {}".format(response))
        print("Successfully uploaded receipt date {}".format(
            transaction['settled']))
        #print("Successfully uploaded receipt {}: {}\n".format(receipt_id, response))
        return receipt_id
예제 #2
0
    def example_add_receipt_data(self, data_in, transaction_id, amount, currency, payment_type, vat_amount):
        ''' An example in which we add receipt data to the latest transaction 
            of the account, with fabricated information. You can set varying 
            receipts data on the same transaction again and again to test it 
            if you need to. 
        '''

        #transaction_id = self.transactions[-2]
        print("Using most recent transaction to attach receipt: {}".format(transaction_id))

        # Using a random receipt ID we generate as external ID
        receipt_id = uuid.uuid4().hex
       
        items = []
        for i in data_in:
            items.append(receipt_types.Item(i[0], i[1], "", i[2], currency, 0, []))

        payments = [receipt_types.Payment(payment_type, "", "", "", "", "", "", "", amount, currency )]
        taxes = [receipt_types.Tax("VAT", vat_amount, currency, "12345678" )]
        receipt = receipt_types.Receipt("", receipt_id, transaction_id, amount, currency, payments, taxes, items)
        receipt_marshaled = receipt.marshal()        
        
        print("Uploading receipt data to API: ", json.dumps(receipt_marshaled, indent=4, sort_keys=True))
        print(type(receipt_marshaled))
        
        success, response = self.api_put("transaction-receipts/", receipt_marshaled)
        if not success:
            error("Failed to upload receipt: {}".format(response))

        print("Successfully uploaded receipt {}: {}".format(receipt_id, response))
        return receipt_id
예제 #3
0
    def example_add_receipt_data(self):
        ''' An example in which we add receipt data to the latest transaction
            of the account, with fabricated information. You can set varying
            receipts data on the same transaction again and again to test it
            if you need to.
        '''
        if len(self.transactions) == 0:
            error(
                "No transactions found, either it was not loaded with list_transactions() or there's no transaction in the Monzo account :/"
            )

        most_recent_transaction = self.transactions[-1]
        print("Using most recent transaction to attach receipt: {}".format(
            most_recent_transaction))

        # Using a random receipt ID we generate as external ID
        receipt_id = uuid.uuid4().hex

        # Price amounts are in the number of pences.
        example_sub_item_1 = receipt_types.SubItem("Oranges loose", 1.5, "kg",
                                                   30, "GBP", 0)
        example_sub_item_2 = receipt_types.SubItem("Organic oranges", 1, "kg",
                                                   20, "GBP", 0)
        example_items = [
            receipt_types.Item("Selected oranges", 2.5, "kg", 50, "GBP", 0,
                               [example_sub_item_1, example_sub_item_2])
        ]
        if abs(most_recent_transaction["amount"]) > 269:
            example_items.append(
                receipt_types.Item(
                    "Excess fare", 1, "",
                    abs(most_recent_transaction["amount"]) - 269, "GBP", 20,
                    []))
        example_payments = [
            receipt_types.Payment("card", "123321", "1234", "A10B2C", "",
                                  "", "", "",
                                  abs(most_recent_transaction["amount"]),
                                  "GBP")
        ]
        example_taxes = [receipt_types.Tax("VAT", 0, "GBP", "12345678")]

        example_receipt = receipt_types.Receipt(
            "", receipt_id, most_recent_transaction["id"],
            abs(most_recent_transaction["amount"]), "GBP", example_payments,
            example_taxes, example_items)
        example_receipt_marshaled = example_receipt.marshal()
        print("Uploading receipt data to API: ",
              json.dumps(example_receipt_marshaled, indent=4, sort_keys=True))
        print("")

        success, response = self._api_client.api_put(
            "transaction-receipts/", example_receipt_marshaled)
        if not success:
            error("Failed to upload receipt: {}".format(response))

        print("Successfully uploaded receipt {}: {}".format(
            receipt_id, response))
        return receipt_id
def match_and_upload_receipt(price, date, text, link, transactions,
                             ACCESS_TOKEN):
    #get all transactions
    #http "https://api.monzo.com/transactions" \
    "Authorization: Bearer $access_token" \
    "account_id==$account_id"

    if not str(-abs(price)) in transactions:
        return -1

    candidates = list(transactions[str(-abs(price))])

    #print("candidates" + str(candidates))
    #print("firstcandidates" + str(candidates[0]))

    if len(candidates) == 0:
        print("no candidates")
        return -1

    #candidate = min(candidates, key=lambda x: (datetime.fromisoformat(x['created']) - date).total_seconds())
    candidate = candidates[0]

    before = date + timedelta(days=7)
    since = date - timedelta(days=7)

    if not (True):
        #fix the time thing
        return

    #print("found candidate: " + str(candidate))

    # Using a random receipt ID we generate as external ID
    receipt_id = uuid.uuid4().hex

    example_items = [
        receipt_types.Item(text + " " + link, 1, "", abs(candidate["amount"]),
                           "GBP", 0, [])
    ]

    example_receipt = receipt_types.Receipt("", receipt_id, candidate["id"],
                                            abs(candidate["amount"]), "GBP",
                                            "", "", example_items)
    example_receipt_marshaled = example_receipt.marshal()
    #print(date.isoformat() + " " + str(example_receipt_marshaled))
    client = requests.put("https://api.monzo.com/transaction-receipts/",
                          data=example_receipt_marshaled,
                          headers={'Authorization': 'Bearer ' + ACCESS_TOKEN})
    return 1


# test
#match_and_upload_receipt(-1010, datetime(2019,1,2),"Testing testing 1 2 3 receipt muncher","downloadmoreram.com")
예제 #5
0
def genReceipt(transaction, taxes, items):
    receipt_id = genReceiptID(transaction)
    payments = [genPayment(transaction)]

    #Gen items in json format
    jsonitems = []
    for item in items:
        jsonitems.append(genItem(item[0], item[1], item[2]))

    receipt = receipt_types.Receipt("", receipt_id, transaction["id"],
                                    abs(transaction["amount"]), "GBP",
                                    payments, taxes, jsonitems)

    return receipt
예제 #6
0
    def example_add_receipt_data(self):
        ''' An example in which we add receipt data to the latest transaction 
            of the account, with fabricated information. You can set varying 
            receipts data on the same transaction again and again to test it 
            if you need to. 
        '''
        if len(self.transactions) == 0:
            error(
                "No transactions found, either it was not loaded with list_transactions() or there's no transaction in the Monzo account :/"
            )

        transactions_index = -1
        most_recent_transaction = None
        while abs(transactions_index) <= len(self.transactions):
            # Some transactions are not initiated by the user, for example the monthly transaction charging overdraft fees. Because
            # we can only add receipts to transactions initiated by the user, we need to search from the most recent until such a
            # transaction is found.
            if self.transactions[transactions_index][
                    "user_id"] != self._api_client._user_id:
                transactions_index -= 1
                continue

            most_recent_transaction = self.transactions[transactions_index]
            break

        if most_recent_transaction == None:
            error(
                "Could not find a transaction initiated by the user, cannot continue."
            )

        print("Using most recent transaction to attach receipt: {}".format(
            most_recent_transaction))

        # Using a random receipt ID we generate as external ID
        receipt_id = uuid.uuid4().hex

        # Price amounts are in the number of pences.
        example_sub_item_1 = receipt_types.SubItem("Bananas loose", 1.5, "kg",
                                                   119, "GBP", 0)
        example_sub_item_2 = receipt_types.SubItem("Organic bananas", 1, "kg",
                                                   150, "GBP", 0)
        example_items = [
            receipt_types.Item("Selected bananas", 2.5, "kg", 269, "GBP", 0,
                               [example_sub_item_1, example_sub_item_2])
        ]
        if abs(most_recent_transaction["amount"]) > 269:
            example_items.append(
                receipt_types.Item(
                    "Excess fare", 1, "",
                    abs(most_recent_transaction["amount"]) - 269, "GBP", 20,
                    []))
        example_payments = [
            receipt_types.Payment("card", "123321", "1234", "A10B2C", "",
                                  "", "", "",
                                  abs(most_recent_transaction["amount"]),
                                  "GBP")
        ]
        example_taxes = [receipt_types.Tax("VAT", 0, "GBP", "12345678")]

        example_receipt = receipt_types.Receipt(
            "", receipt_id, most_recent_transaction["id"],
            abs(most_recent_transaction["amount"]), "GBP", example_payments,
            example_taxes, example_items)
        example_receipt_marshaled = example_receipt.marshal()
        print("Uploading receipt data to API: ",
              json.dumps(example_receipt_marshaled, indent=4, sort_keys=True))
        print("")

        success, response = self._api_client.api_put(
            "transaction-receipts/", example_receipt_marshaled)
        if not success:
            error("Failed to upload receipt: {}".format(response))

        print("Successfully uploaded receipt {}: {}".format(
            receipt_id, response))
        return receipt_id