Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / libs / plugins / backends / aqpaypal / provider_getbalance.c @ f4a16f5b

History | View | Annotate | Download (7.77 KB)

1
/***************************************************************************
2
    begin       : Sat Dec 01 2018
3
    copyright   : (C) 2018 by Martin Preuss
4
    email       : martin@libchipcard.de
5

6
 ***************************************************************************
7
 *          Please see toplevel file COPYING for license details           *
8
 ***************************************************************************/
9

    
10

    
11
/* included from provider.c */
12

    
13

    
14

    
15

    
16
int APY_Provider_ExecGetBal(AB_PROVIDER *pro,
17
                              AB_IMEXPORTER_ACCOUNTINFO *ai,
18
                              AB_USER *u,
19
                              AB_TRANSACTION *j) {
20
  GWEN_HTTP_SESSION *sess;
21
  GWEN_BUFFER *tbuf;
22
  const char *s;
23
  int vmajor;
24
  int vminor;
25
  int rv;
26
  GWEN_DB_NODE *dbResponse;
27
  GWEN_DB_NODE *dbCurr;
28

    
29
  sess=AB_HttpSession_new(pro, u, APY_User_GetServerUrl(u), "https", 443);
30
  if (sess==NULL) {
31
    DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Could not create http session for user [%s]", AB_User_GetUserId(u));
32
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
33
    return GWEN_ERROR_GENERIC;
34
  }
35

    
36
  vmajor=APY_User_GetHttpVMajor(u);
37
  vminor=APY_User_GetHttpVMinor(u);
38
  if (vmajor==0 && vminor==0) {
39
    vmajor=1;
40
    vminor=0;
41
  }
42
  GWEN_HttpSession_SetHttpVMajor(sess, vmajor);
43
  GWEN_HttpSession_SetHttpVMinor(sess, vminor);
44
  GWEN_HttpSession_SetHttpContentType(sess, "application/x-www-form-urlencoded");
45

    
46
  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
47

    
48
  GWEN_Buffer_AppendString(tbuf, "user=");
49
  s=APY_User_GetApiUserId(u);
50
  if (s && *s)
51
    GWEN_Text_EscapeToBuffer(s, tbuf);
52
  else {
53
    DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Missing user id");
54
    GWEN_Buffer_free(tbuf);
55
    GWEN_HttpSession_free(sess);
56
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
57
    return GWEN_ERROR_INVALID;
58
  }
59

    
60
  GWEN_Buffer_AppendString(tbuf, "&pwd=");
61
  s=APY_User_GetApiPassword(u);
62
  if (s && *s)
63
    GWEN_Text_EscapeToBuffer(s, tbuf);
64
  else {
65
    DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Missing API password");
66
    GWEN_Buffer_free(tbuf);
67
    GWEN_HttpSession_free(sess);
68
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
69
    return GWEN_ERROR_INVALID;
70
  }
71

    
72
  GWEN_Buffer_AppendString(tbuf, "&signature=");
73
  s=APY_User_GetApiSignature(u);
74
  if (s && *s)
75
    GWEN_Text_EscapeToBuffer(s, tbuf);
76
  else {
77
    DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Missing API signature");
78
    GWEN_Buffer_free(tbuf);
79
    GWEN_HttpSession_free(sess);
80
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
81
    return GWEN_ERROR_INVALID;
82
  }
83

    
84
  GWEN_Buffer_AppendString(tbuf, "&version=");
85
  GWEN_Text_EscapeToBuffer(AQPAYPAL_API_VER, tbuf);
86

    
87
  GWEN_Buffer_AppendString(tbuf, "&method=GetBalance");
88

    
89
  /* init session */
90
  rv=GWEN_HttpSession_Init(sess);
91
  if (rv<0) {
92
    DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d)", rv);
93
    GWEN_Buffer_free(tbuf);
94
    GWEN_HttpSession_free(sess);
95
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
96
    return rv;
97
  }
98

    
99
  if (getenv("AQPAYPAL_LOG_COMM")) {
100
    int len;
101
    FILE *f;
102

    
103
    len=GWEN_Buffer_GetUsedBytes(tbuf);
104

    
105
    f=fopen("paypal.log", "a+");
106
    if (f) {
107
      fprintf(f, "\n============================================\n");
108
      fprintf(f, "Sending (GetBal):\n");
109
      if (len>0) {
110
        if (1!=fwrite(GWEN_Buffer_GetStart(tbuf), GWEN_Buffer_GetUsedBytes(tbuf), 1, f)) {
111
          DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
112
          fclose(f);
113
        }
114
        else {
115
          if (fclose(f)) {
116
            DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
117
          }
118
        }
119
      }
120
      else {
121
        fprintf(f, "Empty data.\n");
122
        if (fclose(f)) {
123
          DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
124
        }
125
      }
126
    }
127
  }
128

    
129

    
130
  /* send request */
131
  rv=GWEN_HttpSession_SendPacket(sess, "POST",
132
                                 (const uint8_t*) GWEN_Buffer_GetStart(tbuf),
133
                                 GWEN_Buffer_GetUsedBytes(tbuf));
134
  if (rv<0) {
135
    DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d)", rv);
136
    GWEN_HttpSession_Fini(sess);
137
    GWEN_Buffer_free(tbuf);
138
    GWEN_HttpSession_free(sess);
139
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
140
    return rv;
141
  }
142
  AB_Transaction_SetStatus(j, AB_Transaction_StatusSending);
143

    
144
  /* get response */
145
  GWEN_Buffer_Reset(tbuf);
146
  rv=GWEN_HttpSession_RecvPacket(sess, tbuf);
