简体   繁体   中英

How do I maintain consistency between third-party API and my own database?

I am working with REST API and have the following scenario for processing request to make a payment:

  1. A client sends the request (payment information) to my own API.
  2. My API calls third-party REST API, passes it payment information, and receives a payment ID from it. If third-party API call fails for any reason, the error is returned to the client immediately.
  3. My API stores the resulting payment ID from step 2 and some extra info to its own database.

I want the data to be consistent between third-party API and my own database. However, if for some reason the database is not accessible on step 3, I will end up with inconsistent data — the payment will exist in third-party API, but not in my own API's database.

What's the best way of handling this consistency issue?

@Gusman talks about how to allow step 2 if step 3 cannot be completed.

TransactionScope allows you to do steps 2 and 3 in the same transaction, ie step 2 is cancelled if step 3 cannot be completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM