RefundTransaction processes a refund for a given transaction by its ID. It starts a tracing span, retrieves the original transaction, validates its status, creates a new refund transaction, and queues it. Parameters: - ctx context.Context: The context for the operation. - transactionID string: The
(ctx context.Context, transactionID string, skipQueue bool)
| 2345 | // - *model.Transaction: A pointer to the refunded Transaction model. |
| 2346 | // - error: An error if the transaction could not be refunded. |
| 2347 | func (l *LedgerForge) RefundTransaction(ctx context.Context, transactionID string, skipQueue bool) (*model.Transaction, error) { |
| 2348 | ctx, span := tracer.Start(ctx, "RefundTransaction") |
| 2349 | defer span.End() |
| 2350 | |
| 2351 | // 1. Retrieve the original transaction (from DB or Queue) |
| 2352 | originalTxn, err := l.getOriginalTransactionForRefund(ctx, transactionID) |
| 2353 | if err != nil { |
| 2354 | span.RecordError(err) |
| 2355 | return nil, err // Error includes context from the helper function |
| 2356 | } |
| 2357 | |
| 2358 | // 2. Validate if the transaction can be refunded |
| 2359 | if err := l.validateTransactionForRefund(ctx, originalTxn); err != nil { |
| 2360 | span.RecordError(err) |
| 2361 | return nil, err // Error includes context from the helper function |
| 2362 | } |
| 2363 | |
| 2364 | // 3. Prepare the refund transaction object |
| 2365 | refundTxnObject := prepareRefundTransaction(originalTxn, skipQueue) |
| 2366 | |
| 2367 | // 4. Queue the refund transaction for processing |
| 2368 | queuedRefundTxn, err := l.QueueTransaction(ctx, refundTxnObject) |
| 2369 | if err != nil { |
| 2370 | span.RecordError(err) |
| 2371 | return nil, fmt.Errorf("failed to queue refund transaction %s: %w", refundTxnObject.TransactionID, err) |
| 2372 | } |
| 2373 | |
| 2374 | span.AddEvent("Refund transaction queued", trace.WithAttributes(attribute.String("refund.transaction.id", queuedRefundTxn.TransactionID))) |
| 2375 | return queuedRefundTxn, nil |
| 2376 | } |
| 2377 | |
| 2378 | // processBulkTransactions prepares and queues all transactions in a batch with the given batch ID |
| 2379 | func (l *LedgerForge) processBulkTransactions(ctx context.Context, transactions []*model.Transaction, batchID string, inflight bool, skipQueue bool) error { |