Last active
November 5, 2019 07:24
-
-
Save incarnate/294476023d38944bc2e1 to your computer and use it in GitHub Desktop.
Examples of using eWAY's iOS SDK in a Swift project.
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
/** | |
Example functions using the eWAY iOS SDK in Swift | |
To use the eWAY iOS SDK with a Swift project, simply: | |
1. Add the eWAY SDK using CocoaPods as usual | |
2. Add the Objective-C bridging header file | |
3. Add RapidAPI.h to the bridging header file | |
For more info on using Objective C with Swift, see: | |
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html | |
For more detail on using the eWAY iOS SDK, see: | |
https://www.eway.com.au/developers/sdk/ios | |
Happy coding! | |
*/ | |
/** | |
Setting the eWAY Public API Key & endpoint | |
An AppDelegate class method | |
*/ | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
let Endpoint = "https://api.sandbox.ewaypayments.com/" | |
let PublicAPIKey = "epk-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" | |
RapidAPI.sharedManager().setRapidEndpoint(Endpoint as NSString) | |
RapidAPI.sharedManager().setPublicAPIKey(PublicAPIKey as NSString) | |
return true | |
} | |
/** | |
Example function to encrypt sensitive card data values | |
Once encrypted, the data can be passed to your sever for processing using | |
eWAY's Rapid API. | |
*/ | |
func encryptMethod() { | |
// Create array of details to encrypt | |
var arrNVpair = NSMutableArray() | |
var nvpair1: NVpair = NVpair() | |
nvpair1.name = "cards" | |
nvpair1.value = "4444333322221111" | |
var nvpair2: NVpair = NVpair() | |
nvpair2.name = "CVN" | |
nvpair2.value = "123" | |
arrNVpair.addObject(nvpair1) | |
arrNVpair.addObject(nvpair2) | |
RapidAPI.encryptValues(arrNVpair, completed: { | |
(encryptValuesResponse: EncryptValuesResponse) -> () in | |
if encryptValuesResponse.Status == Success { | |
var values: [AnyObject] = NSMutableArray.new() | |
for nv: NVpair in encryptValuesResponse.Values { | |
values.addObject(nv.dictionary()) | |
} | |
} | |
}) | |
} | |
/** | |
Example of making a payment using the eWAY iOS SDK | |
*/ | |
func makePayment() { | |
// Create transaction objects | |
var transaction = Transaction() | |
var customerObj = Customer() | |
customerObj.Reference = "A12345" | |
customerObj.Title = "Mr." | |
customerObj.FirstName = "John" | |
customerObj.LastName = "Smith" | |
customerObj.CompanyName = "Demo Shop 123" | |
customerObj.JobDescription = "Developer" | |
customerObj.Phone = "09 889 0986" | |
customerObj.Mobile = "09 889 0986" | |
var customerAddress = Address() | |
customerAddress.Street1 = "Level 5" | |
customerAddress.Street2 = "369 Queen Street" | |
customerAddress.City = "Sydney" | |
customerAddress.State = "NSW" | |
customerAddress.PostalCode = "2010" | |
customerAddress.Country = "au" | |
customerObj.Address = customerAddress | |
var cardDetails = CardDetails() | |
cardDetails.Name = "eWAY Test" | |
cardDetails.Number = "4444333322221111" | |
cardDetails.ExpiryMonth = "12" | |
cardDetails.ExpiryYear = "19" | |
cardDetails.CVN = "123" | |
customerObj.CardDetails = cardDetails | |
transaction.Customer = customerObj | |
var payment = Payment() | |
payment.Payment = 100 | |
payment.InvoiceNumber = "Inv 21540" | |
payment.InvoiceDescription = "Individual Invoice Description" | |
payment.InvoiceReference = "513456" | |
payment.CurrencyCode = "AUD" | |
transaction.Payment = payment | |
// Submit the data to eWAY with the submitPayment method | |
// The submissionID can be passed to your server to fetch the transaction result | |
var mySubmitPaymentResponse:SubmitPaymentResponse; | |
RapidAPI.submitPayment(transaction, completed: { | |
(submitPaymentResponse: SubmitPaymentResponse) in | |
var submissionID: String = submitPaymentResponse.SubmissionID | |
mySubmitPaymentResponse = SubmitPaymentResponse | |
}) | |
// Error handling example | |
if mySubmitPaymentResponse.Status.value == Error.value { | |
RapidAPI.userMessage(mySubmitPaymentResponse.Errors, Language: "EN", completed: { | |
(userMessageResponse: UserMessageResponse) in | |
var msg: String = "\(userMessageResponse.Errors) \n \(userMessageResponse.Messages)" | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How i initialise in app delegate :
let Endpoint = "https://api.sandbox.ewaypayments.com/"
let PublicAPIKey = "xx-xxxx0x0xxxxxxxx"
Trying to implement while payment but getting this error
s9992 Communication error with Rapid API.