Background: Our company uses a billing system called Zuora, which integrates and syncs with Salesforce, to handle our customers subscriptions. This application (Zuora) also includes a custom Quoting interface, custom product catalog, etc. Our team uses the Zuora UI (visualforce) to build quotes, the customers then sign the agreements for those quotes, and an outbound API (part of the managed package) sends the quote and associated products to the billing system for processing.
Scenario: We are working on building an eCommerce platform. However, rather than integrating directly to Zuora, we'd like to integrate with Salesforce. Our reason for this is because we want to keep consistent records in Salesforce regarding all opportunities and quotes, and by pushing the data straight into Zuora we would bypass ever having those records created in Salesforce.
Zuora has a pretty robust set of classes/objects/methods that allows us to build and modify quotes through apex with ease, so I built an apexREST endpoint to allow the eCommerce portal to pass all of the relevant account/contact/credit card/product data to the endpoint, and then I build all of the records behind the scenes. Once I am finished building the quote and associated products, I initiate the outbound API method (from the managed package) to send that new quote to Zuora for billing.
Problem/Challenge: Just because the customer gave us credit card data and we created the quote and products in Salesforce to send to Zuora, doesn't mean that the customers credit card will be accepted by Zuora. It could decline for any number of reasons (lost/stolen card, not enough funds, general errors, etc). My challenge is that, in an ideal scenario, I would ONLY return a 200 Success from my apexREST endpoint if/when the quote is 100% processed with no errors. Unfortunately this has to happen in a separate transaction since it requires an outbound API callout to Zuora, and then Zuora kicks back an error (if there is one) and updates the quote with that error message.
I'd like to 'wait' until I confirm that Zuora accepted the entire quote without any issues before I return a 200 OK response from my endpoint. I'd hate to return a 200 OK to the customer buying online, only to have the card decline on the back-end and result in a bad customer experience/confusion since everything appears fine but actually isn't. Is something like this even possible? Can I build some sort of 'polling' logic into my apexREST endpoint to have it check the quote a few times until the status changes, and then return only after a success? If not, can anyone provide any other suggestions that may allow for this type of front-end experience?