· 7 years ago · Oct 06, 2018, 10:32 PM
1
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Web;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Net;
9using System.Configuration;
10using System.IO;
11using System.Xml;
12using System.Text;
13using System.Web.Script.Serialization;
14using System.Drawing;
15using System.Net.Security;
16using System.Security.Cryptography.X509Certificates;
17using System.Text.RegularExpressions;
18
19public partial class _Default : System.Web.UI.Page
20{
21 string shortCode, accessTokenFilePath, FQDN, oauthFlow, subsDetailsFile, subsRefundFile;
22 string api_key, secret_key, auth_code, access_token, authorize_redirect_uri, scope, expiryMilliSeconds, refresh_token, lastTokenTakenTime, refreshTokenExpiryTime;
23 Table successTable, failureTable;
24 Table successTableGetTransaction, failureTableGetTransaction, successTableGetSubscriptionDetails;
25 string amount;
26 Int32 category;
27 string channel, description, merchantTransactionId, merchantProductId, merchantApplicationId;
28 Uri merchantRedirectURI;
29 string MerchantSubscriptionIdList, SubscriptionRecurringPeriod;
30 string SubscriptionRecurringNumber, SubscriptionRecurringPeriodAmount;
31 string IsPurchaseOnNoActiveSubscription;
32 DateTime transactionTime;
33 string transactionTimeString;
34 string payLoadStringFromRequest;
35 string signedPayload, signedSignature;
36 string notaryURL;
37 //string consumerId;
38 int subsDetailsCountToDisplay = 0;
39 List<KeyValuePair<string, string>> subsDetailsList = new List<KeyValuePair<string, string>>();
40 List<KeyValuePair<string, string>> subsRefundList = new List<KeyValuePair<string, string>>();
41 bool LatestFive = true;
42 protected void Page_Load(object sender, EventArgs e)
43 {
44 subsRefundSuccessTable.Visible = false;
45 subsDetailsSuccessTable.Visible = false;
46 subscriptionSuccessTable.Visible = false;
47 subsGetStatusTable.Visible = false;
48 DateTime currentServerTime = DateTime.UtcNow;
49 lblServerTime.Text = String.Format("{0:ddd, MMM dd, yyyy HH:mm:ss}", currentServerTime) + " UTC";
50 if (ConfigurationManager.AppSettings["FQDN"] == null)
51 {
52 drawPanelForFailure(newSubscriptionPanel, "FQDN is not defined in configuration file");
53 return;
54 }
55 FQDN = ConfigurationManager.AppSettings["FQDN"].ToString();
56 if (ConfigurationManager.AppSettings["api_key"] == null)
57 {
58 drawPanelForFailure(newSubscriptionPanel, "api_key is not defined in configuration file");
59 return;
60 }
61 api_key = ConfigurationManager.AppSettings["api_key"].ToString();
62 if (ConfigurationManager.AppSettings["secret_key"] == null)
63 {
64 drawPanelForFailure(newSubscriptionPanel, "secret_key is not defined in configuration file");
65 return;
66 }
67 secret_key = ConfigurationManager.AppSettings["secret_key"].ToString();
68 if (ConfigurationManager.AppSettings["AccessTokenFilePath"] != null)
69 {
70 accessTokenFilePath = ConfigurationManager.AppSettings["AccessTokenFilePath"];
71 }
72 else
73 {
74 accessTokenFilePath = "~\\PayApp2AccessToken.txt";
75 }
76 if (ConfigurationManager.AppSettings["subsDetailsFile"] != null)
77 {
78 subsDetailsFile = ConfigurationManager.AppSettings["subsDetailsFile"];
79 }
80 else
81 {
82 subsDetailsFile = "~\\subsDetailsFile.txt";
83 }
84 if (ConfigurationManager.AppSettings["subsRefundFile"] != null)
85 {
86 subsRefundFile = ConfigurationManager.AppSettings["subsRefundFile"];
87 }
88 else
89 {
90 subsRefundFile = "~\\subsRefundFile.txt";
91 }
92 if (ConfigurationManager.AppSettings["subsDetailsCountToDisplay"] != null)
93 {
94 subsDetailsCountToDisplay = Convert.ToInt32(ConfigurationManager.AppSettings["subsDetailsCountToDisplay"]);
95 }
96 else
97 {
98 subsDetailsCountToDisplay = 5;
99 }
100 if (ConfigurationManager.AppSettings["scope"] == null)
101 {
102 scope = "PAYMENT";
103 }
104 else
105 {
106 scope = ConfigurationManager.AppSettings["scope"].ToString();
107 }
108 /* if (ConfigurationManager.AppSettings["consumerId"] == null)
109 {
110 drawPanelForFailure(newSubscriptionPanel, "consumerId is not defined in configuration file");
111 return;
112 }
113 consumerId = ConfigurationManager.AppSettings["consumerId"].ToString(); */
114 if (ConfigurationManager.AppSettings["notaryURL"] == null)
115 {
116 drawPanelForFailure(newSubscriptionPanel, "notaryURL is not defined in configuration file");
117 return;
118 }
119 if (ConfigurationManager.AppSettings["MerchantPaymentRedirectUrl"] == null)
120 {
121 drawPanelForFailure(newSubscriptionPanel, "MerchantPaymentRedirectUrl is not defined in configuration file");
122 return;
123 }
124 if (ConfigurationManager.AppSettings["DisableLatestFive"] != null)
125 {
126 LatestFive = false;
127 }
128 merchantRedirectURI = new Uri(ConfigurationManager.AppSettings["MerchantPaymentRedirectUrl"]);
129 notaryURL = ConfigurationManager.AppSettings["notaryURL"];
130 if ((Request["ret_signed_payload"] != null) && (Request["ret_signature"] != null))
131 {
132 signedPayload = Request["ret_signed_payload"].ToString();
133 signedSignature = Request["ret_signature"].ToString();
134 Session["sub_signedPayLoad"] = signedPayload.ToString();
135 Session["sub_signedSignature"] = signedSignature.ToString();
136 processNotaryResponse();
137 }
138 else if ((Request["SubscriptionAuthCode"] != null) && (Session["sub_merTranId"] != null))
139 {
140 processCreateTransactionResponse();
141 }
142 else if ((Request["shown_notary"] != null) && (Session["sub_processNotary"] != null))
143 {
144 Session["sub_processNotary"] = null;
145 GetSubscriptionMerchantSubsID.Text = "Merchant Sub. ID: " + Session["sub_tempMerTranId"].ToString();
146 GetSubscriptionAuthCode.Text = "Auth Code: " + Session["sub_TranAuthCode"].ToString();
147 }
148 subsDetailsTable.Controls.Clear();
149 drawSubsDetailsSection(false);
150 subsRefundTable.Controls.Clear();
151 drawSubsRefundSection(false);
152 return;
153 }
154 public void addRowToSubsDetailsSection(string subscription, string merchantsubscription)
155 {
156 TableRow rowOne = new TableRow();
157 TableCell cellOne = new TableCell();
158 cellOne.HorizontalAlign = HorizontalAlign.Right;
159 cellOne.CssClass = "cell";
160 cellOne.Width = Unit.Pixel(150);
161 //cellOne.Text = transaction.ToString();
162 RadioButton rbutton = new RadioButton();
163 rbutton.Text = subscription.ToString();
164 rbutton.GroupName = "SubsDetailsSection";
165 rbutton.ID = subscription.ToString();
166 cellOne.Controls.Add(rbutton);
167 rowOne.Controls.Add(cellOne);
168 TableCell CellTwo = new TableCell();
169 CellTwo.CssClass = "cell";
170 CellTwo.Width = Unit.Pixel(100);
171 rowOne.Controls.Add(CellTwo);
172
173 TableCell CellThree = new TableCell();
174 CellThree.CssClass = "cell";
175 CellThree.HorizontalAlign = HorizontalAlign.Left;
176 CellThree.Width = Unit.Pixel(240);
177 CellThree.Text = merchantsubscription.ToString();
178 rowOne.Controls.Add(CellThree);
179
180 TableCell CellFour = new TableCell();
181 CellFour.CssClass = "cell";
182 rowOne.Controls.Add(CellFour);
183
184 subsDetailsTable.Controls.Add(rowOne);
185 }
186 public void addRowToSubsRefundSection(string subscription, string merchantsubscription)
187 {
188 TableRow rowOne = new TableRow();
189 TableCell cellOne = new TableCell();
190 cellOne.HorizontalAlign = HorizontalAlign.Right;
191 cellOne.CssClass = "cell";
192 cellOne.Width = Unit.Pixel(150);
193 //cellOne.Text = transaction.ToString();
194 RadioButton rbutton = new RadioButton();
195 rbutton.Text = subscription.ToString();
196 rbutton.GroupName = "SubsRefundSection";
197 rbutton.ID = subscription.ToString();
198 cellOne.Controls.Add(rbutton);
199 rowOne.Controls.Add(cellOne);
200 TableCell CellTwo = new TableCell();
201 CellTwo.CssClass = "cell";
202 CellTwo.Width = Unit.Pixel(100);
203 rowOne.Controls.Add(CellTwo);
204
205 TableCell CellThree = new TableCell();
206 CellThree.CssClass = "cell";
207 CellThree.HorizontalAlign = HorizontalAlign.Left;
208 CellThree.Width = Unit.Pixel(240);
209 CellThree.Text = merchantsubscription.ToString();
210 rowOne.Controls.Add(CellThree);
211
212 TableCell CellFour = new TableCell();
213 CellFour.CssClass = "cell";
214 rowOne.Controls.Add(CellFour);
215
216 subsRefundTable.Controls.Add(rowOne);
217 }
218 public void drawSubsDetailsSection(bool onlyRow)
219 {
220 try
221 {
222 if (onlyRow == false)
223 {
224 TableRow headingRow = new TableRow();
225 TableCell headingCellOne = new TableCell();
226 headingCellOne.HorizontalAlign = HorizontalAlign.Right;
227 headingCellOne.CssClass = "cell";
228 headingCellOne.Width = Unit.Pixel(200);
229 headingCellOne.Font.Bold = true;
230 headingCellOne.Text = "Merchant Subscription ID";
231 headingRow.Controls.Add(headingCellOne);
232 TableCell headingCellTwo = new TableCell();
233 headingCellTwo.CssClass = "cell";
234 headingCellTwo.Width = Unit.Pixel(100);
235 headingRow.Controls.Add(headingCellTwo);
236 TableCell headingCellThree = new TableCell();
237 headingCellThree.CssClass = "cell";
238 headingCellThree.HorizontalAlign = HorizontalAlign.Left;
239 headingCellThree.Width = Unit.Pixel(240);
240 headingCellThree.Font.Bold = true;
241 headingCellThree.Text = "Consumer ID";
242 headingRow.Controls.Add(headingCellThree);
243 TableCell headingCellFour = new TableCell();
244 headingCellFour.CssClass = "warning";
245 LiteralControl warningMessage = new LiteralControl("<b>WARNING:</b><br/>You must use Get Subscription Status before you can view details of it.");
246 headingCellFour.Controls.Add(warningMessage);
247 headingRow.Controls.Add(headingCellFour);
248 subsDetailsTable.Controls.Add(headingRow);
249 }
250 resetSubsDetailsList();
251 getSubsDetailsFromFile();
252
253 int tempCountToDisplay = 1;
254 while ((tempCountToDisplay <= subsDetailsCountToDisplay) && (tempCountToDisplay <= subsDetailsList.Count) && (subsDetailsList.Count > 0))
255 {
256 addRowToSubsDetailsSection(subsDetailsList[tempCountToDisplay - 1].Key, subsDetailsList[tempCountToDisplay - 1].Value);
257 tempCountToDisplay++;
258 }
259 //addButtonToRefundSection("Refund Transaction");
260 }
261 catch (Exception ex)
262 {
263 drawPanelForFailure(subsDetailsPanel, ex.ToString());
264 }
265 }
266 public void drawSubsRefundSection(bool onlyRow)
267 {
268 try
269 {
270 if (onlyRow == false)
271 {
272 TableRow headingRow = new TableRow();
273 TableCell headingCellOne = new TableCell();
274 headingCellOne.HorizontalAlign = HorizontalAlign.Right;
275 headingCellOne.CssClass = "cell";
276 headingCellOne.Width = Unit.Pixel(200);
277 headingCellOne.Font.Bold = true;
278 headingCellOne.Text = "Subscription ID";
279 headingRow.Controls.Add(headingCellOne);
280 TableCell headingCellTwo = new TableCell();
281 headingCellTwo.CssClass = "cell";
282 headingCellTwo.Width = Unit.Pixel(100);
283 headingRow.Controls.Add(headingCellTwo);
284 TableCell headingCellThree = new TableCell();
285 headingCellThree.CssClass = "cell";
286 headingCellThree.HorizontalAlign = HorizontalAlign.Left;
287 headingCellThree.Width = Unit.Pixel(240);
288 headingCellThree.Font.Bold = true;
289 headingCellThree.Text = "Merchant Subscription ID";
290 headingRow.Controls.Add(headingCellThree);
291 TableCell headingCellFour = new TableCell();
292 headingCellFour.CssClass = "warning";
293 LiteralControl warningMessage = new LiteralControl("<b>WARNING:</b><br/>You must use Get Subscription Status before you can refund.");
294 headingCellFour.Controls.Add(warningMessage);
295 headingRow.Controls.Add(headingCellFour);
296 subsRefundTable.Controls.Add(headingRow);
297 }
298 resetSubsRefundList();
299 getSubsRefundFromFile();
300
301 int tempCountToDisplay = 1;
302 while ((tempCountToDisplay <= subsDetailsCountToDisplay) && (tempCountToDisplay <= subsRefundList.Count) && (subsRefundList.Count > 0))
303 {
304 addRowToSubsRefundSection(subsRefundList[tempCountToDisplay - 1].Key, subsRefundList[tempCountToDisplay - 1].Value);
305 tempCountToDisplay++;
306 }
307 }
308 catch (Exception ex)
309 {
310 drawPanelForFailure(subsRefundPanel, ex.ToString());
311 }
312 }
313 public string getValueOfKeyFromRefund(string key)
314 {
315 int tempCount = 0;
316 while (tempCount < subsDetailsList.Count)
317 {
318 if (subsRefundList[tempCount].Key.CompareTo(key) == 0)
319 return subsRefundList[tempCount].Value;
320 tempCount++;
321 }
322 return "null";
323 }
324 public string getValueOfKey( string key)
325 {
326 int tempCount = 0;
327 while (tempCount < subsDetailsList.Count)
328 {
329 if ( subsDetailsList[tempCount].Key.CompareTo(key) == 0)
330 return subsDetailsList[tempCount].Value;
331 tempCount++;
332 }
333 return "null";
334 }
335 public void resetSubsRefundList()
336 {
337 subsRefundList.RemoveRange(0, subsRefundList.Count);
338 }
339 public void resetSubsDetailsList()
340 {
341 subsDetailsList.RemoveRange(0, subsDetailsList.Count);
342 }
343 public void getSubsDetailsFromFile()
344 {
345 /* Read the refund file for the list of transactions and store locally */
346 FileStream file = new FileStream(Request.MapPath(subsDetailsFile), FileMode.Open, FileAccess.Read);
347 StreamReader sr = new StreamReader(file);
348 string line;
349
350 while (((line = sr.ReadLine()) != null))
351 {
352 string[] subsDetailsKeys = Regex.Split(line, ":-:");
353 if (subsDetailsKeys[0] != null && subsDetailsKeys[1] != null)
354 {
355 subsDetailsList.Add(new KeyValuePair<string, string>(subsDetailsKeys[0], subsDetailsKeys[1]));
356 }
357 }
358 sr.Close();
359 file.Close();
360 subsDetailsList.Reverse(0, subsDetailsList.Count);
361 }
362 public void getSubsRefundFromFile()
363 {
364 /* Read the refund file for the list of transactions and store locally */
365 FileStream file = new FileStream(Request.MapPath(subsRefundFile), FileMode.Open, FileAccess.Read);
366 StreamReader sr = new StreamReader(file);
367 string line;
368
369 while (((line = sr.ReadLine()) != null))
370 {
371 string[] subsRefundKeys = Regex.Split(line, ":-:");
372 if (subsRefundKeys[0] != null && subsRefundKeys[1] != null)
373 {
374 subsRefundList.Add(new KeyValuePair<string, string>(subsRefundKeys[0], subsRefundKeys[1]));
375 }
376 }
377 sr.Close();
378 file.Close();
379 subsRefundList.Reverse(0, subsRefundList.Count);
380 }
381 public void updatesubsRefundListToFile()
382 {
383 if (subsRefundList.Count != 0)
384 subsRefundList.Reverse(0, subsRefundList.Count);
385 using (StreamWriter sr = File.CreateText(Request.MapPath(subsRefundFile)))
386 {
387 int tempCount = 0;
388 while (tempCount < subsRefundList.Count)
389 {
390 string lineToWrite = subsRefundList[tempCount].Key + ":-:" + subsRefundList[tempCount].Value;
391 sr.WriteLine(lineToWrite);
392 tempCount++;
393 }
394 sr.Close();
395 }
396 }
397 public void updatesubsDetailsListToFile()
398 {
399 if (subsDetailsList.Count != 0)
400 subsDetailsList.Reverse(0, subsDetailsList.Count);
401 using (StreamWriter sr = File.CreateText(Request.MapPath(subsDetailsFile)))
402 {
403 int tempCount = 0;
404 while (tempCount < subsDetailsList.Count)
405 {
406 string lineToWrite = subsDetailsList[tempCount].Key + ":-:" + subsDetailsList[tempCount].Value;
407 sr.WriteLine(lineToWrite);
408 tempCount++;
409 }
410 sr.Close();
411 }
412 }
413 public bool checkItemInSubsRefundFile(string transactionid, string merchantTransactionId)
414 {
415 string line;
416 string lineToFind = transactionid + ":-:" + merchantTransactionId;
417 System.IO.StreamReader file = new System.IO.StreamReader(Request.MapPath(subsRefundFile));
418 while ((line = file.ReadLine()) != null)
419 {
420 if (line.CompareTo(lineToFind) == 0)
421 {
422 file.Close();
423 return true;
424 }
425 }
426 file.Close();
427 return false;
428 }
429 public bool checkItemInSubsDetailsFile(string transactionid, string merchantTransactionId)
430 {
431 string line;
432 string lineToFind = transactionid + ":-:" + merchantTransactionId;
433 System.IO.StreamReader file = new System.IO.StreamReader(Request.MapPath(subsDetailsFile));
434 while ((line = file.ReadLine()) != null)
435 {
436 if (line.CompareTo(lineToFind) == 0)
437 {
438 file.Close();
439 return true;
440 }
441 }
442 file.Close();
443 return false;
444 }
445 public void writeSubsRefundToFile(string transactionid, string merchantTransactionId)
446 {
447 /* Read the refund file for the list of transactions and store locally */
448 //FileStream file = new FileStream(Request.MapPath(refundFile), FileMode.Append, FileAccess.Write);
449 //StreamWriter sr = new StreamWriter(file);
450 //DateTime junkTime = DateTime.UtcNow;
451 //string junkTimeString = String.Format("{0:dddMMMddyyyyHHmmss}", junkTime);
452 using (StreamWriter appendContent = File.AppendText(Request.MapPath(subsRefundFile)))
453 {
454 string line = transactionid + ":-:" + merchantTransactionId;
455 appendContent.WriteLine(line);
456 appendContent.Flush();
457 appendContent.Close();
458 //file.Close();
459 }
460 }
461
462 public void writeSubsDetailsToFile(string transactionid, string merchantTransactionId)
463 {
464 /* Read the refund file for the list of transactions and store locally */
465 //FileStream file = new FileStream(Request.MapPath(refundFile), FileMode.Append, FileAccess.Write);
466 //StreamWriter sr = new StreamWriter(file);
467 //DateTime junkTime = DateTime.UtcNow;
468 //string junkTimeString = String.Format("{0:dddMMMddyyyyHHmmss}", junkTime);
469 using (StreamWriter appendContent = File.AppendText(Request.MapPath(subsDetailsFile)))
470 {
471 string line = transactionid + ":-:" + merchantTransactionId;
472 appendContent.WriteLine(line);
473 appendContent.Flush();
474 appendContent.Close();
475 //file.Close();
476 }
477 }
478
479 private void readTransactionParametersFromConfigurationFile()
480 {
481 transactionTime = DateTime.UtcNow;
482 transactionTimeString = String.Format("{0:dddMMMddyyyyHHmmss}", transactionTime);
483 if (Radio_SubscriptionProductType.SelectedIndex == 0)
484 amount = "1.99";
485 else if (Radio_SubscriptionProductType.SelectedIndex == 1)
486 amount = "3.99";
487 Session["sub_tranType"] = Radio_SubscriptionProductType.SelectedIndex.ToString();
488 if (ConfigurationManager.AppSettings["Category"] == null)
489 {
490 drawPanelForFailure(newSubscriptionPanel, "Category is not defined in configuration file");
491 return;
492 }
493 category = Convert.ToInt32(ConfigurationManager.AppSettings["Category"]);
494 if (ConfigurationManager.AppSettings["Channel"] == null)
495 {
496 channel = "MOBILE_WEB";
497 }
498 else
499 {
500 channel = ConfigurationManager.AppSettings["Channel"];
501 }
502 description = "TrDesc" + transactionTimeString;
503 merchantTransactionId = "TrId" + transactionTimeString;
504 Session["sub_merTranId"] = merchantTransactionId.ToString();
505 merchantProductId = "ProdId" + transactionTimeString;
506 merchantApplicationId = "MerAppId" + transactionTimeString;
507 MerchantSubscriptionIdList = "MSIList" + transactionTimeString;
508 Session["MerchantSubscriptionIdList"] = MerchantSubscriptionIdList.ToString();
509 IsPurchaseOnNoActiveSubscription = ConfigurationManager.AppSettings["IsPurchaseOnNoActiveSubscription"];
510 if (ConfigurationManager.AppSettings["IsPurchaseOnNoActiveSubscription"] == null)
511 {
512 IsPurchaseOnNoActiveSubscription = "false";
513 }
514 else
515 {
516 IsPurchaseOnNoActiveSubscription = ConfigurationManager.AppSettings["IsPurchaseOnNoActiveSubscription"];
517 }
518 if (ConfigurationManager.AppSettings["SubscriptionRecurringNumber"] == null)
519 {
520 SubscriptionRecurringNumber = "99999";
521 }
522 else
523 {
524 SubscriptionRecurringNumber = ConfigurationManager.AppSettings["SubscriptionRecurringNumber"];
525 }
526 if (ConfigurationManager.AppSettings["SubscriptionRecurringPeriod"] == null)
527 {
528 SubscriptionRecurringPeriod = "MONTHLY";
529 }
530 else
531 {
532 SubscriptionRecurringPeriod = ConfigurationManager.AppSettings["SubscriptionRecurringPeriod"];
533 }
534 if (ConfigurationManager.AppSettings["SubscriptionRecurringPeriodAmount"] == null)
535 {
536 SubscriptionRecurringPeriodAmount = "1";
537 }
538 else
539 {
540 SubscriptionRecurringPeriodAmount = ConfigurationManager.AppSettings["SubscriptionRecurringPeriodAmount"];
541 }
542 }
543
544
545 private void processNotaryResponse()
546 {
547 if (Session["sub_tranType"] != null)
548 {
549 Radio_SubscriptionProductType.SelectedIndex = Convert.ToInt32(Session["sub_tranType"].ToString());
550 Session["sub_tranType"] = null;
551 }
552 Response.Redirect(FQDN + "/Commerce/Payment/Rest/2/Subscriptions?clientid=" + api_key.ToString() + "&SignedPaymentDetail=" + signedPayload.ToString() + "&Signature=" + signedSignature.ToString());
553
554 }
555
556 public void processCreateTransactionResponse()
557 {
558 lblsubscode.Text = Request["SubscriptionAuthCode"].ToString();
559 lblsubsid.Text = Session["sub_merTranId"].ToString();
560 subscriptionSuccessTable.Visible = true;
561 GetSubscriptionMerchantSubsID.Text = "Merchant Sub. ID: " + Session["sub_merTranId"].ToString();
562 GetSubscriptionAuthCode.Text = "Auth Code: " + Request["SubscriptionAuthCode"].ToString();
563 GetSubscriptionID.Text = "Subscription ID: ";
564 Session["sub_tempMerTranId"] = Session["sub_merTranId"].ToString();
565 Session["sub_merTranId"] = null;
566 Session["sub_TranAuthCode"] = Request["SubscriptionAuthCode"].ToString();
567 return;
568 }
569
570 /* This function draws the success table */
571 private void drawPanelForSuccess(Panel panelParam)
572 {
573 successTable = new Table();
574 successTable.Font.Name = "Sans-serif";
575 successTable.Font.Size = 8;
576 successTable.BorderStyle = BorderStyle.Outset;
577 successTable.Width = Unit.Pixel(650);
578 TableRow rowOne = new TableRow();
579 TableCell rowOneCellOne = new TableCell();
580 rowOneCellOne.Font.Bold = true;
581 rowOneCellOne.Text = "SUCCESS:";
582 rowOne.Controls.Add(rowOneCellOne);
583 successTable.Controls.Add(rowOne);
584 successTable.BorderWidth = 2;
585 successTable.BorderColor = Color.DarkGreen;
586 successTable.BackColor = System.Drawing.ColorTranslator.FromHtml("#cfc");
587 panelParam.Controls.Add(successTable);
588 }
589 /*This function adds row to the success table */
590 private void addRowToSuccessPanel(Panel panelParam, string attribute, string value)
591 {
592 TableRow row = new TableRow();
593 TableCell cellOne = new TableCell();
594 cellOne.Text = attribute.ToString();
595 cellOne.Font.Bold = true;
596 row.Controls.Add(cellOne);
597 TableCell cellTwo = new TableCell();
598 cellTwo.Text = value.ToString();
599 row.Controls.Add(cellTwo);
600 successTable.Controls.Add(row);
601 }
602 /* This function draws error table */
603 private void drawPanelForFailure(Panel panelParam, string message)
604 {
605 failureTable = new Table();
606 failureTable.Font.Name = "Sans-serif";
607 failureTable.Font.Size = 8;
608 failureTable.BorderStyle = BorderStyle.Outset;
609 failureTable.Width = Unit.Pixel(650);
610 TableRow rowOne = new TableRow();
611 TableCell rowOneCellOne = new TableCell();
612 rowOneCellOne.Font.Bold = true;
613 rowOneCellOne.Text = "ERROR:";
614 rowOne.Controls.Add(rowOneCellOne);
615 failureTable.Controls.Add(rowOne);
616 TableRow rowTwo = new TableRow();
617 TableCell rowTwoCellOne = new TableCell();
618 rowTwoCellOne.Text = message.ToString();
619 rowTwo.Controls.Add(rowTwoCellOne);
620 failureTable.Controls.Add(rowTwo);
621 failureTable.BorderWidth = 2;
622 failureTable.BorderColor = Color.Red;
623 failureTable.BackColor = System.Drawing.ColorTranslator.FromHtml("#fcc");
624 panelParam.Controls.Add(failureTable);
625 }
626
627 private void drawPanelForGetTransactionSuccess(Panel panelParam)
628 {
629 successTableGetTransaction = new Table();
630 successTableGetTransaction.Font.Name = "Sans-serif";
631 successTableGetTransaction.Font.Size = 8;
632 successTableGetTransaction.Width = Unit.Pixel(650);
633 TableRow rowOne = new TableRow();
634 TableCell rowOneCellOne = new TableCell();
635 rowOneCellOne.Font.Bold = true;
636 rowOneCellOne.HorizontalAlign = HorizontalAlign.Right;
637 rowOneCellOne.Text = "Parameter";
638 rowOneCellOne.Width = Unit.Pixel(300);
639 rowOne.Controls.Add(rowOneCellOne);
640 TableCell rowOneCellTwo = new TableCell();
641 rowOneCellTwo.Width = Unit.Pixel(50);
642 rowOne.Controls.Add(rowOneCellTwo);
643
644 TableCell rowOneCellThree = new TableCell();
645 rowOneCellThree.Font.Bold = true;
646 rowOneCellThree.HorizontalAlign = HorizontalAlign.Left;
647 rowOneCellThree.Text = "Value";
648 rowOneCellThree.Width = Unit.Pixel(300);
649 rowOne.Controls.Add(rowOneCellThree);
650 successTableGetTransaction.Controls.Add(rowOne);
651 panelParam.Controls.Add(successTableGetTransaction);
652 }
653 /*This function adds row to the success table */
654 private void addRowToGetTransactionSuccessPanel(Panel panelParam, string attribute, string value)
655 {
656 TableRow row = new TableRow();
657 TableCell cellOne = new TableCell();
658 cellOne.HorizontalAlign = HorizontalAlign.Right;
659 cellOne.Text = attribute.ToString();
660 cellOne.Width = Unit.Pixel(300);
661 row.Controls.Add(cellOne);
662 TableCell cellTwo = new TableCell();
663 cellTwo.Width = Unit.Pixel(50);
664 row.Controls.Add(cellTwo);
665 TableCell cellThree = new TableCell();
666 cellThree.HorizontalAlign = HorizontalAlign.Left;
667 cellThree.Text = value.ToString();
668 cellThree.Width = Unit.Pixel(300);
669 row.Controls.Add(cellThree);
670 successTableGetTransaction.Controls.Add(row);
671 }
672
673 private void drawPanelForGetSubscriptionDetailsSuccess(Panel panelParam)
674 {
675 successTableGetSubscriptionDetails = new Table();
676 successTableGetSubscriptionDetails.Font.Name = "Sans-serif";
677 successTableGetSubscriptionDetails.Font.Size = 8;
678 successTableGetSubscriptionDetails.Width = Unit.Pixel(650);
679 TableRow rowOne = new TableRow();
680 TableCell rowOneCellOne = new TableCell();
681 rowOneCellOne.Font.Bold = true;
682 rowOneCellOne.HorizontalAlign = HorizontalAlign.Right;
683 rowOneCellOne.Text = "Parameter";
684 rowOneCellOne.Width = Unit.Pixel(300);
685 rowOne.Controls.Add(rowOneCellOne);
686 TableCell rowOneCellTwo = new TableCell();
687 rowOneCellTwo.Width = Unit.Pixel(50);
688 rowOne.Controls.Add(rowOneCellTwo);
689
690 TableCell rowOneCellThree = new TableCell();
691 rowOneCellThree.Font.Bold = true;
692 rowOneCellThree.HorizontalAlign = HorizontalAlign.Left;
693 rowOneCellThree.Text = "Value";
694 rowOneCellThree.Width = Unit.Pixel(300);
695 rowOne.Controls.Add(rowOneCellThree);
696 successTableGetSubscriptionDetails.Controls.Add(rowOne);
697 panelParam.Controls.Add(successTableGetSubscriptionDetails);
698 }
699 /*This function adds row to the success table */
700 private void addRowToGetSubscriptionDetailsSuccessPanel(Panel panelParam, string attribute, string value)
701 {
702 TableRow row = new TableRow();
703 TableCell cellOne = new TableCell();
704 cellOne.HorizontalAlign = HorizontalAlign.Right;
705 cellOne.Text = attribute.ToString();
706 cellOne.Width = Unit.Pixel(300);
707 row.Controls.Add(cellOne);
708 TableCell cellTwo = new TableCell();
709 cellTwo.Width = Unit.Pixel(50);
710 row.Controls.Add(cellTwo);
711 TableCell cellThree = new TableCell();
712 cellThree.HorizontalAlign = HorizontalAlign.Left;
713 cellThree.Text = value.ToString();
714 cellThree.Width = Unit.Pixel(300);
715 row.Controls.Add(cellThree);
716 successTableGetSubscriptionDetails.Controls.Add(row);
717 }
718 /* This function reads the Access Token File and stores the values of access token, expiry seconds
719 * refresh token, last access token time and refresh token expiry time
720 * This funciton returns true, if access token file and all others attributes read successfully otherwise returns false
721 */
722 public bool readAccessTokenFile()
723 {
724 try
725 {
726 FileStream file = new FileStream(Request.MapPath(accessTokenFilePath), FileMode.OpenOrCreate, FileAccess.Read);
727 StreamReader sr = new StreamReader(file);
728 access_token = sr.ReadLine();
729 expiryMilliSeconds = sr.ReadLine();
730 refresh_token = sr.ReadLine();
731 lastTokenTakenTime = sr.ReadLine();
732 refreshTokenExpiryTime = sr.ReadLine();
733 sr.Close();
734 file.Close();
735 }
736 catch (Exception ex)
737 {
738 return false;
739 }
740 if ((access_token == null) || (expiryMilliSeconds == null) || (refresh_token == null) || (lastTokenTakenTime == null) || (refreshTokenExpiryTime == null))
741 {
742 return false;
743 }
744 return true;
745 }
746
747 /* This function validates the expiry of the access token and refresh token,
748 * function compares the current time with the refresh token taken time, if current time is greater then
749 * returns INVALID_REFRESH_TOKEN
750 * function compares the difference of last access token taken time and the current time with the expiry seconds, if its more,
751 * funciton returns INVALID_ACCESS_TOKEN
752 * otherwise returns VALID_ACCESS_TOKEN
753 */
754 public string isTokenValid()
755 {
756 try
757 {
758
759 DateTime currentServerTime = DateTime.UtcNow.ToLocalTime();
760 DateTime lastRefreshTokenTime = DateTime.Parse(refreshTokenExpiryTime);
761 TimeSpan refreshSpan = currentServerTime.Subtract(lastRefreshTokenTime);
762 if (currentServerTime >= lastRefreshTokenTime)
763 {
764 return "INVALID_ACCESS_TOKEN";
765 }
766 DateTime lastTokenTime = DateTime.Parse(lastTokenTakenTime);
767 TimeSpan tokenSpan = currentServerTime.Subtract(lastTokenTime);
768 if (((tokenSpan.TotalSeconds)) > Convert.ToInt32(expiryMilliSeconds))
769 {
770 return "REFRESH_ACCESS_TOKEN";
771 }
772 else
773 {
774 return "VALID_ACCESS_TOKEN";
775 }
776 }
777 catch (Exception ex)
778 {
779 return "INVALID_ACCESS_TOKEN";
780 }
781 }
782
783
784 /* This function get the access token based on the type parameter type values.
785 * If type value is 1, access token is fetch for client credential flow
786 * If type value is 2, access token is fetch for client credential flow based on the exisiting refresh token
787 */
788 public bool getAccessToken(int type, Panel panelParam)
789 {
790 /* This is client credential flow: */
791 if (type == 1)
792 {
793 try
794 {
795 DateTime currentServerTime = DateTime.UtcNow.ToLocalTime();
796 WebRequest accessTokenRequest = System.Net.HttpWebRequest.Create("" + FQDN + "/oauth/access_token?client_id=" + api_key.ToString() + "&client_secret=" + secret_key.ToString() + "&grant_type=client_credentials&scope=PAYMENT");
797 accessTokenRequest.Method = "GET";
798
799 WebResponse accessTokenResponse = accessTokenRequest.GetResponse();
800 using (StreamReader accessTokenResponseStream = new StreamReader(accessTokenResponse.GetResponseStream()))
801 {
802 string jsonAccessToken = accessTokenResponseStream.ReadToEnd().ToString();
803 JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
804 AccessTokenResponse deserializedJsonObj = (AccessTokenResponse)deserializeJsonObject.Deserialize(jsonAccessToken, typeof(AccessTokenResponse));
805 access_token = deserializedJsonObj.access_token.ToString();
806 expiryMilliSeconds = deserializedJsonObj.expires_in.ToString();
807 refresh_token = deserializedJsonObj.refresh_token.ToString();
808 FileStream file = new FileStream(Request.MapPath(accessTokenFilePath), FileMode.OpenOrCreate, FileAccess.Write);
809 StreamWriter sw = new StreamWriter(file);
810 sw.WriteLine(access_token);
811 sw.WriteLine(expiryMilliSeconds);
812 sw.WriteLine(refresh_token);
813 sw.WriteLine(currentServerTime.ToLongDateString() + " " + currentServerTime.ToLongTimeString());
814 lastTokenTakenTime = currentServerTime.ToLongDateString() + " " + currentServerTime.ToLongTimeString();
815 //Refresh token valids for 24 hours
816 DateTime refreshExpiry = currentServerTime.AddHours(24);
817 refreshTokenExpiryTime = refreshExpiry.ToLongDateString() + " " + refreshExpiry.ToLongTimeString();
818 sw.WriteLine(refreshExpiry.ToLongDateString() + " " + refreshExpiry.ToLongTimeString());
819 sw.Close();
820 file.Close();
821 // Close and clean up the StreamReader
822 accessTokenResponseStream.Close();
823 return true;
824 }
825 }
826 catch (Exception ex)
827 {
828 drawPanelForFailure(panelParam, ex.ToString());
829 return false;
830 }
831 }
832 else if (type == 2)
833 {
834 try
835 {
836 DateTime currentServerTime = DateTime.UtcNow.ToLocalTime();
837 WebRequest accessTokenRequest = System.Net.HttpWebRequest.Create("" + FQDN + "/oauth/access_token?grant_type=refresh_token&client_id=" + api_key.ToString() + "&client_secret=" + secret_key.ToString() + "&refresh_token=" + refresh_token.ToString());
838 accessTokenRequest.Method = "GET";
839 WebResponse accessTokenResponse = accessTokenRequest.GetResponse();
840 using (StreamReader accessTokenResponseStream = new StreamReader(accessTokenResponse.GetResponseStream()))
841 {
842 string access_token_json = accessTokenResponseStream.ReadToEnd().ToString();
843 JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
844 AccessTokenResponse deserializedJsonObj = (AccessTokenResponse)deserializeJsonObject.Deserialize(access_token_json, typeof(AccessTokenResponse));
845 access_token = deserializedJsonObj.access_token.ToString();
846 expiryMilliSeconds = deserializedJsonObj.expires_in.ToString();
847 refresh_token = deserializedJsonObj.refresh_token.ToString();
848 FileStream file = new FileStream(Request.MapPath(accessTokenFilePath), FileMode.OpenOrCreate, FileAccess.Write);
849 StreamWriter sw = new StreamWriter(file);
850 sw.WriteLine(access_token);
851 sw.WriteLine(expiryMilliSeconds);
852 sw.WriteLine(refresh_token);
853 sw.WriteLine(currentServerTime.ToLongDateString() + " " + currentServerTime.ToLongTimeString());
854 lastTokenTakenTime = currentServerTime.ToLongDateString() + " " + currentServerTime.ToLongTimeString();
855 //Refresh token valids for 24 hours
856 DateTime refreshExpiry = currentServerTime.AddHours(24);
857 refreshTokenExpiryTime = refreshExpiry.ToLongDateString() + " " + refreshExpiry.ToLongTimeString();
858 sw.WriteLine(refreshExpiry.ToLongDateString() + " " + refreshExpiry.ToLongTimeString());
859 sw.Close();
860 file.Close();
861 accessTokenResponseStream.Close();
862 return true;
863 }
864 }
865 catch (Exception ex)
866 {
867 drawPanelForFailure(panelParam, ex.ToString());
868 return false;
869 }
870 }
871 return false;
872 }
873
874 /* This function is used to neglect the ssl handshake error with authentication server */
875
876 public static void BypassCertificateError()
877 {
878 ServicePointManager.ServerCertificateValidationCallback +=
879 delegate(Object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
880 {
881 return true;
882 };
883 }
884 /* This function is used to read access token file and validate the access token
885 * this function returns true if access token is valid, or else false is returned
886 */
887 public bool readAndGetAccessToken(Panel panelParam)
888 {
889 bool result = true;
890 if (readAccessTokenFile() == false)
891 {
892 result = getAccessToken(1, panelParam);
893 }
894 else
895 {
896 string tokenValidity = isTokenValid();
897 if (tokenValidity.CompareTo("REFRESH_ACCESS_TOKEN") == 0)
898 {
899 result = getAccessToken(2, panelParam);
900 }
901 else if (string.Compare(isTokenValid(), "INVALID_ACCESS_TOKEN") == 0)
902 {
903 result = getAccessToken(1, panelParam);
904 }
905 }
906 return result;
907 }
908 public class AccessTokenResponse
909 {
910 public string access_token;
911 public string refresh_token;
912 public string expires_in;
913 }
914 public class RefundResponse
915 {
916 public string TransactionId { get; set; }
917 public string TransactionStatus { get; set; }
918 public string IsSuccess { get; set; }
919 public string Version { get; set; }
920 }
921 public class subscriptionStatusResponse
922 {
923 public string Currency { get; set; }
924 public string Version { get; set; }
925 public string IsSuccess { get; set; }
926 public string MerchantTransactionId { get; set; }
927 public string ConsumerId { get; set; }
928 public string Description { get; set; }
929 public string Amount { get; set; }
930 public string ContentCategory { get; set; }
931 public string MerchantProductId { get; set; }
932 public string MerchantApplicationId { get; set; }
933 public string Channel { get; set; }
934 public string SubscriptionPeriod{ get; set; }
935 public string PeriodAmount { get; set; }
936 public string Recurrences { get; set; }
937 public string MerchantSubscriptionId { get; set; }
938 public string MerchantIdentifier { get; set; }
939 public string IsAutoCommitted { get; set; }
940 public string SubscriptionId { get; set; }
941 public string SubscriptionStatus { get; set; }
942 public string SubscriptionType { get; set; }
943 public string OriginalTransactionId { get; set;}
944 }
945
946 public class subscriptionDetailsResponse
947 {
948 public string IsActiveSubscription { get; set; }
949 public string Currency { get; set; }
950 public string CreationDate { get; set; }
951 public string CurrentStartDate { get; set; }
952 public string CurrentEndDate { get; set; }
953 public string GrossAmount { get; set; }
954 public string Recurrences { get; set; }
955 public string RecurrencesLeft { get; set; }
956 public string Version { get; set; }
957 public string IsSuccess { get; set; }
958 }
959 //"\",\"MerchantApplicationId\":\"" + merchantApplicationId.ToString() +
960 //for amount do we need to add quotes, it worked for transaction with quotes
961 protected void newSubscriptionButton_Click1(object sender, EventArgs e)
962 {
963 readTransactionParametersFromConfigurationFile();
964 string payLoadString = "{\"Amount\":" + amount.ToString() + ",\"Category\":" + category.ToString() + ",\"Channel\":\"" +
965 channel.ToString() + "\",\"Description\":\"" + description.ToString() + "\",\"MerchantTransactionId\":\""
966 + merchantTransactionId.ToString() + "\",\"MerchantProductId\":\"" + merchantProductId.ToString()
967 + "\",\"MerchantPaymentRedirectUrl\":\"" + merchantRedirectURI.ToString() + "\",\"MerchantSubscriptionIdList\":\""
968 + MerchantSubscriptionIdList.ToString() + "\",\"IsPurchaseOnNoActiveSubscription\":\""
969 + IsPurchaseOnNoActiveSubscription.ToString() + "\",\"SubscriptionRecurringNumber\":" + SubscriptionRecurringNumber.ToString()
970 + ",\"SubscriptionRecurringPeriod\":\"" + SubscriptionRecurringPeriod.ToString()
971 + "\",\"SubscriptionRecurringPeriodAmount\":" + SubscriptionRecurringPeriodAmount.ToString() +
972 "}";
973 Session["sub_payloadData"] = payLoadString.ToString();
974 Response.Redirect(notaryURL.ToString() + "?request_to_sign=" + payLoadString.ToString() + "&goBackURL=" + merchantRedirectURI.ToString() + "&api_key=" + api_key.ToString() + "&secret_key=" + secret_key.ToString());
975 }
976 protected void getSubscriptionButton_Click(object sender, EventArgs e)
977 {
978 string resourcePathString = "";
979 try
980 {
981 string keyValue = "";
982 if (Radio_SubscriptionStatus.SelectedIndex == 0)
983 {
984 keyValue = GetSubscriptionMerchantSubsID.Text.ToString().Replace("Merchant Sub. ID: ", "");
985 if (keyValue.Length == 0)
986 return;
987 resourcePathString = "" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/MerchantTransactionId/" + keyValue.ToString();
988 }
989 if (Radio_SubscriptionStatus.SelectedIndex == 1)
990 {
991 keyValue = GetSubscriptionAuthCode.Text.ToString().Replace("Auth Code: ", "");
992 if (keyValue.Length == 0)
993 return;
994 resourcePathString = "" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/TransactionAuthCode/" + keyValue.ToString();
995 }
996 if (Radio_SubscriptionStatus.SelectedIndex == 2)
997 {
998 keyValue = GetSubscriptionID.Text.ToString().Replace("Subscription ID: ", "");
999 if (keyValue.Length == 0)
1000 return;
1001 resourcePathString = "" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/SubscriptionId/" + keyValue.ToString();
1002 }
1003 if (readAndGetAccessToken(newSubscriptionPanel) == true)
1004 {
1005 if (access_token == null || access_token.Length <= 0)
1006 {
1007 return;
1008 }
1009 //String getTransactionStatusResponseData;
1010 resourcePathString = resourcePathString + "?access_token=" + access_token.ToString();
1011 //HttpWebRequest objRequest = (HttpWebRequest)System.Net.WebRequest.Create("" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/" + Session["MerchantSubscriptionIdList"].ToString() + "/Detail/" + consumerId.ToString() + "?access_token=" + access_token.ToString());
1012 HttpWebRequest objRequest = (HttpWebRequest)System.Net.WebRequest.Create(resourcePathString);
1013 objRequest.Method = "GET";
1014 HttpWebResponse getTransactionStatusResponseObject = (HttpWebResponse)objRequest.GetResponse();
1015 using (StreamReader getTransactionStatusResponseStream = new StreamReader(getTransactionStatusResponseObject.GetResponseStream()))
1016 {
1017 String getTransactionStatusResponseData = getTransactionStatusResponseStream.ReadToEnd();
1018 JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
1019 subscriptionStatusResponse deserializedJsonObj = (subscriptionStatusResponse)deserializeJsonObject.Deserialize(getTransactionStatusResponseData, typeof(subscriptionStatusResponse));
1020 lblstatusMerSubsId.Text = deserializedJsonObj.MerchantSubscriptionId.ToString();
1021 lblstatusSubsId.Text = deserializedJsonObj.SubscriptionId.ToString();
1022 GetSubscriptionID.Text = "Subscription ID: " + deserializedJsonObj.SubscriptionId.ToString();
1023 if (checkItemInSubsDetailsFile(deserializedJsonObj.MerchantSubscriptionId.ToString(),deserializedJsonObj.ConsumerId.ToString()) == false)
1024 {
1025 writeSubsDetailsToFile(deserializedJsonObj.MerchantSubscriptionId.ToString(),deserializedJsonObj.ConsumerId.ToString());
1026 }
1027 if (checkItemInSubsRefundFile(deserializedJsonObj.SubscriptionId.ToString(), deserializedJsonObj.MerchantSubscriptionId.ToString()) == false)
1028 {
1029 writeSubsRefundToFile(deserializedJsonObj.SubscriptionId.ToString(), deserializedJsonObj.MerchantSubscriptionId.ToString());
1030 }
1031 subsDetailsTable.Controls.Clear();
1032 drawSubsDetailsSection(false);
1033 subsRefundTable.Controls.Clear();
1034 drawSubsRefundSection(false);
1035 subsGetStatusTable.Visible = true;
1036 drawPanelForGetTransactionSuccess(getSubscriptionStatusPanel);
1037 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "SubscriptionId", deserializedJsonObj.SubscriptionId.ToString());
1038 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "SubscriptionStatus", deserializedJsonObj.SubscriptionStatus.ToString());
1039 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "SubscriptionType", deserializedJsonObj.SubscriptionType.ToString());
1040 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Version", deserializedJsonObj.Version.ToString());
1041 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "ConsumerId", deserializedJsonObj.ConsumerId.ToString());
1042 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "MerchantTransactionId", deserializedJsonObj.MerchantTransactionId.ToString());
1043 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Description", deserializedJsonObj.Description.ToString());
1044 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Amount", deserializedJsonObj.Amount.ToString());
1045 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "ContentCategory", deserializedJsonObj.ContentCategory.ToString());
1046 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "MerchantProductId", deserializedJsonObj.MerchantProductId.ToString());
1047 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "MerchantApplicationId", deserializedJsonObj.MerchantApplicationId.ToString());
1048 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Channel ", deserializedJsonObj.Channel.ToString());
1049 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "SubscriptionPeriod", deserializedJsonObj.SubscriptionPeriod.ToString());
1050 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "PeriodAmount", deserializedJsonObj.PeriodAmount.ToString());
1051 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Recurrences", deserializedJsonObj.Recurrences.ToString());
1052 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "MerchantSubscriptionId", deserializedJsonObj.MerchantSubscriptionId.ToString());
1053 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "MerchantIdentifier", deserializedJsonObj.MerchantIdentifier.ToString());
1054 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "IsAutoCommitted", deserializedJsonObj.IsAutoCommitted.ToString());
1055 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "Currency", deserializedJsonObj.Currency.ToString());
1056 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "IsSuccess", deserializedJsonObj.IsSuccess.ToString());
1057 addRowToGetTransactionSuccessPanel(getSubscriptionStatusPanel, "OriginalTransactionId", deserializedJsonObj.OriginalTransactionId.ToString());
1058 getTransactionStatusResponseStream.Close();
1059 }
1060 }
1061 }
1062 catch (Exception ex)
1063 {
1064 drawPanelForFailure(getSubscriptionStatusPanel, ex.ToString());
1065 }
1066/*
1067 DateTime dummy = DateTime.UtcNow;
1068 string dummyString = String.Format("{0:dddMMMddyyyyHHmmss}", dummy);
1069 if (checkItemInSubsDetailsFile("Tran" + dummyString, "MerTran" + dummyString) == false)
1070 {
1071 writeSubsDetailsToFile("Tran" + dummyString, "MerTran" + dummyString);
1072 }
1073 subsDetailsTable.Controls.Clear();
1074 drawSubsDetailsSection(false);
1075*/
1076 }
1077 protected void viewNotaryButton_Click(object sender, EventArgs e)
1078 {
1079 if ((Session["sub_payloadData"] != null) && (Session["sub_signedPayLoad"] != null) && (Session["sub_signedSignature"] != null))
1080 {
1081 Session["sub_processNotary"] = "notary";
1082 Response.Redirect(notaryURL.ToString() + "?signed_payload=" + Session["sub_signedPayLoad"].ToString() + "&goBackURL=" + merchantRedirectURI.ToString() + "&signed_signature=" + Session["sub_signedSignature"].ToString() + "&signed_request=" + Session["sub_payloadData"].ToString());
1083 }
1084 }
1085
1086 protected void btnGetSubscriptionDetails_Click(object sender, EventArgs e)
1087 {
1088 string merSubsID = "";
1089 bool recordFound = false;
1090 try
1091 {
1092 if (subsDetailsList.Count > 0)
1093 {
1094 foreach (Control subDetailsTableRow in subsDetailsTable.Controls)
1095 {
1096 if (subDetailsTableRow is TableRow)
1097 {
1098 foreach (Control subDetailsTableRowCell in subDetailsTableRow.Controls)
1099 {
1100 if (subDetailsTableRowCell is TableCell)
1101 {
1102 foreach (Control subDetailsTableCellControl in subDetailsTableRowCell.Controls)
1103 {
1104 if ((subDetailsTableCellControl is RadioButton))
1105 {
1106
1107 if (((RadioButton)subDetailsTableCellControl).Checked)
1108 {
1109 merSubsID = ((RadioButton)subDetailsTableCellControl).Text.ToString();
1110 recordFound = true;
1111 break;
1112 }
1113 }
1114 }
1115 }
1116 }
1117 }
1118 }
1119 if (recordFound == true)
1120 {
1121 if (readAndGetAccessToken(subsDetailsPanel) == true)
1122 {
1123 if (access_token == null || access_token.Length <= 0)
1124 {
1125 return;
1126 }
1127 String consID = getValueOfKey(merSubsID);
1128 if (consID.CompareTo("null") == 0)
1129 return;
1130 //drawPanelForFailure(getSubscriptionStatusPanel, merchantSubId.ToString());
1131 //String getTransactionStatusResponseData;
1132 WebRequest objRequest = (WebRequest)System.Net.WebRequest.Create("" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/" + merSubsID.ToString() + "/Detail/" + consID.ToString() + "?access_token=" + access_token.ToString());
1133 //WebRequest objRequest = (WebRequest)System.Net.WebRequest.Create("" + FQDN + "/Commerce/Payment/Rest/2/Subscriptions/" + merSubsID.ToString() + "/Detail/" + consID.ToString() );
1134 objRequest.Method = "GET";
1135 objRequest.ContentType = "application/json";
1136 WebResponse subsDetailsResponeObject = (WebResponse)objRequest.GetResponse();
1137 using (StreamReader subsDetailsResponseStream = new StreamReader(subsDetailsResponeObject.GetResponseStream()))
1138 {
1139 String subsDetailsResponseData = subsDetailsResponseStream.ReadToEnd();
1140 JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
1141 subscriptionDetailsResponse deserializedJsonObj = (subscriptionDetailsResponse)deserializeJsonObject.Deserialize(subsDetailsResponseData, typeof(subscriptionDetailsResponse));
1142 subsDetailsSuccessTable.Visible = true;
1143 lblMerSubId.Text = merSubsID.ToString();
1144 lblConsId.Text = consID.ToString();
1145 drawPanelForGetSubscriptionDetailsSuccess(subsDetailsPanel);
1146 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "Recurrences", deserializedJsonObj.Recurrences.ToString());
1147 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "CurrentStartDate", deserializedJsonObj.CurrentStartDate.ToString());
1148 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "IsSuccess", deserializedJsonObj.IsSuccess.ToString());
1149 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "IsActiveSubscription", deserializedJsonObj.IsActiveSubscription.ToString());
1150 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "RecurrencesLeft", deserializedJsonObj.RecurrencesLeft.ToString());
1151 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "GrossAmount", deserializedJsonObj.GrossAmount.ToString());
1152 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "CreationDate", deserializedJsonObj.CreationDate.ToString());
1153 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "Version", deserializedJsonObj.Version.ToString());
1154 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "Currency", deserializedJsonObj.Currency.ToString());
1155 addRowToGetSubscriptionDetailsSuccessPanel(getSubscriptionStatusPanel, "CurrentEndDate", deserializedJsonObj.CurrentEndDate.ToString());
1156 if (LatestFive == false)
1157 {
1158 //subsDetailsList.RemoveAll(x => x.Key.Equals(merSubsID));
1159 //updatesubsDetailsListToFile();
1160 //resetSubsDetailsList();
1161 //subsDetailsTable.Controls.Clear();
1162 //drawSubsDetailsSection(false);
1163 //GetSubscriptionMerchantSubsID.Text = "Merchant Sub. ID: ";
1164 //GetSubscriptionAuthCode.Text = "Auth Code: ";
1165 //GetSubscriptionID.Text = "Subscription ID: ";
1166 }
1167 subsDetailsResponseStream.Close();
1168 }
1169 }
1170/*
1171 subsDetailsSuccessTable.Visible = true;
1172 lblMerSubsId.Text = merSubsID.ToString();
1173 lblSubsIdDetails.Text = "SUCCESSFUL";
1174 lblMerTranId.Text = "true";
1175 if (LatestFive == false)
1176 {
1177 //subsDetailsList.RemoveAll(x => x.Key.Equals(merSubsID));
1178 //updatesubsDetailsListToFile();
1179 //resetSubsDetailsList();
1180 //subsDetailsTable.Controls.Clear();
1181 //drawSubsDetailsSection(false);
1182 GetSubscriptionMerchantSubsID.Text = "Merchant Sub. ID: ";
1183 GetSubscriptionAuthCode.Text = "Auth Code: ";
1184 GetSubscriptionID.Text = "Subscription ID: ";
1185 }*/
1186 }
1187 }
1188 }
1189 catch (Exception ex)
1190 {
1191 drawPanelForFailure(subsDetailsPanel, ex.ToString());
1192 }
1193 }
1194 protected void btnGetSubscriptionRefund_Click(object sender, EventArgs e)
1195 {
1196 string SubsID = "";
1197 bool recordFound = false;
1198 string strReq = "{\"RefundReasonCode\":1,\"RefundReasonText\":\"Customer was not happy\"}";
1199 string dataLength = "";
1200 try
1201 {
1202 if (subsRefundList.Count > 0)
1203 {
1204 foreach (Control subRefundTableRow in subsRefundTable.Controls)
1205 {
1206 if (subRefundTableRow is TableRow)
1207 {
1208 foreach (Control subRefundTableRowCell in subRefundTableRow.Controls)
1209 {
1210 if (subRefundTableRowCell is TableCell)
1211 {
1212 foreach (Control subRefundTableCellControl in subRefundTableRowCell.Controls)
1213 {
1214 if ((subRefundTableCellControl is RadioButton))
1215 {
1216
1217 if (((RadioButton)subRefundTableCellControl).Checked)
1218 {
1219 SubsID = ((RadioButton)subRefundTableCellControl).Text.ToString();
1220 recordFound = true;
1221 break;
1222 }
1223 }
1224 }
1225 }
1226 }
1227 }
1228 }
1229 if (recordFound == true)
1230 {
1231 if (readAndGetAccessToken(subsRefundPanel) == true)
1232 {
1233 if (access_token == null || access_token.Length <= 0)
1234 {
1235 return;
1236 }
1237 String merSubsID = getValueOfKeyFromRefund(SubsID);
1238 if (merSubsID.CompareTo("null") == 0)
1239 return;
1240 //drawPanelForFailure(getSubscriptionStatusPanel, merchantSubId.ToString());
1241 //String getTransactionStatusResponseData;
1242 WebRequest objRequest = (WebRequest)System.Net.WebRequest.Create("" + FQDN + "/Commerce/Payment/Rest/2/Transactions/" + SubsID.ToString() + "?access_token=" + access_token.ToString() + "&Action=refund");
1243 objRequest.Method = "PUT";
1244 objRequest.ContentType = "application/json";
1245 UTF8Encoding encoding = new UTF8Encoding();
1246 byte[] postBytes = encoding.GetBytes(strReq);
1247 objRequest.ContentLength = postBytes.Length;
1248 Stream postStream = objRequest.GetRequestStream();
1249 postStream.Write(postBytes, 0, postBytes.Length);
1250 dataLength = postBytes.Length.ToString();
1251 postStream.Close();
1252 WebResponse subsRefundResponeObject = (WebResponse)objRequest.GetResponse();
1253 using (StreamReader subsRefundResponseStream = new StreamReader(subsRefundResponeObject.GetResponseStream()))
1254 {
1255 String subsRefundResponseData = subsRefundResponseStream.ReadToEnd();
1256 JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer();
1257 RefundResponse deserializedJsonObj = (RefundResponse)deserializeJsonObject.Deserialize(subsRefundResponseData, typeof(RefundResponse));
1258 subsRefundSuccessTable.Visible = true;
1259 //drawPanelForGetSubscriptionDetailsSuccess(subsRefundPanel);
1260 lbRefundTranID.Text = deserializedJsonObj.TransactionId.ToString();
1261 lbRefundTranStatus.Text = deserializedJsonObj.TransactionStatus.ToString();
1262 lbRefundIsSuccess.Text = deserializedJsonObj.IsSuccess.ToString();
1263 lbRefundVersion.Text = deserializedJsonObj.Version.ToString();
1264 if (LatestFive == false)
1265 {
1266 subsRefundList.RemoveAll(x => x.Key.Equals(SubsID));
1267 updatesubsRefundListToFile();
1268 resetSubsRefundList();
1269 subsRefundTable.Controls.Clear();
1270 drawSubsRefundSection(false);
1271 GetSubscriptionMerchantSubsID.Text = "Merchant Sub. ID: ";
1272 GetSubscriptionAuthCode.Text = "Auth Code: ";
1273 GetSubscriptionID.Text = "Subscription ID: ";
1274 }
1275 subsRefundResponseStream.Close();
1276 }
1277 }
1278 }
1279 }
1280 }
1281 catch (Exception ex)
1282 {
1283 drawPanelForFailure(subsRefundPanel, ex.ToString());
1284 }
1285 }
1286}