Skip to content

Instantly share code, notes, and snippets.

@incarnate
Last active November 5, 2019 07:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incarnate/294476023d38944bc2e1 to your computer and use it in GitHub Desktop.
Save incarnate/294476023d38944bc2e1 to your computer and use it in GitHub Desktop.
Examples of using eWAY's iOS SDK in a Swift project.
/**
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)"
})
}
}
@newarockkid
Copy link

Getting a Cannot call value of non-function type 'String!!' error while trying to initialise/setup the SDK in the AppDelegate file. Was wondering if you were also encountering the same issue?

Thanks.

@Ovais1070
Copy link

Is this Code still applicable? When i am setting
RapidAPI.sharedManager().setRapidEndpoint(Endpoint as NSString)
RapidAPI.sharedManager().setPublicAPIKey(PublicAPIKey as NSString)

i am getting error msg Value of type 'Any?' has no member 'setRapidEndpoint'

@incarnate
Copy link
Author

@Ovais1070 this code sample is a bit old (2015), so may not work.
I'm not up to speed with Swift, but a quick Google suggests that with Swift 3 the use of AnyObject? has been replaced with Any? - if you adjust line 24 & 57 hopefully you will have success.

@Murteza12
Copy link

Murteza12 commented Nov 5, 2019

How i initialise in app delegate :
let Endpoint = "https://api.sandbox.ewaypayments.com/"
let PublicAPIKey = "xx-xxxx0x0xxxxxxxx"

 RapidAPI.init().rapidEndpoint = Endpoint
 RapidAPI.init().publicAPIKey = PublicAPIKey

Trying to implement while payment but getting this error
s9992 Communication error with Rapid API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment