@ -12,9 +12,9 @@ import Stripe
@objc ( StripePaymentsPlugin ) class StripePaymentsPlugin : CDVPlugin , STPPaymentContextDelegate {
private var paymentStatusCallback : String ? = nil
private let customerContext : STPCustomerContext
private let paymentContext : STPPaymentContext
private var paymentStatusCallback : String = " "
private var customerContext : STPCustomerContext !
private var paymentContext : STPPaymentContext !
override func pluginInitialize ( ) {
super . pluginInitialize ( )
@ -27,8 +27,8 @@ import Stripe
// MARK: I n i t M e t h o d
@objc ( init : )
public func init ( command : CDVInvokedUrlCommand ) {
@objc ( beginStripe : )
public func beginStripe ( command : CDVInvokedUrlCommand ) {
let error = " The Stripe Publishable Key and ephemeral key generation URL are required "
guard let dict = command . arguments [ 0 ] as ? [ String : Any ] ? ? nil else {
@ -43,10 +43,10 @@ import Stripe
PluginConfig . ephemeralKeyUrl = dict [ " ephemeralKeyUrl " ] as ? String ? ? " "
PluginConfig . appleMerchantId = dict [ " appleMerchantId " ] as ? String ? ? " "
PluginConfig . companyName = dict [ " companyName " ] as ? String ? ? " "
PluginConfig . requestPaymentImmediately = dict [ " requestPaymentImmediately " ] as ? Boolean ? ? true
PluginConfig . requestPaymentImmediately = dict [ " requestPaymentImmediately " ] as ? Bool ? ? true
if headersDict = dict [ " extraHTTPHeaders " ] as ? [ String : String ] {
PluginConfig . parseExtraHeaders ( headersDict)
if let headersDict = dict [ " extraHTTPHeaders " ] as ? [ String : String ] {
PluginConfig . parseExtraHeaders ( dict: headersDict)
}
if ! self . verifyConfig ( ) {
@ -54,7 +54,7 @@ import Stripe
return
}
APIClient. shared . ephemeralKeyUrl = PluginConfig . ephemeralKeyUrl
Stripe APIClient. shared . ephemeralKeyUrl = PluginConfig . ephemeralKeyUrl
STPPaymentConfiguration . shared ( ) . companyName = PluginConfig . companyName
STPPaymentConfiguration . shared ( ) . publishableKey = PluginConfig . publishableKey
@ -62,7 +62,7 @@ import Stripe
STPPaymentConfiguration . shared ( ) . appleMerchantIdentifier = PluginConfig . appleMerchantId
}
customerContext = STPCustomerContext ( keyProvider : APIClient. shared )
customerContext = STPCustomerContext ( keyProvider : Stripe APIClient. shared )
paymentContext = STPPaymentContext ( customerContext : customerContext )
paymentContext . delegate = self
@ -90,18 +90,24 @@ import Stripe
return
}
let paymentOptions = PaymentOptions ( options)
let paymentOptions = PaymentOptions ( dict: options)
paymentContext . paymentAmount = paymentOptions . price
paymentContext . paymentCurrency = paymentOptions . currency
paymentContext . paymentCountry = paymentOptions . country
// A l l o w t h e s e t o b e o v e r r i d d e n
PluginConfig . requestPaymentImmediately = options [ " requestPaymentImmediately " ] as ? Bool ? ? PluginConfig . requestPaymentImmediately
if let headersDict = options [ " extraHTTPHeaders " ] as ? [ String : String ] {
PluginConfig . parseExtraHeaders ( dict : headersDict )
}
// T h i s d i a l o g c o l l e c t s a p a y m e n t m e t h o d f r o m t h e u s e r . W h e n t h e y c l o s e i t , y o u g e t a c o n t e x t
// c h a n g e e v e n t w i t h t h e p a y m e n t i n f o . N O c h a r g e h a s b e e n c r e a t e d a t t h a t p o i n t , N O s o u r c e
// h a s b e e n c r e a t e d f r o m t h e p a y m e n t m e t h o d . A l l t h a t h a s h a p p e n e d i s t h e u s e r e n t e r e d
// p a y m e n t d a t a a n d c l i c k e d ' o k ' . T h a t ' s a l l .
// A f t e r t h a t d i a l o g c l o s e s - a f t e r p a y m e n t C o n t e x t D i d C h a n g e i s c a l l e d w i t h
// a s e l e c t e d P a y m e n t M e t h o d - T H E N y o u w a n t t o c a l l r e q u e s t P a y m e n t .
paymentContext . presentPayment Method sViewController( )
paymentContext . presentPayment Option sViewController( )
successCallback ( command . callbackId , [ " status " : " PAYMENT_DIALOG_SHOWN " ] )
}
@ -126,57 +132,58 @@ import Stripe
// MARK: S T P P a y m e n t C o n t e x t D e l e g a t e
func paymentContext ( _ paymentContext : STPPaymentContext , didFailToLoadWithError error : Error ) {
let alertController = UIAlertController (
preferredStyle : . alert ,
retryHandler : { ( action ) in
// R e t r y p a y m e n t c o n t e x t l o a d i n g
paymentContext . retryLoading ( )
}
)
var message = error ? . localizedDescription ? ? " "
var message = error . localizedDescription
var callbackMessage : String = " "
if let customerKeyError = error as ? APIClient. CustomerKeyError {
if let customerKeyError = error as ? StripeAPIClient . CustomerKeyError {
switch customerKeyError {
case . ephemeralKeyUrl :
// F a i l s i l e n t l y u n t i l b a s e u r l s t r i n g i s s e t
callbackMessage = " [ERROR]: Please assign a value to ` APIClient.shared.ephemeralKeyUrl` before continuing. See `StripePaymentsPlugin.swift`."
callbackMessage = " [ERROR]: Please assign a value to `StripeAPIClient.shared.ephemeralKeyUrl` before continuing. See `StripePaymentsPlugin.swift`. "
case . invalidResponse :
// U s e c u s t o m e r k e y s p e c i f i c e r r o r m e s s a g e
callbackMessage = " [ERROR]: Missing or malformed response when attempting to call ` APIClient.shared.createCustomerKey`. Please check internet connection and backend response."
callbackMessage = " [ERROR]: Missing or malformed response when attempting to call ` Stripe APIClient.shared.createCustomerKey`. Please check internet connection and backend response."
message = " Could not retrieve customer information "
}
}
else {
// U s e g e n e r i c e r r o r m e s s a g e
callbackMessage = " [ERROR]: Unrecognized error while loading payment context: \( error ) "
message = error . localizedDescription ? ? " Could not retrieve payment information "
callbackMessage = " [ERROR]: Unrecognized error while loading payment context: \( error . localizedDescription ) "
message = " Could not retrieve payment information "
}
print ( callbackMessage )
errorCallback ( paymentStatusCallback , [ " error " : callbackMessage ] , keepCallback : true )
alertController . setMessage ( message ) // ? ?
let alertController = UIAlertController (
title : " " ,
message : message ,
preferredStyle : . alert
)
let retry = UIAlertAction ( title : " Retry " , style : . default , handler : { ( action ) in
// R e t r y p a y m e n t c o n t e x t l o a d i n g
self . paymentContext . retryLoading ( )
} )
alertController . addAction ( retry )
self . viewController . present ( alertController , animated : true , completion : nil )
}
func paymentContextDidChange ( _ paymentContext : STPPaymentContext ) {
var isLoading = paymentContext . isLoading
var isPaymentReady = paymentContext . selectedPaymentMethod != nil
let isLoading = paymentContext . l oading
let isPaymentReady = paymentContext . selectedPaymentOption != nil
var label = " "
var image = " "
// h t t p s : / / s t a c k o v e r f l o w . c o m / q u e s t i o n s / 1 1 5 9 2 3 1 3 / h o w - d o - i - s a v e - a - u i i m a g e - t o - a - f i l e
if selectedPaymentMethod = paymentContext . selectedPaymentMethod {
label = selectedPayment Method . label
if let selectedPaymentOption = paymentContext . selectedPaymentOption {
label = selectedPayment Option . label
image = " "
let paths = FileManager . default . urls ( for : . documentDirectory , in : . userDomainMask )
if let filePath = paths . first ? . appendingPathComponent ( " StripePaymentMethod.jpg " ) {
// S a v e i m a g e .
do {
try UIImageJPEGRepresentation( selectedPaymentMethod . image , 1 ) ? . write ( to : filePath , options : . atomic )
image = filePath
try selectedPaymentOption. image . jpegData ( compressionQuality : 1 ) ? . write ( to : filePath , options : . atomic )
image = filePath . absoluteString
}
catch { }
}
@ -230,11 +237,9 @@ import Stripe
case . error :
// U s e g e n e r i c e r r o r m e s s a g e
print ( " [ERROR]: Unrecognized error while finishing payment: \( String ( describing : error ) ) " ) ;
self . viewController . present ( UIAlertController ( message : " Could not complete payment " ) , animated : true )
resultMsg = [
" status " : " PAYMENT_COMPLETED_ERROR " ,
error : " [ERROR]: Unrecognized error while finishing payment: \( String ( describing : error ) ) "
" error " : " [ERROR]: Unrecognized error while finishing payment: \( String ( describing : error ) ) "
]
errorCallback ( paymentStatusCallback , resultMsg , keepCallback : true )
@ -247,26 +252,25 @@ import Stripe
}
func successCallback ( _ callbackId : String , _ data : [ String : Any ? ] , keepCallback : Bool = false ) {
var pluginResult = CDVPluginResult (
let pluginResult = CDVPluginResult (
status : . ok ,
messageAs : data
messageAs : data as [ AnyHashable : Any ]
)
pluginResult ? . setKeepCallbackAs ( keepCallback )
self . commandDelegate ! . send ( pluginResult , callbackId : callbackId )
}
func errorCallback ( _ callbackId : String , _ data : [ String : Any ? ] , keepCallback : Bool = false ) {
var pluginResult = CDVPluginResult (
let pluginResult = CDVPluginResult (
status : . error ,
messageAs : data
messageAs : data as [ AnyHashable : Any ]
)
pluginResult ? . setKeepCallbackAs ( keepCallback )
self . commandDelegate ! . send ( pluginResult , callbackId : callbackId )
}
func verifyConfig ( ) -> Bool {
return PluginConfig . publishableKey != nil && ! PluginConfig . publishableKey ! . isEmpty
&& PluginConfig . ephemeralKeyUrl != nil && ! PluginConfig . ephemeralKeyUrl ! . isEmpty
return ! PluginConfig . publishableKey . isEmpty && ! PluginConfig . ephemeralKeyUrl . isEmpty
}
}