如何从我的asp.net网站重定向到authorized.net付款网关的付款形式

时间:2020-05-30 12:06:46

标签: c# asp.net

我创建了一个asp.net网络表单。当我单击该按钮时,表单上有一个按钮,我需要重定向带有金额的超额付款形式授权付款网关,并且需要捕获具有交易ID的响应,但我不知道如何通过网关重定向。

我阅读了他们的开发人员文档,并且知道需要将令牌与URL传递并生成令牌,这需要客户个人资料ID。所以我需要了解如何生成正确的令牌。

我正在使用以下代码生成令牌:

//build a path to IframeCommunicator from the calling page
string communicatorPath = String.Format("{0}://{1}:{2}", callingPage.Scheme, callingPage.Host, callingPage.Port);
string[] segments = callingPage.Segments;
//replace the very last entry with contentx/IframeCommunicator.html
segments[segments.GetUpperBound(0)] = "/contentx/IframeCommunicator.html";
foreach(string s in segments)
communicatorPath += s;

string requestXML = String.Format("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
 "<getHostedProfilePageRequest xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\">" +
 "<merchantAuthentication>" +
 "<name>{0}</name>" +
 "<transactionKey>{1}</transactionKey>" +
 "</merchantAuthentication>" +
 "<customerProfileId>{2}</customerProfileId>" +
 "<hostedProfileSettings>" +
 "<setting>" +
 "<settingName>hostedProfilePageBorderVisible</settingName>" +
 "<settingValue>false</settingValue>" +
 "</setting>" +
 "<setting>" +
 "<settingName>hostedProfileIFrameCommunicatorUrl</settingName>" +
 "<settingValue>{3}</settingValue>" +
 "</setting>" +
 "</hostedProfileSettings>" +
 "</getHostedProfilePageRequest>",
 "API_LOGIN_ID", "TRANSACTION_KEY", CustomerProfileId, communicatorPath);

string XMLURL = "https://apitest.authorize.net/xml/v1/request.api";
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(XMLURL);
req.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
req.KeepAlive = false;
req.Timeout = 30000; //30 seconds
req.Method = "POST";
byte[] byte1 = null;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte1 = encoding.GetBytes(requestXML);
req.ContentType = "text/xml";
req.ContentLength = byte1.Length;
System.IO.Stream reqStream = req.GetRequestStream();
reqStream.Write(byte1, 0, byte1.Length);
reqStream.Close();

System.Net.WebResponse resp = req.GetResponse();
System.IO.Stream read = resp.GetResponseStream();
System.IO.StreamReader io = new System.IO.StreamReader(read, new System.Text.ASCIIEncoding());
string data = io.ReadToEnd();
resp.Close();

//parse out value
XmlDocument doc = new XmlDocument();
doc.LoadXml(data);
XmlNodeList m_AccessToken = doc.GetElementsByTagName("token");
return m_AccessToken[0].InnerText;

并使用以下代码生成客户资料ID:

 // set whether to use the sandbox environment, or production enviornment
 ApiOperationBase < ANetApiRequest, ANetApiResponse > .RunEnvironment = AuthorizeNet.Environment.SANDBOX;

 // define the merchant information (authentication / transaction id)
 ApiOperationBase < ANetApiRequest, ANetApiResponse > .MerchantAuthentication = new merchantAuthenticationType() {

  name = ApiLoginID,
   ItemElementName = ItemChoiceType.transactionKey,
   Item = ApiTransactionKey,
 };

 //standard api call to retrieve response
 paymentType cc = new paymentType {
  Item = "Credit Card"
 };
 paymentType echeck = new paymentType {
  Item = "Credit Card"
 };

 List < customerPaymentProfileType > paymentProfileList = new List < customerPaymentProfileType > ();
 customerPaymentProfileType ccPaymentProfile = new customerPaymentProfileType();
 ccPaymentProfile.payment = cc;

 customerPaymentProfileType echeckPaymentProfile = new customerPaymentProfileType();
 echeckPaymentProfile.payment = echeck;

 paymentProfileList.Add(ccPaymentProfile);
 paymentProfileList.Add(echeckPaymentProfile);

 List < customerAddressType > addressInfoList = new List < customerAddressType > ();
 customerAddressType homeAddress = new customerAddressType();
 homeAddress.address = "10900 NE 8th St";
 homeAddress.city = "Seattle";
 homeAddress.zip = "98006";


 customerAddressType officeAddress = new customerAddressType();
 officeAddress.address = "1200 148th AVE NE";
 officeAddress.city = "NorthBend";
 officeAddress.zip = "92101";

 addressInfoList.Add(homeAddress);
 addressInfoList.Add(officeAddress);


 customerProfileType customerProfile = new customerProfileType();
 // customerProfile.merchantCustomerId = "22";
 customerProfile.email = emailId;
 customerProfile.paymentProfiles = paymentProfileList.ToArray();
 //customerProfile.shipToList = addressInfoList.ToArray();

 var request = new createCustomerProfileRequest {
  profile = customerProfile, validationMode = validationModeEnum.none
 };

 //instantiate the controller that will call the service
 var controller = new createCustomerProfileController(request);
 controller.Execute();
 //get the response from the service (errors contained if any)
 createCustomerProfileResponse response = controller.GetApiResponse();

 // validate response 
 if (response != null) {
  if (response.messages.resultCode == messageTypeEnum.Ok) {
   if (response.messages.message != null) {

    Console.WriteLine("Customer Profile ID: " + response.customerProfileId);
    Console.WriteLine("Payment Profile ID: " + response.customerPaymentProfileIdList[0]);
    Console.WriteLine("Shipping Profile ID: " + response.customerShippingAddressIdList[0]);
   }
  } else {
   Console.WriteLine("Customer Profile Creation Failed.");
   Console.WriteLine("Error Code: " + response.messages.message[0].code);
   Console.WriteLine("Error message: " + response.messages.message[0].text);
  }
 } else {
  if (controller.GetErrorResponse().messages.message.Length > 0) {
   Console.WriteLine("Customer Profile Creation Failed.");
   Console.WriteLine("Error Code: " + response.messages.message[0].code);
   Console.WriteLine("Error message: " + response.messages.message[0].text);
  } else {
   Console.WriteLine("Null Response.");
  }
 }

 return response;

但是代码不起作用。

0 个答案:

没有答案