@@ -80,13 +80,41 @@ func (a *API) PaymentListForOrder(w http.ResponseWriter, r *http.Request) error
80
80
return sendJSON (w , http .StatusOK , order .Transactions )
81
81
}
82
82
83
- // PaymentCreate is the endpoint for creating a payment for an order
84
- func (a * API ) PaymentCreate (w http.ResponseWriter , r * http.Request ) error {
83
+ func paymentComplete (r * http.Request , tx * gorm.DB , tr * models.Transaction , order * models.Order ) {
85
84
ctx := r .Context ()
86
85
log := getLogEntry (r )
87
86
config := gcontext .GetConfig (ctx )
87
+
88
+ tr .Status = models .PaidState
89
+ tx .Create (tr )
90
+ order .PaymentState = models .PaidState
91
+ tx .Save (order )
92
+
93
+ if config .Webhooks .Payment != "" {
94
+ hook , err := models .NewHook ("payment" , config .SiteURL , config .Webhooks .Payment , order .UserID , config .Webhooks .Secret , order )
95
+ if err != nil {
96
+ log .WithError (err ).Error ("Failed to process webhook" )
97
+ }
98
+ tx .Save (hook )
99
+ }
100
+ }
101
+
102
+ func sendOrderConfirmation (ctx context.Context , log logrus.FieldLogger , tr * models.Transaction ) {
88
103
mailer := gcontext .GetMailer (ctx )
89
104
105
+ err1 := mailer .OrderConfirmationMail (tr )
106
+ err2 := mailer .OrderReceivedMail (tr )
107
+
108
+ if err1 != nil || err2 != nil {
109
+ log .Errorf ("Error sending order confirmation mails: %v %v" , err1 , err2 )
110
+ }
111
+ }
112
+
113
+ // PaymentCreate is the endpoint for creating a payment for an order
114
+ func (a * API ) PaymentCreate (w http.ResponseWriter , r * http.Request ) error {
115
+ ctx := r .Context ()
116
+ log := getLogEntry (r )
117
+
90
118
params := PaymentParams {Currency : "USD" }
91
119
err := json .NewDecoder (r .Body ).Decode (& params )
92
120
if err != nil {
@@ -156,21 +184,28 @@ func (a *API) PaymentCreate(w http.ResponseWriter, r *http.Request) error {
156
184
return internalServerError ("We failed to authorize the amount for this order: %v" , err )
157
185
}
158
186
159
- invoiceNumber , err := models .NextInvoiceNumber (tx , order .InstanceID )
160
- if err != nil {
161
- tx .Rollback ()
162
- return internalServerError ("We failed to generate a valid invoice ID, please try again later: %v" , err )
187
+ invoiceNumber := order .InvoiceNumber
188
+ if invoiceNumber == 0 {
189
+ var err error
190
+ invoiceNumber , err = models .NextInvoiceNumber (tx , order .InstanceID )
191
+ if err != nil {
192
+ tx .Rollback ()
193
+ return internalServerError ("We failed to generate a valid invoice ID, please try again later: %v" , err )
194
+ }
195
+ order .InvoiceNumber = invoiceNumber
163
196
}
164
197
165
198
tr := models .NewTransaction (order )
166
199
processorID , err := charge (params .Amount , params .Currency , order , invoiceNumber )
167
200
tr .ProcessorID = processorID
168
201
tr .InvoiceNumber = invoiceNumber
202
+ order .PaymentProcessor = provider .Name ()
169
203
170
204
if pendingErr , ok := err .(* payments.PaymentPendingError ); ok {
171
205
tr .Status = models .PendingState
172
206
tr .ProviderMetadata = pendingErr .Metadata ()
173
207
tx .Create (tr )
208
+ tx .Save (order )
174
209
tx .Commit ()
175
210
return sendJSON (w , 200 , tr )
176
211
}
@@ -184,32 +219,10 @@ func (a *API) PaymentCreate(w http.ResponseWriter, r *http.Request) error {
184
219
return internalServerError ("There was an error charging your card: %v" , err ).WithInternalError (err )
185
220
}
186
221
187
- // mark order and transaction as paid
188
- tr .Status = models .PaidState
189
- tx .Create (tr )
190
- order .PaymentProcessor = provider .Name ()
191
- order .PaymentState = models .PaidState
192
- order .InvoiceNumber = invoiceNumber
193
- tx .Save (order )
194
-
195
- if config .Webhooks .Payment != "" {
196
- hook , err := models .NewHook ("payment" , config .SiteURL , config .Webhooks .Payment , order .UserID , config .Webhooks .Secret , order )
197
- if err != nil {
198
- log .WithError (err ).Error ("Failed to process webhook" )
199
- }
200
- tx .Save (hook )
201
- }
202
-
222
+ paymentComplete (r , tx , tr , order )
203
223
tx .Commit ()
204
224
205
- go func () {
206
- err1 := mailer .OrderConfirmationMail (tr )
207
- err2 := mailer .OrderReceivedMail (tr )
208
-
209
- if err1 != nil || err2 != nil {
210
- log .Errorf ("Error sending order confirmation mails: %v %v" , err1 , err2 )
211
- }
212
- }()
225
+ go sendOrderConfirmation (ctx , log , tr )
213
226
214
227
return sendJSON (w , http .StatusOK , tr )
215
228
}
@@ -271,15 +284,13 @@ func (a *API) PaymentConfirm(w http.ResponseWriter, r *http.Request) error {
271
284
trans .InvoiceNumber = invoiceNumber
272
285
}
273
286
274
- trans .Status = models .PaidState
275
- tx .Save (trans )
276
- order .State = models .PaidState
277
- order .InvoiceNumber = trans .InvoiceNumber
278
- tx .Save (order )
287
+ paymentComplete (r , tx , trans , order )
279
288
if err := tx .Commit ().Error ; err != nil {
280
289
return internalServerError ("Saving payment failed" ).WithInternalError (err )
281
290
}
282
291
292
+ go sendOrderConfirmation (ctx , log , trans )
293
+
283
294
return sendJSON (w , 200 , trans )
284
295
}
285
296
0 commit comments