-
Notifications
You must be signed in to change notification settings - Fork 0
/
createComplexInvoice.py
75 lines (59 loc) · 2.49 KB
/
createComplexInvoice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import datetime
import json
import ConfigParser
import requests
# merchant and environment configuration
config = ConfigParser.ConfigParser()
config.read('merchant.cfg')
apikey = config.get('merchant', 'apikey')
merchant_account = config.get('merchant', 'merchant_account')
merchant_code = config.get('merchant', 'merchant_code')
base_endpoint = config.get('environment', 'base_endpoint')
# generate html page containing payment form
def generate_request(items_number, items_amount_minorunits, currency, customer_id, vat_rate, timestamp):
endpoint = base_endpoint + "/customers/" + customer_id + "/invoices"
headers = {'Content-type': 'application/json', 'apikey': apikey}
items = []
for i in range(0,items_number):
items.append(
{'name': 'product #' + str(i),
'unitPrice': items_amount_minorunits,
'amountNet': items_amount_minorunits,
'amountGross': items_amount_minorunits * (100 + vat_rate / 100) / 100,
'units': 1,
'taxName': 'IVA',
'rate': vat_rate}
)
data = {'type': 'purchase',
'fromDate': timestamp,
'toDate': timestamp,
'totalNet': items_amount_minorunits * items_number,
'totalGross': items_amount_minorunits * items_number * (100 + vat_rate / 100) / 100,
'totalTaxes': items_amount_minorunits * items_number * vat_rate / 10000,
'currency': currency,
'issueDate': timestamp,
'taxDetails': [{
'subtotalNet': items_amount_minorunits * items_number,
'rate': vat_rate,
'taxName': 'IVA'}],
'items': items
}
r = requests.post(endpoint, data=json.dumps(data), headers=headers, verify=False)
print "REQUEST : POST " + endpoint
print str(json.dumps(data))
print "RESPONSE : " + str(r.status_code)
print r.text
def main():
if len(sys.argv) != 6:
print 'usage: ./createInvoice.py items_number items_amount_minorunits currency_code customer_email vat_rate'
sys.exit(1)
items_number = int(sys.argv[1])
items_amount_minorunits = int(sys.argv[2])
currency = sys.argv[3]
customer_id = sys.argv[4]
vat_rate = int(sys.argv[5])
timestamp = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
generate_request(items_number, items_amount_minorunits, currency, customer_id, vat_rate, timestamp)
if __name__ == '__main__':
main()