-
Notifications
You must be signed in to change notification settings - Fork 0
/
combineOrderGrooveATG.py
executable file
·157 lines (131 loc) · 4.85 KB
/
combineOrderGrooveATG.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
"""
Created on Mar 25, 2021
combineOrderGrooveATG.py
Script was created to combine credit card info from OrderGroove
and with the Oracle ATG extract
@author: dougrob
"""
import configparser
import csv
# import os
# import re
import sys
import base64
import time
# ## Function to open a file as a csv
# ## All of the files are treated as a Csv, whether they are true CSVs or not.
# ## The reason for this is so that if a file needs more columns we have that ability
def open_csv(fname, t="r", fieldnames=""):
""" Function to open a csv file """
fhand = open(fname, t)
if t == "r":
csvfile = csv.DictReader(fhand, dialect='excel')
else:
csvfile = csv.DictWriter(
fhand, dialect='excel', quoting=csv.QUOTE_NONE, fieldnames=fieldnames)
return csvfile
def trace(level, string):
""" Trace function """
if level <= int(config.get('Debug', 'LogLevel')):
print('%s' % string)
sys.stdout.flush()
def combineOrderGroove(input_file, atgPayments):
""" Import OrderGroove function
This is the main import function
It starts off reading in the csv file provided by Order Groove
Then it it puts those into a dictionary
"""
ogcsv = open_csv(input_file)
goodRows = {}
badRows = {}
for row in ogcsv:
if len(row) > 0:
ycccustid = row["YC Customer ID"].strip()
label = row["Payment Label"].strip()
if label != "PayPal":
label = "CreditCard"
try:
token = atgPayments[ycccustid + label]
except Exception as error:
token = ""
ogpayid = row["OG Payment Public ID"].strip()
rowdict = {
"OGPaymentID": row["OG Payment ID"].strip(),
"OGCustomerID": row["OG Customer ID"].strip(),
"PaymentLabel": label,
"TokenID": token,
"OGPaymentPublicID": ogpayid,
"YCCustomerID": ycccustid
}
if token != "":
trace(2, "good - %s" % rowdict)
goodRows[ogpayid] = rowdict
else:
trace(2, "bad - %s" % rowdict)
badRows[ogpayid] = rowdict
else:
trace(3, "Row length was 0")
return goodRows, badRows
def importATG(creditcard_file, paypal_file):
""" import ATG function
This is function creates a payment map that we can use to fill in the token ids
for OrderGroove
"""
cccsv = open_csv(creditcard_file)
decodedDictionary = {}
for row in cccsv:
try:
if len(row) > 0:
custId = row["OG Customer ID"].strip()
label = "CreditCard"
token_id = row["SUBSCRIPTION_ID"].strip()
trace(5, "%s,%s,%s" % (custId, label, token_id))
decodedDictionary[custId + label] = token_id
else:
trace(3, "Row length was 0")
except Exception as error:
print("Error! %s had the following error %s" %
(row["OG Customer ID"].strip(), error))
pplcsv = open_csv(paypal_file)
for row in pplcsv:
try:
if len(row) > 0:
custId = row["OG Customer ID"].strip()
label = "PayPal"
token_id = row["BILL_AGRMNT_ID"].strip()
trace(5, "%s,%s,%s" % (custId, label, token_id))
decodedDictionary[custId + label] = token_id
else:
trace(3, "Row length was 0")
except Exception as error:
print("Error! %s had the following error %s" %
(row["OG Customer ID"].strip(), error))
return decodedDictionary
# ## Output Writer
def writeOutput(rows, ofile):
""" Function that will write the output file for Cybersource """
csv = open_csv(ofile, "w", config.get(
'OrderGroove', 'outputColumnNames').split(','))
csv.writeheader()
for k in rows.keys():
csv.writerow(rows[k])
# # This is the main Function for decodeOrderGroove.py
# # This is where it all starts. The Main Function
if __name__ == '__main__':
# # Set up global variables
# Note: We must use Raw Config Parser to prevent interpolation of '%' and other weird characters
config = configparser.ConfigParser()
config.read_file(open('./config.ini'))
inputfile = config.get('Base', 'input_file')
creditcard_file = config.get('Base', 'atg_cc_file')
paypal_file = config.get('Base', 'atg_ppl_file')
outputfile = config.get('Base', 'output_file')
trace(3, "Output file is %s" % outputfile)
# Open & Decode File
atgPayments = importATG(creditcard_file, paypal_file)
goodrows, badrows = combineOrderGroove(inputfile, atgPayments)
# Write output file
writeOutput(goodrows, outputfile)
# Write bad file
writeOutput(badrows, outputfile + ".bad")