I have an existing Stripe integration that I am modernizing to a Web API. Using a Stripe CLI (command line interface) I can trigger a Stripe Event to my local Controller:
[HttpPost("webhook")]
public async Task<IActionResult> Webhook()
{
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
var ignoredEvents = new List<string>() { "payment_method.attached", "payment_intent.created", "payment_intent.succeeded", "customer.source.created", "source.chargeable", "customer.subscription.updated", "customer.updated", "invoiceitem.created", "invoiceitem.updated", "invoice.created", "plan.created", "plan.updated" };
try
{
var stripeEvent = EventUtility.ParseEvent(json);
if (!ignoredEvents.Contains(stripeEvent.Type))
{
if (stripeEvent.Type == Events.InvoiceSent)
{
var invoiceSent = stripeEvent.Data.Object as Invoice;
if (invoiceSent.Status == "open")
{
if (invoiceSent.SubscriptionId == null)
{
//OwnerTxn workflow
updateOwnerTxn(invoiceSent);
invoiceSent is a Stripe Invoice Object that is passed to updateOwnerTxn (a method in the same Controller:
public void updateOwnerTxn(Invoice invoiceItem)
{
JsonPatchDocument<StOwnerTxn> patchDocOwnerTxn = new();
patchDocOwnerTxn.Replace(ot => ot.InvoiceSent, invoiceItem.StatusTransitions.FinalizedAt);
patchDocOwnerTxn.Replace(ot => ot.Modified, DateTime.UtcNow);
var serializedItem = JsonConvert.SerializeObject(patchDocOwnerTxn);
var response = PatchOwnerTxn(Convert.ToInt32(invoiceItem.Id),
new StringContent(serializedItem, System.Text.Encoding.Unicode, "application/json-patch+json"));
The plan is to create the patchDoc in updateOwnerTxn and then call PatchOwnerTxn in the same Controller:
[Route("UpdateOwnerTxn/{id}")]
[HttpPatch]
public async Task<IActionResult> PatchOwnerTxn(int id, [FromBody] JsonPatchDocument<StOwnerTxn> ownerTxn)
{
if (ownerTxn == null) { return BadRequest(); }
var theOwnerTxn = context.StOwnerTxns.FirstOrDefault(ot => ot.Id == id);
if (theOwnerTxn == null) { return NotFound(); }
ownerTxn.ApplyTo(theOwnerTxn, ModelState);
var isValid = TryValidateModel(theOwnerTxn);
if (!isValid) { return BadRequest(ModelState); }
await context.SaveChangesAsync();
return Ok();
}
There is a squiggle under new StringContent(serializedItem, System.Text.Encoding.Unicode, "application/json-patch+json"));
Argument 2: Cannot convert from System.Net.Http.StringContent to JsonPatchDocument<BtApiEf5.Model.StOwnerTxn>, which is the red peg square hole issue, understood.
This is my pattern I use for when a Patch request is made directly to the action controller i.e. [FromBody]. What is the pattern that I need to follow here?