我正在使用HttpWebRequest
将付款详细信息传达给付款处理商并收到回复。
当命中封装此请求的方法时,模型将被传入,但一旦请求发生,它就会突然变为空。也就是说,这种情况发生在谷歌浏览器中,但我确定这里存在更深层次的问题。
我也只在生产服务器上观察到这种行为,但我无法在localhost下调试产品。
对于那些使用过付款处理器的人,我使用了类似的方法将付款发布到Authorize.Net而没有问题。也许这是一个与如何将信息发布到另一台服务器以及采用何种格式相关的问题。
查看模型
public class PaymentModel
{
// --------------------
// Patient Information
// --------------------
[Required(ErrorMessage = "First Name is required")]
public string PatientFirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string PatientLastName { get; set; }
public bool EmailReceipt { get; set; }
[EmailAddress(ErrorMessage = "A valid email address is required if a receipt is to be emailed")]
public string EmailAddress { get; set; }
[DataType(DataType.Date, ErrorMessage = "A valid Date Of Service is required (mm/dd/yyyy)")]
[Required(ErrorMessage = "A valid Date Of Service is required")]
public DateTime DateOfService { get; set; }
public string BillCode { get; set; }
// Company Specific information
public static string AccountNumberPrefix = "xx";
public static string CompanyAccountNumber = "xxx";
public static string CompanyName = "xxx";
[Required(ErrorMessage = "Account Number is required")]
public string AccountNumber { get; set; }
public string PhoneNumber { get; set; }
// ---------------------
// Payment Information
// ---------------------
[Required(ErrorMessage = "Name on Card is required")]
public string NameOnCard { get; set; }
[Required(ErrorMessage = "Card Billing Address is required")]
public string BillingAddress { get; set; }
[Required(ErrorMessage = "Card Billing Zipcode in required")]
public string BillingZipCode { get; set; }
public string CardType { get; set; }
[Required(ErrorMessage = "A valid Credit Card Number is required")]
public string CardNumber { get; set; }
[Required(ErrorMessage = "CSC/CVN is required")]
public string SecurityCode { get; set; }
public string ExpireMonth { get; set; }
public string ExpireYear { get; set; }
[Required(ErrorMessage = "A valid payment amount is required")]
[RegularExpression(@"^(?=.*\d)\d{0,6}(\.\d{1,2})?$", ErrorMessage = "Invalid format - Allowed: x.xx (Number/Decimal)")]
public string PaymentAmount { get; set; }
// -----------------
// Static Options
// -----------------
// CC Expire Month Options
public static List<SelectListItem> ExpMonthOptions = new List<SelectListItem>()
{
new SelectListItem() {Text="01", Value="01"},
new SelectListItem() {Text="02", Value="02"},
new SelectListItem() {Text="03", Value="03"},
new SelectListItem() {Text="04", Value="04"},
new SelectListItem() {Text="05", Value="05"},
new SelectListItem() {Text="06", Value="06"},
new SelectListItem() {Text="07", Value="07"},
new SelectListItem() {Text="08", Value="08"},
new SelectListItem() {Text="09", Value="09"},
new SelectListItem() {Text="10", Value="10"},
new SelectListItem() {Text="11", Value="11"},
new SelectListItem() {Text="12", Value="12"}
};
// CC Expire Year Options
public static List<SelectListItem> ExpYearOptions = new List<SelectListItem>()
{
new SelectListItem() {Text=DateTime.Now.Year.ToString(), Value=DateTime.Now.Year.ToString()},
new SelectListItem() {Text=DateTime.Now.AddYears(1).ToString("yyyy"), Value=DateTime.Now.AddYears(1).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(2).ToString("yyyy"), Value=DateTime.Now.AddYears(2).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(3).ToString("yyyy"), Value=DateTime.Now.AddYears(3).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(4).ToString("yyyy"), Value=DateTime.Now.AddYears(4).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(5).ToString("yyyy"), Value=DateTime.Now.AddYears(5).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(6).ToString("yyyy"), Value=DateTime.Now.AddYears(6).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(7).ToString("yyyy"), Value=DateTime.Now.AddYears(7).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(8).ToString("yyyy"), Value=DateTime.Now.AddYears(8).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(9).ToString("yyyy"), Value=DateTime.Now.AddYears(9).ToString("yyyy")},
new SelectListItem() {Text=DateTime.Now.AddYears(10).ToString("yyyy"), Value=DateTime.Now.AddYears(10).ToString("yyyy")}
};
// ------------------
// Payment Processor
// ------------------
public string referenceNumber { get; set; }
public string processorError { get; set; }
}
控制器
// POST: /Payment/Confirmation
[HttpPost]
public ActionResult Confirmation(string restartBtn, string prevBtn, string nextBtn)
{
try
{
// Get current payment session
PaymentModel paymentSession = GetPayment();
// Return to previous step
if (prevBtn != null)
{
// Ensure there are no false processor errors
paymentSession.processorError = string.Empty;
return View("index", paymentSession);
}
// Proceed to process and next step
if (nextBtn != null)
{
// Initialize Transaction Reference
paymentSession.referenceNumber = PaymentUtilities.GenerateReferenceNumber(15, false);
// Initialize new transaction object for Authorize.Net
MerchantOne merchantName = new MerchantOne(
"xxxx", // User Name
"xxxx" // Password
);
// Perform the transaction and get the response
var response = merchantName.Charge(paymentSession);
// Store the initial response
_repository.InsertTransaction(response);
// Was the payment declined?
if (response.IsError)
{
paymentSession.processorError = response.Message;
return View("index", paymentSession);
}
// Store successful payment details
_repository.InsertPayment(paymentSession, response);
// Did the user elect to receive a receipt?
if (paymentSession.EmailReceipt)
{
// Email receipt
EmailReceipt(paymentSession, response);
}
return View("receipt", paymentSession);
}
// Clear payment session and return to first step
if (restartBtn != null)
{
RemovePayment();
return View("index");
}
}
catch (Exception ex)
{
EmailError(ex);
return View("Error");
}
return View();
}
处理交易的类(发送HttpWebRequest
)
public class MerchantOne
{
private readonly string _userName;
private readonly string _password;
// POST URLS
private const string postURL = "https://secure.merchantonegateway.com/api/transact.php?";
private const string testURL = "";
public MerchantOne(string username, string password)
{
_userName = username;
_password = password;
}
public MerchantResponse Charge(PaymentModel paymentSession)
{
// Prepare and assign post values
Dictionary<string, string> postValues = new Dictionary<string, string>();
// Merchant Information
postValues.Add("username", _userName);
postValues.Add("password", _password);
postValues.Add("type", "sale");
// Transaction Information
postValues.Add("orderid", paymentSession.referenceNumber);
postValues.Add("ccnumber", paymentSession.CardNumber);
postValues.Add("ccexp", paymentSession.ExpireMonth + paymentSession.ExpireYear.Substring(2, 2));
postValues.Add("cvv", paymentSession.SecurityCode);
postValues.Add("amount", paymentSession.PaymentAmount.Replace("$", "").Replace(",", "").Trim());
postValues.Add("address1", paymentSession.BillingAddress);
postValues.Add("zip", paymentSession.BillingZipCode);
// Convert the fields into http POST format
String postString = String.Empty;
foreach (KeyValuePair<string, string> postValue in postValues)
{
postString += postValue.Key + "=" + HttpUtility.UrlEncode(postValue.Value) + "&";
}
postString = postString.TrimEnd('&');
// Create an HttpWebRequest object for communication with Merchant One
HttpWebRequest objRequest;
objRequest = (HttpWebRequest)WebRequest.Create(postURL);
objRequest.Method = "POST";
objRequest.ContentLength = postString.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
// POST the data as stream
StreamWriter streamWriter = null;
streamWriter = new StreamWriter(objRequest.GetRequestStream());
streamWriter.Write(postString);
streamWriter.Close();
// Returned values are returned as a stream, then read into a string
String postResponse;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
postResponse = responseStream.ReadToEnd();
responseStream.Close();
}
// Prepare new response object
MerchantResponse response = new MerchantResponse();
// ----------------------------------------------------------------
// Fill response properties with gateway response array indexes
//-----------------------------------------------------------------
response.RawResponseData = postResponse; // Capture the entire response string in raw format
string[] responseArray = postResponse.Split('&');
response.MOresponse = responseArray[0].Split('=')[1];
response.MOresponseText = responseArray[1].Split('=')[1];
response.MOauthCode = responseArray[2].Split('=')[1];
response.MOtransactionId = responseArray[3].Split('=')[1];
response.MOavsResponse = responseArray[4].Split('=')[1];
response.MOcvvResponse = responseArray[5].Split('=')[1];
response.MOorderId = responseArray[6].Split('=')[1];
response.MOtype = responseArray[7].Split('=')[1];
response.MOresponse_Code = responseArray[8].Split('=')[1];
// Add payment method to response based on CC number
response.MOpaymentType = ResponsePaymentTypePerCC(paymentSession.CardNumber);
// Add name on card from payment field for transaction storage
response.MOnameOnCard = paymentSession.NameOnCard;
// Add Transaction Amount from payment field for transaction storage
response.MOtransactionAmt = paymentSession.PaymentAmount.Replace("$", "").Replace(",", "").Trim();
// Transaction result
response.IsTransactionApproved = response.MOresponse == "1"; // Transaction Approved?
response.IsError = response.MOresponse != "1"; // Transaction Failed (i.e. Declined)?
response.Message = ResponseReasonPerCode(response.MOresponse_Code, response.MOresponseText); // Reason for response
// Return response object
return response;
}
private string ResponseReasonPerCode(string responseCode, string responseText)
{
string responseReason;
switch (responseCode)
{
case "200":
responseReason = "Transaction was declined by processor.";
break;
case "201":
responseReason = "Transaction denied. Do not honor.";
break;
case "202":
responseReason = "Insufficient Funds.";
break;
case "203":
responseReason = "Over Limit.";
break;
case "204":
responseReason = "Transaction not allowed.";
break;
case "220":
responseReason = "Incorrect payment data.";
break;
case "221":
responseReason = "No such card issuer.";
break;
case "222":
responseReason = "No such card number on file with issuer.";
break;
case "223":
responseReason = "Card has expired.";
break;
case "224":
responseReason = "Invalid expiration date.";
break;
case "225":
responseReason = "Invalid card security code (CVV).";
break;
case "240":
responseReason = "Call issuer for further information.";
break;
case "250":
responseReason = "Pick up card.";
break;
case "251":
responseReason = "Lost card.";
break;
case "252":
responseReason = "Stolen card.";
break;
case "253":
responseReason = "Fraudulent card.";
break;
case "260":
responseReason = "Transaction declined (260): " + responseText;
break;
case "430":
responseReason = "Duplicate transaction.";
break;
default:
responseReason = "Unable to process payment. Error code " + responseText;
break;
}
return responseReason;
}
private string ResponsePaymentTypePerCC(string ccNumber)
{
string paymentType = string.Empty;
switch (ccNumber.Trim().Substring(0, 1))
{
case "3":
paymentType = "AMEX";
break;
case "4":
paymentType = "VISA";
break;
case "5":
paymentType = "MC";
break;
case "6":
paymentType = "DISC";
break;
}
return paymentType;
}
}
错误消息是这个..
System.NullReferenceException: Object reference not set to an instance of an object. at namespace.MerchantOne.Charge(PaymentModel paymentSession) at namespace.Controllers.PaymentController.Confirmation(String restartBtn, String prevBtn, String nextBtn)
老实说,它可能是另一个对象变为空或未被初始化但是现在,我能想到的是我的PaymentModel
在使用后迷失了。
感谢任何帮助。
答案 0 :(得分:0)
我有类似的问题。我从paypal获得了购物车物品和其他金额的零响应。当接收到我正在使用IP地址的响应时输出,但在发送请求时我使用的是localhost。你能否检查一下这不是问题。
答案 1 :(得分:0)
所以,事实证明...... Chrome浏览器完全愚蠢。 这一整天我一直在敲打我的脑袋。我的代码很好,服务器上的会话设置很好。
在woff / woff2文件中找不到404。我在我的项目中使用了font-awesome,它正在寻找的两个资源抛出404 /找不到,这就是我的asp.net会话。
这是最终解决了我的问题...将.woff和.woff2 MIME类型添加到我的服务器。
Why is @font-face throwing a 404 error on woff files?
感谢所有试图提供帮助的人。