147
  if (rv<0 || rv<200 || rv>299) {
148
    DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d)", rv);
149
    GWEN_HttpSession_Fini(sess);
150
    GWEN_Buffer_free(tbuf);
151
    GWEN_HttpSession_free(sess);
152
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
153
    return rv;
154
  }
155

    
156
  if (getenv("AQPAYPAL_LOG_COMM")) {
157
    int len;
158
    FILE *f;
159

    
160
    len=GWEN_Buffer_GetUsedBytes(tbuf);
161
    f=fopen("paypal.log", "a+");
162
    if (f) {
163
      fprintf(f, "\n============================================\n");
164
      fprintf(f, "Received (GetBal):\n");
165
      if (len>0) {
166
        if (1!=fwrite(GWEN_Buffer_GetStart(tbuf), GWEN_Buffer_GetUsedBytes(tbuf), 1, f)) {
167
          DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
168
          fclose(f);
169
        }
170
        else {
171
          if (fclose(f)) {
172
            DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
173
          }
174
        }
175
      }
176
      else {
177
        fprintf(f, "Empty data.\n");
178
        if (fclose(f)) {
179
          DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno));
180
        }
181
      }
182
    }
183
  }
184

    
185

    
186
  /* deinit (ignore result because it isn't important) */
187
  GWEN_HttpSession_Fini(sess);
188
  GWEN_HttpSession_free(sess);
189

    
190
  /* parse response */
191
  dbResponse=GWEN_DB_Group_new("response");
192
  rv=APY_Provider_ParseResponse(pro, GWEN_Buffer_GetStart(tbuf), dbResponse);
193
#ifdef DEBUG_PAYPAL
194
  GWEN_DB_WriteFile(dbResponse, "paypal.db", GWEN_DB_FLAGS_DEFAULT);
195
#endif
196
  if (rv<0) {
197
    DBG_INFO(AQPAYPAL_LOGDOMAIN, "here (%d)", rv);
198
    GWEN_DB_Group_free(dbResponse);
199
    GWEN_Buffer_free(tbuf);
200
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
201
    return rv;
202
  }
203

    
204
  /* check result */
205
  s=GWEN_DB_GetCharValue(dbResponse, "ACK", 0, NULL);
206
  if (s && *s) {
207
    if (strcasecmp(s, "Success")==0 ||
208
        strcasecmp(s, "SuccessWithWarning")==0) {
209
      DBG_INFO(AQPAYPAL_LOGDOMAIN, "Success");
210
    }
211
    else {
212
      DBG_INFO(AQPAYPAL_LOGDOMAIN, "No positive response from server");
213
      GWEN_DB_Group_free(dbResponse);
214
      GWEN_Buffer_free(tbuf);
215
      AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
216
      return GWEN_ERROR_BAD_DATA;
217
    }
218
  }
219
  else {
220
    DBG_INFO(AQPAYPAL_LOGDOMAIN, "No ACK response from server");
221
    GWEN_DB_Group_free(dbResponse);
222
    GWEN_Buffer_free(tbuf);
223
    AB_Transaction_SetStatus(j, AB_Transaction_StatusError);
224
    return GWEN_ERROR_BAD_DATA;
225
  }
226

    
227
  /* now get the transactions */
228
  dbCurr=GWEN_DB_GetFirstGroup(dbResponse);
229
  while(dbCurr) {
230
    AB_BALANCE *bal;
231
    GWEN_DATE *t=NULL;
232
    AB_VALUE *vc;
233
    const char *p;
234
    
235
    DBG_NOTICE(AQPAYPAL_LOGDOMAIN, "Got a balance");
236
    
237
    /* read and parse value */
238
    p=GWEN_DB_GetCharValue(dbCurr, "L_AMT", 0, 0);
239
    if (!p)
240
      return GWEN_ERROR_BAD_DATA;
241
    vc=AB_Value_fromString(p);
242
    if (vc==NULL)
243
      return GWEN_ERROR_BAD_DATA;
244
    
245
    /* read currency (if any) */
246
    p=GWEN_DB_GetCharValue(dbCurr, "L_CURRENCYCODE", 0, "EUR");
247
    if (p)
248
      AB_Value_SetCurrency(vc, p);
249
    
250
    p=GWEN_DB_GetCharValue(dbResponse, "TIMESTAMP", 0, NULL);
251
    if (p && *p) {
252
      /*t=GWEN_Time_fromUtcString(p, "YYYY-MM-DDThh:mm:ssZ");*/
253
      t=GWEN_Date_fromStringWithTemplate(p, "YYYY-MM-DD");
254
      if (t==NULL) {
255
        DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Invalid timespec [%s]", p);
256
      }
257
    }
258
    else {
259
      DBG_ERROR(AQPAYPAL_LOGDOMAIN, "Missing timespec");
260
    }
261

    
262
    bal=AB_Balance_new();
263
    AB_Balance_SetType(bal, AB_Balance_TypeBooked);
264
    AB_Balance_SetDate(bal, t);
265
    AB_Balance_SetValue(bal, vc);
266

    
267
    AB_Value_free(vc);
268
    GWEN_Date_free(t);
269
    
270
    /* add new balance */
271
    AB_ImExporterAccountInfo_AddBalance(ai, bal);
272
    break; /* break loop, we found the balance */
273

    
274
    dbCurr=GWEN_DB_GetNextGroup(dbCurr);
275
  }
276

    
277
  GWEN_DB_Group_free(dbResponse);
278
  GWEN_Buffer_free(tbuf);
279
  AB_Transaction_SetStatus(j, AB_Transaction_StatusAccepted);
280
  return 0;
281
}
282