I am using Stripe payment gateway for Ecommerce transaction. To communicate I have to use webhook url means I will provide a url so that they can commincate with us.
I created a controller and Action which is [AllowAnonymus]
. When I run application locally and type controller and action on browser then it hit the action. But when I deployed on my test server and do the same it gives the following error
Server Error in '/' Application. The device is not ready.
Stack Trace:
[IOException: The device is not ready.
]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +14840940
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +211
System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +210
System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) +87
SchoolManagement.DTO.Entities.OrderItem.SavePartialCharge(String invoiceData) in c:\Atlassian\Bamboo-home\xml-data\build-dir\SMS-PPINBOX-JOB1\SchoolDTO\Entities\OrderItem.cs:185
SchoolWeb.Controllers.StripeWebhookController.GetStripeResponse() in c:\Atlassian\Bamboo-home\xml-data\build-dir\SMS-PPINBOX-JOB1\SchoolWeb\Controllers\StripeWebhookController.cs:24
lambda_method(Closure , ControllerBase , Object[] ) +79
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +270
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
Can any one help me to resolve this issue.
The following code written in Action:
public class StripeWebhookController : BaseController
{
//
// GET: /StripeWebhook/
[AllowAnonymous]
public JsonResult GetStripeResponse()
{
Stream req = Request.InputStream;
req.Seek(0, System.IO.SeekOrigin.Begin);
string json = new StreamReader(req).ReadToEnd();
DTO.OrderItem.SavePartialCharge(json);
return Json("", JsonRequestBehavior.AllowGet);
}
}
Adding the method SavePartialCharge
public static String SavePartialCharge(string invoiceData)
{
try
{
string stripeEventType = BL.PaymentGateway.StripeReturn.GetStripeType(invoiceData);
var obj = JObject.Parse(invoiceData);
var dataObj = obj.SelectToken("data.object");
var subscriptionID = dataObj.SelectToken("subscription").ToString();
var customerID = dataObj.SelectToken("customer").ToString();
var chargeID = dataObj.SelectToken("charge").ToString();
var amount = Convert.ToDecimal(!dataObj.SelectToken("subtotal").ToString().IsNullOrEmpty() ?dataObj.SelectToken("subtotal").ToString() : "0");
using (var db = new SchoolEntities())
{
switch (stripeEventType)
{
case "invoice.payment_failed":
var resultorderFailed =
db.OrderItems.Where(oi => oi.MerchantServiceSubscriptionID == subscriptionID)
.Select(oi => new {oi.ID, oi.Order.CreditCard4Digits})
.FirstOrDefault();
if (resultorderFailed != null)
OrderItemInstallment.AddOrderItemInstallment(db, resultorderFailed.ID, amount,
resultorderFailed.CreditCard4Digits, chargeID, false);
return "";
case "invoice.payment_succeeded":
var resultOrderSucceed =
db.OrderItems.Where(oi => oi.MerchantServiceSubscriptionID == subscriptionID)
.Select(oi => new {oi.ID, oi.Order.CreditCard4Digits, oi.TotalInstallments})
.FirstOrDefault();
if (resultOrderSucceed != null)
{
OrderItemInstallment.AddOrderItemInstallment(db, resultOrderSucceed.ID, amount,
resultOrderSucceed.CreditCard4Digits, chargeID, true);
var count =
db.OrderItemInstallments.Count(
oii => oii.OrderItemID == resultOrderSucceed.ID && oii.Success);
if (count == resultOrderSucceed.TotalInstallments)
BL.PaymentGateway.StripePaymentGateway.CancelSubscription(customerID,
subscriptionID);
}
return "";
}
}
}
catch (Exception ex)
{
System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message);
}
return "";
}
Your Stacktrace points to an error with a StreamWriter
. According to your code, there is only one occurence of StreamWriter:
catch (Exception ex)
{
System.IO.File.WriteAllText(@"D:\exception.txt", ex.Message);
}
This means, an exception occurs and your catch block wants to log this exception to your local drive. This causes another exception, because there possibly is no drive D. Logically, after you solved this exception, there will be another exception, which your catch block will then properly log.
Check your test machine, if a drive D is present (and it is no cd drive) and that your IIS user account has appropriate permissions.
On a sidenote: I wouldn't catch exception this way. This just pollutes a text file with error messages, without a stacktrace, timestamps etc. You could levarage one of the many Logging Frameworks already out there (NLOG for example) and make your live way easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With