Last active
February 26, 2016 05:03
-
-
Save ianleeder/2bf770ff70a5a9288f13 to your computer and use it in GitHub Desktop.
Test for registering and updating stored payment details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" %> | |
<%@ Import Namespace="System.IO" %> | |
<%@ Import Namespace="eWAY.Rapid" %> | |
<%@ Import Namespace="eWAY.Rapid.Enums" %> | |
<%@ Import Namespace="eWAY.Rapid.Models" %> | |
<!DOCTYPE html> | |
<script runat="server"> | |
private static readonly string ApiKey = "myKey"; | |
private static readonly string ApiPassword = "myPassword"; | |
private static readonly string RapidEndpoint = "Sandbox"; | |
private static string MyRedirectUrl; | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
MyRedirectUrl = string.Format("{0}://{1}{2}", Context.Request.Url.Scheme, Context.Request.Url.Authority, Context.Request.Url.AbsolutePath); | |
// This will be our state machine | |
// (so I can cram an entire example into one page/file) | |
// 1 = Register | |
// 2 = Register results | |
// 3 = Update details | |
// 4 = Update results | |
// 5 = Charge recurring payment (+ show results) | |
// 6 = Update details with payment | |
// 7 = Update then immediately charge (and show results) | |
int state; | |
if (!Int32.TryParse(Request.QueryString["Step"], out state)) | |
state = 1; | |
// Hide all divs and only show as needed | |
registerDiv.Visible = false; | |
updateDetailsDiv.Visible = false; | |
resultsDiv.Visible = false; | |
switch (state) | |
{ | |
// 1 = Register | |
case 1: | |
registerDiv.Visible = true; | |
RegisterNewUser(); | |
break; | |
case 2: | |
resultsDiv.Visible = true; | |
GetTransactionResults(); | |
break; | |
case 3: | |
updateDetailsDiv.Visible = true; | |
UpdateCustomer(); | |
break; | |
case 4: | |
resultsDiv.Visible = true; | |
GetTransactionResults(); | |
break; | |
case 5: | |
resultsDiv.Visible = true; | |
ChargeCustomerRecurring(); | |
break; | |
case 6: | |
updateDetailsDiv.Visible = true; | |
UpdateCustomerWithPurchase(); | |
break; | |
case 7: | |
updateDetailsDiv.Visible = true; | |
UpdateCustomerWithoutPaymentThenCharge(); | |
break; | |
case 8: | |
resultsDiv.Visible = true; | |
ChargeCustomerUpdateFee(); | |
break; | |
} | |
if (state != 1) | |
Response.Write("<h1>Token = " + HttpContext.Current.Session["token"] + "</h1>"); | |
} | |
// Used in step 1 | |
private void RegisterNewUser() | |
{ | |
Transaction transaction = new Transaction() | |
{ | |
Customer = new Customer() | |
{ | |
Reference = "A12345", | |
Title = "Mr.", | |
FirstName = "John", | |
LastName = "Smith", | |
Address = new Address() | |
{ | |
Street1 = "Level 5", | |
Street2 = "369 Queen Street", | |
City = "Sydney", | |
State = "NSW", | |
Country = "au", | |
PostalCode = "2000", | |
}, | |
}, | |
PaymentDetails = new PaymentDetails() | |
{ | |
TotalAmount = 100, | |
InvoiceDescription = "Registration Invoice Description", | |
// Paypal (sandbox) won't allow me to create multiple payments with the same InvoiceNumber | |
// (even though returned error code is V6014 - Invalid Invoice*Reference*. See eWAY Case 00334694) | |
// Make a unique reference number | |
InvoiceNumber = DateTime.UtcNow.Ticks.ToString(), | |
CurrencyCode = "AUD" | |
}, | |
RedirectURL = MyRedirectUrl + "?Step=2", | |
SaveCustomer = true, | |
TransactionType = TransactionTypes.Purchase | |
}; | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
CreateTransactionResponse createResponse = ewayClient.Create(PaymentMethod.TransparentRedirect, transaction); | |
// Set values for Paypal form | |
literalRegistrationPaypalActionUrl.Text = createResponse.FormActionUrl; | |
literalRegistrationPaypalAccessCode.Text = createResponse.AccessCode; | |
// Set values for Credit Card form | |
literalRegistrationCreditCardActionUrl.Text = createResponse.FormActionUrl; | |
literalRegistrationCreditCardAccessCode.Text = createResponse.AccessCode; | |
} | |
// Used in step 2, 4 | |
private void GetTransactionResults() | |
{ | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
string accessCode = Request.QueryString["AccessCode"]; | |
QueryTransactionResponse transactionResponse = ewayClient.QueryTransaction(accessCode); | |
UpdateResults(transactionResponse); | |
HttpContext.Current.Session["token"] = transactionResponse.Transaction.Customer.TokenCustomerID; | |
} | |
// Used in step 3 | |
private void UpdateCustomer() | |
{ | |
Customer customer = new Customer | |
{ | |
TokenCustomerID = (string)HttpContext.Current.Session["token"], | |
RedirectURL = MyRedirectUrl + "?Step=4" | |
}; | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
CreateCustomerResponse createResponse = ewayClient.UpdateCustomer(PaymentMethod.TransparentRedirect, customer); | |
// Set values for Paypal form | |
literalUpdatePaypalActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdatePaypalAccessCode.Text = createResponse.AccessCode; | |
// Set values for Credit Card form | |
literalUpdateCreditCardActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdateCreditCardAccessCode.Text = createResponse.AccessCode; | |
} | |
// Used in step 5 | |
private void ChargeCustomerRecurring() | |
{ | |
Transaction transaction = new Transaction | |
{ | |
Customer = new Customer | |
{ | |
TokenCustomerID = (string)HttpContext.Current.Session["token"] | |
}, | |
TransactionType = TransactionTypes.Recurring, | |
PaymentDetails = new PaymentDetails | |
{ | |
TotalAmount = 500, | |
CurrencyCode = "AUD", | |
InvoiceNumber = DateTime.UtcNow.Ticks.ToString(), | |
InvoiceDescription = "Recurring Payment Invoice Description" | |
} | |
}; | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
CreateTransactionResponse createResponse = ewayClient.Create(PaymentMethod.Direct, transaction); | |
// Note that even though I am using ALL the same fields as I do from a QueryTransactionResponse, | |
// I can't call UpdateResults because QueryTransactionResults and CreateTransactionResponse don't | |
// share a base class. | |
resultsResponseCode.InnerText = createResponse.TransactionStatus.ProcessingDetails.ResponseCode; | |
resultsResponseMessage.InnerText = createResponse.TransactionStatus.ProcessingDetails.ResponseMessage; | |
resultsTransactionNumber.InnerText = createResponse.TransactionStatus.TransactionID.ToString(); | |
resultsInvoiceDescription.InnerText = createResponse.Transaction.PaymentDetails.InvoiceDescription; | |
resultsInvoiceNumber.InnerText = createResponse.Transaction.PaymentDetails.InvoiceNumber; | |
resultsAmount.InnerText = createResponse.Transaction.PaymentDetails.CurrencyCode + " " + | |
(((double)createResponse.Transaction.PaymentDetails.TotalAmount) / 100).ToString("C"); | |
} | |
// Used in step 6 | |
private void UpdateCustomerWithPurchase() | |
{ | |
Transaction transaction = new Transaction | |
{ | |
Customer = new Customer | |
{ | |
TokenCustomerID = (string)HttpContext.Current.Session["token"] | |
}, | |
TransactionType = TransactionTypes.Purchase, | |
PaymentDetails = new PaymentDetails | |
{ | |
TotalAmount = 600, | |
CurrencyCode = "AUD", | |
InvoiceNumber = DateTime.UtcNow.Ticks.ToString(), | |
InvoiceDescription = "Update Customer with Payment Invoice Description" | |
}, | |
RedirectURL = MyRedirectUrl + "?Step=4" | |
}; | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
CreateTransactionResponse createResponse = ewayClient.Create(PaymentMethod.TransparentRedirect, transaction); | |
// Set values for Paypal form | |
literalUpdatePaypalActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdatePaypalAccessCode.Text = createResponse.AccessCode; | |
// Set values for Credit Card form | |
literalUpdateCreditCardActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdateCreditCardAccessCode.Text = createResponse.AccessCode; | |
} | |
// Used in step 7 | |
private void UpdateCustomerWithoutPaymentThenCharge() | |
{ | |
Customer customer = new Customer | |
{ | |
TokenCustomerID = (string)HttpContext.Current.Session["token"], | |
RedirectURL = MyRedirectUrl + "?Step=8" | |
}; | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
CreateCustomerResponse createResponse = ewayClient.UpdateCustomer(PaymentMethod.TransparentRedirect, customer); | |
// Set values for Paypal form | |
literalUpdatePaypalActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdatePaypalAccessCode.Text = createResponse.AccessCode; | |
// Set values for Credit Card form | |
literalUpdateCreditCardActionUrl.Text = createResponse.FormActionUrl; | |
literalUpdateCreditCardAccessCode.Text = createResponse.AccessCode; | |
} | |
// Used in step 8 | |
private void ChargeCustomerUpdateFee() | |
{ | |
IRapidClient ewayClient = RapidClientFactory.NewRapidClient(ApiKey, ApiPassword, RapidEndpoint); | |
// First check the results of the update (no payment) | |
string accessCode = Request.QueryString["AccessCode"]; | |
QueryTransactionResponse updateResponse = ewayClient.QueryTransaction(accessCode); | |
if (!"00".Equals(updateResponse.TransactionStatus.ProcessingDetails.ResponseCode)) | |
throw new InvalidDataException("Unexpected error with update in steap 8"); | |
// Now run another transaction to verify stored payment details work | |
Transaction transaction = new Transaction | |
{ | |
Customer = new Customer | |
{ | |
TokenCustomerID = (string)HttpContext.Current.Session["token"] | |
}, | |
TransactionType = TransactionTypes.Recurring, | |
PaymentDetails = new PaymentDetails | |
{ | |
TotalAmount = 800, | |
CurrencyCode = "AUD", | |
InvoiceNumber = DateTime.UtcNow.Ticks.ToString(), | |
InvoiceDescription = "Update Fee Payment Invoice Description" | |
} | |
}; | |
CreateTransactionResponse createResponse = ewayClient.Create(PaymentMethod.Direct, transaction); | |
// Note that even though I am using ALL the same fields as I do from a QueryTransactionResponse, | |
// I can't call UpdateResults because QueryTransactionResults and CreateTransactionResponse don't | |
// share a base class. | |
resultsResponseCode.InnerText = createResponse.TransactionStatus.ProcessingDetails.ResponseCode; | |
resultsResponseMessage.InnerText = createResponse.TransactionStatus.ProcessingDetails.ResponseMessage; | |
resultsTransactionNumber.InnerText = createResponse.TransactionStatus.TransactionID.ToString(); | |
resultsInvoiceDescription.InnerText = createResponse.Transaction.PaymentDetails.InvoiceDescription; | |
resultsInvoiceNumber.InnerText = createResponse.Transaction.PaymentDetails.InvoiceNumber; | |
resultsAmount.InnerText = createResponse.Transaction.PaymentDetails.CurrencyCode + " " + | |
(((double)createResponse.Transaction.PaymentDetails.TotalAmount) / 100).ToString("C"); | |
} | |
// Used in step 2, 4 | |
private void UpdateResults(QueryTransactionResponse response) | |
{ | |
resultsResponseCode.InnerText = response.TransactionStatus.ProcessingDetails.ResponseCode; | |
resultsResponseMessage.InnerText = response.TransactionStatus.ProcessingDetails.ResponseMessage; | |
resultsTransactionNumber.InnerText = response.TransactionStatus.TransactionID.ToString(); | |
resultsInvoiceDescription.InnerText = response.Transaction.PaymentDetails.InvoiceDescription; | |
resultsInvoiceNumber.InnerText = response.Transaction.PaymentDetails.InvoiceNumber; | |
resultsAmount.InnerText = response.Transaction.PaymentDetails.CurrencyCode + " " + | |
(((double) response.Transaction.PaymentDetails.TotalAmount)/100).ToString("C"); | |
} | |
</script> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title>eWAY Paypal Test</title> | |
<style> | |
th,tr {text-align: left} | |
</style> | |
</head> | |
<body> | |
<div id="registerDiv" runat="server"> | |
<h1>Register</h1> | |
<form method="POST" action="<asp:Literal runat='server' ID='literalRegistrationPaypalActionUrl' />"> | |
<input type="hidden" name="EWAY_ACCESSCODE" value="<asp:Literal runat='server' ID='literalRegistrationPaypalAccessCode' />" /> | |
<input type="hidden" name="EWAY_PAYMENTTYPE" value="PayPal" /> | |
<input type="submit" value="Register With Paypal" /> | |
</form> | |
<form method="POST" action="<asp:Literal runat='server' ID='literalRegistrationCreditCardActionUrl' />"> | |
<input type="hidden" name="EWAY_ACCESSCODE" value="<asp:Literal runat='server' ID='literalRegistrationCreditCardAccessCode' />" /> | |
<input type="hidden" name="EWAY_PAYMENTTYPE" value="Credit Card" /> | |
<input type="hidden" name="EWAY_CARDNAME" value="John Smith" /> | |
<input type="hidden" name="EWAY_CARDNUMBER" value="4444333322221111" /> | |
<input type="hidden" name="EWAY_CARDEXPIRYMONTH" value="01" /> | |
<input type="hidden" name="EWAY_CARDEXPIRYYEAR" value="2020" /> | |
<input type="hidden" name="EWAY_CARDCVN" value="123" /> | |
<input type="submit" value="Register With Credit Card" /> | |
</form> | |
</div> | |
<div id="resultsDiv" runat="server"> | |
<h1>Results</h1> | |
<table> | |
<tr><th>Response code</th><td id="resultsResponseCode" runat="server"></td></tr> | |
<tr><th>Response message</th><td id="resultsResponseMessage" runat="server"></td></tr> | |
<tr><th>Transaction Number</th><td id="resultsTransactionNumber" runat="server"></td></tr> | |
<tr><th>Invoice Description</th><td id="resultsInvoiceDescription" runat="server"></td></tr> | |
<tr><th>Invoice Number</th><td id="resultsInvoiceNumber" runat="server"></td></tr> | |
<tr><th>Amount</th><td id="resultsAmount" runat="server"></td></tr> | |
</table> | |
<a href="EwayPaypalTest.aspx?Step=3">Update details without payment</a><br/> | |
<a href="EwayPaypalTest.aspx?Step=5">Submit recurring payment</a><br/> | |
<a href="EwayPaypalTest.aspx?Step=6">Update details via payment</a><br/> | |
<a href="EwayPaypalTest.aspx?Step=7">Update details without payment THEN charge</a><br/> | |
</div> | |
<div id="updateDetailsDiv" runat="server"> | |
<h1>Update</h1> | |
<form method="POST" action="<asp:Literal runat='server' ID='literalUpdatePaypalActionUrl' />"> | |
<input type="hidden" name="EWAY_ACCESSCODE" value="<asp:Literal runat='server' ID='literalUpdatePaypalAccessCode' />" /> | |
<input type="hidden" name="EWAY_PAYMENTTYPE" value="PayPal" /> | |
<input type="submit" value="Update With Paypal" /> | |
</form> | |
<form method="POST" action="<asp:Literal runat='server' ID='literalUpdateCreditCardActionUrl' />"> | |
<input type="hidden" name="EWAY_ACCESSCODE" value="<asp:Literal runat='server' ID='literalUpdateCreditCardAccessCode' />" /> | |
<input type="hidden" name="EWAY_PAYMENTTYPE" value="Credit Card" /> | |
<input type="hidden" name="EWAY_CARDNAME" value="Jimmy Smits" /> | |
<input type="hidden" name="EWAY_CARDNUMBER" value="4444333322221111" /> | |
<input type="hidden" name="EWAY_CARDEXPIRYMONTH" value="01" /> | |
<input type="hidden" name="EWAY_CARDEXPIRYYEAR" value="2030" /> | |
<input type="hidden" name="EWAY_CARDCVN" value="123" /> | |
<input type="submit" value="Update With Credit Card" /> | |
</form> | |
</div> | |
<hr/> | |
<a href="EwayPaypalTest.aspx?Step=1">Start over</a><br/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment