這是一個非常混亂的問題,所以我會盡我所能詳細說明。我有一個Facebook的畫布應用程序,需要支付,當用戶點擊付款按鈕,將出現以下情況:以錯誤的順序完成回調函數
我的JavaScript回調函數被調用,並通過支付ID,所以我保存這個支付的ID等信息到我的訂單數據庫。
Facebook調用一個回叫網址,我已經設置了讓我知道付款何時通過。它只給我付款ID,所以我使用它來搜索數據庫中的行,並附上他們發送的付款ID。然後,我使用該行中的信息填充發送給客戶的電子郵件,以確認其訂單。
我的大問題是,由於某種原因步驟2中得到第1步之前完成,所以當我嘗試查找付款ID在數據庫中,它還不存在,所以我不能發給客戶發電子郵件。我能做些什麼來解決這個問題?我在下面的兩個步驟都有僞代碼。
第1步:
using (OrderDBContext order = new OrderDBContext())
{
string message = Encryption.SimpleDecryptWithPassword(orderDetails.request_id, GlobalFacebookConfiguration.Configuration.AppId, 0);
string[] finalMessage = message.Split('@');
int orderID = Convert.ToInt16(finalMessage.ElementAtOrDefault(2));
Models.Order row = order.Orders.Where(i => i.ID == orderID).FirstOrDefault();
switch (orderDetails.status)
{
case "completed":
row.PaymentID = orderDetails.payment_id;
row.Currency = orderDetails.currency;
row.HashKey = orderDetails.request_id;
row.Paid = true;
order.SaveChanges();
return Json(new { message = "Your payment was processed! You will receive a confirmation email soon." }, JsonRequestBehavior.AllowGet);
case "initiated":
row.PaymentID = orderDetails.payment_id;
row.Currency = orderDetails.currency;
row.HashKey = orderDetails.request_id;
row.Paid = false;
order.SaveChanges();
return Json(new { message = "Your payment is being processed! You will receive a confirmation email as soon as the payment is confirmed." }, JsonRequestBehavior.AllowGet);
}
}
第2步:
dynamic result = new StreamReader(request.InputStream).ReadToEnd();
var items = JsonConvert.DeserializeObject<RootObject>(result);
string paymentID;
if (items.entry != null && items.entry.Count > 0)
{
paymentID = items.entry[0].id;
}
else
{
// logic when items.entry is null or doesn't have any elements
paymentID = null;
}
if (PaymentHelper.confirmPayment(paymentID, GlobalFacebookConfiguration.Configuration.AppId, GlobalFacebookConfiguration.Configuration.AppSecret))
{
// if payment is confirmed then send email to us with the order details
// then send confirmation email to user letting them know that we are working on it
using (OrderDBContext order = new OrderDBContext())
{
Order row = order.Orders.Where(i => i.PaymentID == paymentID).FirstOrDefault();
SendEmail.sendOrderDetailsToWriter(row);
SendEmail.sendOrderDetailsToCustomer(row);
}
}
你需要第三步同步2個第一。 – tschmit007 2014-12-06 10:25:04
@ tschmit007不確定你的意思。你能舉一些例子代碼嗎? – user3610374 2014-12-06 11:03:51