Skip to content

Instantly share code, notes, and snippets.

@isisnaomi
Last active December 2, 2019 18:25
Show Gist options
  • Save isisnaomi/3b4c927790dd4e88116d813a1a12bd5f to your computer and use it in GitHub Desktop.
Save isisnaomi/3b4c927790dd4e88116d813a1a12bd5f to your computer and use it in GitHub Desktop.
SiriKit Payment Intent Handler
import Intents
extension IntentHandler: INSendPaymentIntentHandling {
func handle(
intent: INSendPaymentIntent,
completion: @escaping (INSendPaymentIntentResponse) -> Void
) {
// Perform the expected action. Report back to Siri
// This is the place to call your service, update database, or whatever you need to do.
completion(INSendPaymentIntentResponse.init(code: .success, userActivity: nil))
return
}
func resolveCurrencyAmount(
for intent: INSendPaymentIntent,
with completion: @escaping (INSendPaymentCurrencyAmountResolutionResult) -> Void
) {
// Validate & clarify currency parameter
let amount = intent.currencyAmount?.amount ?? 0
completion(
INSendPaymentCurrencyAmountResolutionResult.success(with: INCurrencyAmount(amount: amount, currencyCode: "USD" ))
)
}
func resolveNote(
for intent: INSendPaymentIntent,
with completion: @escaping (INStringResolutionResult) -> Void
) {
// Validate & clarify note parameter
guard let note = intent.note else {
completion(INStringResolutionResult.needsValue())
return
}
completion(INStringResolutionResult.success(with: note))
}
func resolvePayee(
for intent: INSendPaymentIntent,
with completion: @escaping (INSendPaymentPayeeResolutionResult) -> Void
) {
// Validate & clarify payee parameter
guard let payee = intent.payee else {
// Request value when no payee was declared
completion(INSendPaymentPayeeResolutionResult.needsValue())
return
}
completion(INSendPaymentPayeeResolutionResult.success(with: payee))
return
}
func confirm(
intent: INSendPaymentIntent,
completion: @escaping (INSendPaymentIntentResponse) -> Void
) {
// Tell Siri how it went. Validate with user the information is correct before sending
let response = INSendPaymentIntentResponse(code: .ready, userActivity: nil)
response.paymentRecord = INPaymentRecord(
payee: intent.payee,
payer: nil,
currencyAmount: intent.currencyAmount,
paymentMethod: nil,
note: intent.note,
status: .pending
)
completion(response)
}
}
extension IntentHandler: INRequestPaymentIntentHandling {
func handle(
intent: INRequestPaymentIntent,
completion: @escaping (INRequestPaymentIntentResponse) -> Void
) {
completion(INRequestPaymentIntentResponse.init(code: .success, userActivity: nil))
return
}
func resolvePayer(
for intent: INRequestPaymentIntent,
with completion: @escaping (INRequestPaymentPayerResolutionResult) -> Void
) {
guard let payer = intent.payer else {
completion(INRequestPaymentPayerResolutionResult.needsValue())
return
}
completion(INRequestPaymentPayerResolutionResult.success(with: payer))
}
func resolveCurrencyAmount(
for intent: INRequestPaymentIntent,
with completion: @escaping (INRequestPaymentCurrencyAmountResolutionResult) -> Void
) {
let amount = intent.currencyAmount?.amount ?? 0
completion(
INRequestPaymentCurrencyAmountResolutionResult.success(with: INCurrencyAmount(amount: amount, currencyCode: "USD"))
)
}
func resolveNote(
for intent: INRequestPaymentIntent,
with completion: @escaping (INStringResolutionResult) -> Void
) {
guard let note = intent.note else {
completion(INStringResolutionResult.needsValue())
return
}
completion(INStringResolutionResult.success(with: note))
}
func confirm(
intent: INRequestPaymentIntent,
completion: @escaping (INRequestPaymentIntentResponse) -> Void
) {
let response = INRequestPaymentIntentResponse(code: .ready, userActivity: nil)
response.paymentRecord = INPaymentRecord(
payee: nil,
payer: intent.payer,
currencyAmount: intent.currencyAmount,
paymentMethod: nil,
note: intent.note,
status: .pending
)
completion(response)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment