public async Task <IActionResult> ProcessPayment([FromBody] PaymentDetails paymentDetails) { // TODO: Relying on the bank to check the validity of the currency. We only ensure it's 3 letters. if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { // Create our own ID to track the request var paymentId = Guid.NewGuid(); // Attempt to pay via the bank PaymentResponse response = await _bank.ProcessPaymentAsync(paymentDetails.CardDetails, paymentDetails.TransactionDetails); // Store history of both failed and succeeded payments var request = new PaymentRequestLog { Id = paymentId, MaskedCardDetails = new MaskedCardDetails(paymentDetails.CardDetails), TransactionDetails = paymentDetails.TransactionDetails, PaymentResponse = response }; await _store.LogPaymentRequest(request); return(Ok(new ProcessPaymentResult(paymentId, response))); } catch (Exception e) { _logger.LogError(e, "Error occurred Processing Payment"); return(InternalServerError()); } }
/// <summary> /// Store the record of an attempted payment, and its result /// </summary> /// <param name="request">Log of the payment request</param> public async Task LogPaymentRequest(PaymentRequestLog request) { var document = new Document <PaymentRequestLog>() { Id = request.Id.ToString(), Content = request }; var result = await Bucket.UpsertAsync(document); if (!result.Success) { throw new CouchbaseResponseException("Could not Log Payment"); } }
public async Task <IActionResult> FindPayment(Guid id) { try { PaymentRequestLog paymentInfo = await _store.FindPaymentRequest(id); return(Ok(paymentInfo)); } catch (RecordNotFoundException) { return(NotFound()); } catch (Exception e) { _logger.LogError(e, "Error occurred Finding Payment"); return(InternalServerError()); } }