-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp2prio.go
executable file
·304 lines (276 loc) · 8.67 KB
/
pp2prio.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Read all Completed messages from bb_ext_paypal and write them to 4priority service
// go build pp2prio.go ; strip pp2prio; cp pp2prio /media/sf_projects/bbpriority/
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
_ "github.com/jmoiron/sqlx"
_ "github.com/joho/godotenv/autoload"
_ "github.com/pkg/errors"
)
// Read messages from database
type Contribution struct {
ID string `db:"ID"`
ORG string `db:"ORG"`
CID sql.NullString `db:"CID"`
QAMO_PARTNAME sql.NullString `db:"QAMO_PARTNAME"`
QAMO_VAT sql.NullString `db:"QAMO_VAT"`
QAMO_CUSTDES sql.NullString `db:"QAMO_CUSTDES"`
QAMO_DETAILS int64 `db:"QAMO_DETAILS"`
QAMO_PARTDES sql.NullString `db:"QAMO_PARTDES"`
QAMO_PAYMENTCODE sql.NullString `db:"QAMO_PAYMENTCODE"`
QAMO_CARDNUM sql.NullString `db:"QAMO_CARDNUM"`
QAMT_AUTHNUM sql.NullString `db:"QAMT_AUTHNUM"`
QAMO_PAYMENTCOUNT sql.NullString `db:"QAMO_PAYMENTCOUNT"`
QAMO_VALIDMONTH sql.NullString `db:"QAMO_VALIDMONTH"`
QAMO_PAYPRICE float64 `db:"QAMO_PAYPRICE"`
QAMO_CURRNCY sql.NullString `db:"QAMO_CURRNCY"`
QAMO_PAYCODE sql.NullInt64 `db:"QAMO_PAYCODE"`
QAMO_FIRSTPAY sql.NullFloat64 `db:"QAMO_FIRSTPAY"`
QAMO_EMAIL sql.NullString `db:"QAMO_EMAIL"`
QAMO_ADRESS sql.NullString `db:"QAMO_ADRESS"`
QAMO_CITY sql.NullString `db:"QAMO_CITY"`
QAMO_CELL sql.NullString `db:"QAMO_CELL"`
QAMO_FROM sql.NullString `db:"QAMO_FROM"`
QAMM_UDATE sql.NullString `db:"QAMM_UDATE"`
QAMO_LANGUAGE sql.NullString `db:"QAMO_LANGUAGE"`
QAMO_REFERENCE sql.NullString `db:"QAMO_REFERENCE"`
}
var (
urlStr string
err error
)
func main() {
host := os.Getenv("CIVI_HOST")
if host == "" {
host = "localhost"
}
dbName := os.Getenv("CIVI_DBNAME")
if dbName == "" {
dbName = "localhost"
}
user := os.Getenv("CIVI_USER")
if user == "" {
log.Fatalf("Unable to connect without username\n")
}
password := os.Getenv("CIVI_PASSWORD")
if password == "" {
log.Fatalf("Unable to connect without password\n")
}
protocol := os.Getenv("CIVI_PROTOCOL")
if protocol == "" {
log.Fatalf("Unable to connect without protocol\n")
}
urlStr = os.Getenv("PRIO_HOST")
if urlStr == "" {
log.Fatalf("Unable to connect Priority without its address\n")
}
db, stmt := OpenDb(host, user, password, protocol, dbName)
defer closeDb(db)
ReadMessages(db, stmt)
}
// Connect to DB
func OpenDb(host string, user string, password string, protocol string, dbName string) (db *sqlx.DB, stmt *sql.Stmt) {
dsn := fmt.Sprintf("%s:%s@%s(%s)/%s", user, password, protocol, host, dbName)
if db, err = sqlx.Open("mysql", dsn); err != nil {
log.Fatalf("DB connection error: %v\n", err)
}
if err = db.Ping(); err != nil {
log.Fatalf("DB real connection error: %v\n", err)
}
if !isTableExists(db, dbName, "bb_ext_paypal") {
log.Fatalf("Table 'bb_ext_paypal' does not exist\n")
}
stmt, err = db.Prepare("UPDATE bb_ext_paypal SET status = 'processed' WHERE id = ?")
if err != nil {
log.Fatalf("Unable to prepare UPDATE statement: %v\n", err)
}
return
}
func closeDb(db *sqlx.DB) {
db.Close()
}
func isTableExists(db *sqlx.DB, dbName string, tableName string) (exists bool) {
var name string
if err = db.QueryRow(
"SELECT table_name name FROM information_schema.tables WHERE table_schema = '" + dbName +
"' AND table_name = '" + tableName + "' LIMIT 1").Scan(&name); err != nil {
return false
} else {
return name == tableName
}
}
func ReadMessages(db *sqlx.DB, markAsDone *sql.Stmt) {
totalPaymentsRead := 0
contribution := Contribution{}
rows, err := db.Queryx(`
SELECT
id ID,
reference QAMO_REFERENCE,
organization ORG,
sku QAMO_PARTNAME,
'0' QAMO_VAT,
name QAMO_CUSTDES, -- customer name
1 QAMO_DETAILS, -- participants
SUBSTRING(details, 1, 48) QAMO_PARTDES, -- description
CASE currency
WHEN 'USD' THEN 'PPU' -- PayPal USD
WHEN 'EUR' THEN 'PPE' -- PayPal EURO
WHEN 'ILS' THEN 'PPS' -- PayPal SHEKEL
ELSE 'PPX' -- PayPal Unknown
END QAMO_PAYMENTCODE,
'' QAMO_CARDNUM,
'' QAMT_AUTHNUM,
'' QAMO_PAYMENTCOUNT,
'' QAMO_VALIDMONTH,
price QAMO_PAYPRICE,
CASE currency
WHEN 'USD' THEN '$'
WHEN 'EUR' THEN 'EUR'
ELSE 'ש"ח'
END QAMO_CURRNCY,
1 QAMO_PAYCODE,
0 QAMO_FIRSTPAY,
email QAMO_EMAIL,
street QAMO_ADRESS,
city QAMO_CITY,
phone QAMO_CELL,
country QAMO_FROM,
created_at QAMM_UDATE,
CASE language
WHEN 'HE' THEN 'HE'
WHEN 'RU' THEN 'RU'
ELSE 'EN'
END QAMO_LANGUAGE
FROM bb_ext_paypal
WHERE
status = 'new'
`)
if err != nil {
log.Fatalf("Unable to select rows: %v\n", err)
}
for rows.Next() {
// Read messages from DB
err = rows.StructScan(&contribution)
if err != nil {
log.Fatalf("Table 'bb_ext_paypal' access error: %v\n", err)
}
// Submit 2 priority
submit2priority(contribution)
// Update Reported2prio in case of success
updateReported2prio(markAsDone, contribution.ID)
totalPaymentsRead++
}
fmt.Printf("Total of %d payments were transferred to Priority\n", totalPaymentsRead)
}
func submit2priority(contribution Contribution) {
// priority's database structure
type Priority struct {
ID string `json:"id"`
UserName string `json:"name"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
City string `json:"city"`
Country string `json:"country"`
Description string `json:"event"`
Participants int64 `json:"participants"`
Income string `json:"income"`
Is46 bool `json:"is46"`
Token string `json:"token"`
Approval string `json:"approval"`
CardType string `json:"cardtype"`
CardNum string `json:"cardnum"`
CardExp string `json:"cardexp"`
Installments int64 `json:"installments"`
FirstPay float64 `json:"firstpay"`
CreatedAt string `json:"created_at"`
Language string `json:"language"`
Reference string `json:"reference"`
Organization string `json:"organization"`
IsVisual bool `json:"is_visual"`
}
type Message struct {
Error bool
Message string
}
priority := Priority{
ID: contribution.ID,
UserName: contribution.QAMO_CUSTDES.String,
Participants: contribution.QAMO_DETAILS,
Income: contribution.QAMO_PARTNAME.String,
Description: contribution.QAMO_PARTDES.String,
CardType: contribution.QAMO_PAYMENTCODE.String,
CardNum: contribution.QAMO_PAYMENTCOUNT.String,
CardExp: contribution.QAMO_VALIDMONTH.String,
Amount: contribution.QAMO_PAYPRICE,
Currency: contribution.QAMO_CURRNCY.String,
Installments: contribution.QAMO_PAYCODE.Int64,
FirstPay: contribution.QAMO_FIRSTPAY.Float64,
Token: contribution.QAMO_CARDNUM.String,
Approval: contribution.QAMT_AUTHNUM.String,
Is46: contribution.QAMO_VAT.String == "1",
Email: contribution.QAMO_EMAIL.String,
Address: contribution.QAMO_ADRESS.String,
City: contribution.QAMO_CITY.String,
Country: contribution.QAMO_FROM.String,
Phone: contribution.QAMO_CELL.String,
CreatedAt: contribution.QAMM_UDATE.String,
Language: contribution.QAMO_LANGUAGE.String,
Reference: contribution.QAMO_REFERENCE.String,
Organization: contribution.ORG,
IsVisual: false, // CiviCRM produces Logical Hebrew
}
marshal, err := json.Marshal(priority)
if err != nil {
log.Fatalf("Marshal error: %v\n", err)
}
log.Printf("=> %s\n", marshal)
log.Printf("=> %s\n", urlStr)
req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer(marshal))
if err != nil {
log.Fatalf("NewRequest error: %v\n", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("client.Do error: %v\n", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("ReadAll error: %v\n", err)
}
message := Message{}
log.Printf("<= %s\n", body)
if err := json.Unmarshal(body, &message); err != nil {
log.Fatalf("Unmarshal error: %v\n", err)
}
if message.Error {
log.Fatalf("Response error: %s\n", message.Message)
}
}
func updateReported2prio(stmt *sql.Stmt, id string) {
res, err := stmt.Exec(id)
if err != nil {
log.Fatalf("Update error: %v\n", err)
}
rowCnt, err := res.RowsAffected()
if err != nil {
log.Fatalf("Update error: %v\n", err)
}
if rowCnt != 1 {
log.Fatalf("Update error: %d rows were updated instead of 1\n", rowCnt)
}
}