import Foundation
import SquareMobilePaymentsSDK
import UIKit
import SwiftUI
class PaymentViewController: UIViewController, PaymentManagerDelegate {
private var paymentHandle: PaymentHandle?
func startPayment() {
let idempotencyKey = UUID().uuidString
let amountMoney = Money(amount: 100, currency: .USD)
let paymentParams = PaymentParameters(
idempotencyKey: idempotencyKey,
amountMoney: amountMoney,
processingMode: .autoDetect
)
paymentParams.note = "POS Order"
paymentParams.referenceID = "Order_\(idempotencyKey)"
let promptParams = PromptParameters(
mode: .default,
additionalMethods: .tapToPay
)
paymentHandle = MobilePaymentsSDK.shared.paymentManager.startPayment(
paymentParams,
promptParameters: promptParams,
from: self,
delegate: self
)
}
// MARK: - PaymentManagerDelegate
func paymentManager(_ paymentManager: PaymentManager, didStart payment: Payment) {
print("đĄ didStart: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFinish payment: Payment) {
print("â
didFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didCancel payment: Payment) {
print("âŞď¸ didFail: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFail payment: Payment, withError error: Error) {
print("â : \(payment.id) error: \(error.localizedDescription)")
}
func paymentManager(_ paymentManager: PaymentManager, willFinish payment: Payment) {
print("đwillFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, willCancel payment: Payment) {
print("đ willCancel: \(payment.id)")
}
}
HERE is my code, keep give me ERROR:
1: Type âPaymentViewControllerâ does not conform to protocol âPaymentManagerDelegateâ
2: Type âStringâ has no member âUSDâ ,
if i change to : let amountMoney = Money(amount: 100, currency: âUSDâ)
THEN : amountMoney: amountMoney, here will error :Argument type âMoneyâ does not conform to expected type âMoneyAmountâ
please help .
Iâm Square Dev AI, here to help answer your question!
Based on the information youâve provided, the following documentation will help answer your question:
Additional Documentation
Enable Apple Pay
Build on iOS
Enable Secure Remote Commerce - iOS
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
Based on the SDK code and the errors youâre encountering, hereâs the corrected version of your code:
import Foundation
import SquareMobilePaymentsSDK
import UIKit
import SwiftUI
class PaymentViewController: UIViewController, PaymentManagerDelegate {
private var paymentHandle: PaymentHandle?
func startPayment() {
let idempotencyKey = UUID().uuidString
// Fix 2: Use MoneyAmount instead of Money
let amountMoney = MoneyAmount(amount: 100, currency: .USD)
let paymentParams = PaymentParameters(
idempotencyKey: idempotencyKey,
amountMoney: amountMoney,
processingMode: .autoDetect
)
paymentParams.note = "POS Order"
paymentParams.referenceID = "Order_\(idempotencyKey)"
let promptParams = PromptParameters(
mode: .default,
additionalMethods: .tapToPay
)
paymentHandle = MobilePaymentsSDK.shared.paymentManager.startPayment(
paymentParams,
promptParameters: promptParams,
from: self,
delegate: self
)
}
// MARK: - PaymentManagerDelegate
func paymentManager(_ paymentManager: PaymentManager, didStartPayment payment: Payment) {
print("đĄ didStart: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFinishPayment payment: Payment) {
print("â
didFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didCancelPayment payment: Payment) {
print("âŞď¸ didFail: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, didFailPayment payment: Payment, withError error: Error) {
print("â : \(payment.id) error: \(error.localizedDescription)")
}
func paymentManager(_ paymentManager: PaymentManager, willFinishPayment payment: Payment) {
print("đwillFinish: \(payment.id)")
}
func paymentManager(_ paymentManager: PaymentManager, willCancelPayment payment: Payment) {
print("đ willCancel: \(payment.id)")
}
}
The key changes made to fix the errors:
- Fixed Error 1 - Protocol Conformance:
- Changed the delegate method names to match exactly what the protocol expects:
didStart
â didStartPayment
didFinish
â didFinishPayment
didCancel
â didCancelPayment
didFail
â didFailPayment
willFinish
â willFinishPayment
willCancel
â willCancelPayment
- Fixed Error 2 - Money Type:
- Changed
Money
to MoneyAmount
- The currency should be accessed as an enum case
.USD
rather than a string "USD"
Make sure you have imported the latest version of the Square Mobile Payments SDK. The SDK uses MoneyAmount
instead of Money
for representing monetary values, and the currency is defined as an enum rather than a string.
Also, ensure youâve properly initialized the SDK before using it:
// In your app delegate or early in app lifecycle:
MobilePaymentsSDK.shared.initialize()

HI BRYAN
i have tried your code, but some error .
1: Type âPaymentViewControllerâ does not conform to protocol âPaymentManagerDelegateâ
2: âany MoneyAmountâ cannot be constructed because it has no accessible initializers
and i clean and reinstall the sdk . and the error still there ? i think it might be have some issue about the proper import SquareMobilePaymentsSDK, any suggest ?
also i want to mention is that my project use Square In-App Payments SDK before ,and i dont know install both sdk will cause that problem ? but i have remove the Square In-App Payments SDK , and error still remain.
Oh, so do you have both SDKs? If so that wonât work. You can only use either Mobile Payments SDK or In-App Payments SDK. Not both in the same app. 
SO i had Square In-App Payments SDK at first ,and it works,
then i tried to add SquareMobilePaymentsSDK to my project, but with out removing Square In-App Payments SDK. so the error show up, so i realize maybe both SDK at some time is not good, and then i remove both SDK, and reinstall the SquareMobilePaymentsSDK. but now the error still shows, what did i do wrong ? and how can i fix it ? thanks Bryan.
do you think , is the COREPAYMENTCARD AND THREEDS_SDK FRAMEWORK remain in the project will cause problem ?
It wouldnât surprise me if thatâs causing problems. Is there a reason you kept them? Theyâre only used with In-App Payments SDK. 