Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

➕ Add the success/failure handling from Dart side for Apple Pay #200

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a3b1b81
add the success/failure handling from dart for apple pay
YazeedAlKhalaf May 3, 2023
78dfe7b
enhance the example app Pay usage
YazeedAlKhalaf Jun 15, 2023
fcab163
move impl. of updatePaymentStatus to pay_ios and test it
YazeedAlKhalaf Aug 2, 2023
0d8c189
update the example app to showcase the new feature
YazeedAlKhalaf Aug 2, 2023
23b63f8
fix deprecated thing in a test
YazeedAlKhalaf Aug 2, 2023
cddc099
try to match the styling of the other fellow files
YazeedAlKhalaf Aug 2, 2023
1323120
remove dark theme from demo app
YazeedAlKhalaf Dec 20, 2023
4a48cc8
rename IOS to Ios in IOSPayMethodChannel
YazeedAlKhalaf Dec 20, 2023
a06e1ec
rename update payment status to update payment result
YazeedAlKhalaf Dec 20, 2023
cd07e7c
add docs for updatePaymentResult
YazeedAlKhalaf Dec 20, 2023
87268b8
ran the project using xcode 15
YazeedAlKhalaf Dec 20, 2023
8e8e194
make the demo app show a regular integration path
YazeedAlKhalaf Dec 20, 2023
8d236bb
move the defaultBinaryMessenger for test calling to a variable to inc…
YazeedAlKhalaf Jun 1, 2024
122d7ba
move completion handler after serialization is successful
YazeedAlKhalaf Jun 1, 2024
b09fd8c
enhance tests and their readability
YazeedAlKhalaf Jun 1, 2024
73fa61c
make updatePaymentResult throw an exception instead of failing silently
YazeedAlKhalaf Jun 1, 2024
747ff50
Merge branch 'main' of github.com:google-pay/flutter-plugin into add-…
YazeedAlKhalaf Jun 1, 2024
b39bddd
fix the linter complaining
YazeedAlKhalaf Jun 1, 2024
15fb1aa
just ran the app with flutter 3.22.1
YazeedAlKhalaf Jun 1, 2024
7b64e53
make the implementation better, thanks to @JlUgia
YazeedAlKhalaf Jun 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pay/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/pay_ios/ios"

SPEC CHECKSUMS:
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
integration_test: a1e7d09bd98eca2fc37aefd79d4f41ad37bdbbe5
pay_ios: 8c7beb9c61d885f3f51b61f75f8793023fc8843a

PODFILE CHECKSUM: fc81e398f362bae88bdf55239bd5cf842faad39f
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
2 changes: 2 additions & 0 deletions pay/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
<true/>
</dict>
</plist>
8 changes: 7 additions & 1 deletion pay/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class _PaySampleAppState extends State<PaySampleApp> {

void onApplePayResult(paymentResult) {
debugPrint(paymentResult.toString());
Pay({
YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
PayProvider.apple_pay: PaymentConfiguration.fromJsonString(
payment_configurations.defaultApplePay,
),
}).updatePaymentStatus(true);
}

@override
Expand Down Expand Up @@ -142,7 +147,8 @@ class _PaySampleAppState extends State<PaySampleApp> {
// Example pay button configured using a string
ApplePayButton(
paymentConfiguration: PaymentConfiguration.fromJsonString(
payment_configurations.defaultApplePay),
payment_configurations.defaultApplePay,
),
paymentItems: _paymentItems,
style: ApplePayButtonStyle.black,
type: ApplePayButtonType.buy,
Expand Down
6 changes: 6 additions & 0 deletions pay/lib/src/pay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class Pay {
_configurations![provider]!, paymentItems);
}

/// Update the payment status with the native platform.
Future<void> updatePaymentStatus(bool isSuccess) async {
await _assetInitializationFuture;
return _payPlatform.updatePaymentStatus(isSuccess);
}

/// Verifies that the selected provider has been previously configured or
/// throws otherwise.
Future throwIfProviderIsNotDefined(PayProvider provider) async {
Expand Down
5 changes: 5 additions & 0 deletions pay_ios/ios/Classes/PayPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PayPlugin: NSObject, FlutterPlugin {
private static let methodChannelName = "plugins.flutter.io/pay_channel"
private let methodUserCanPay = "userCanPay"
private let methodShowPaymentSelector = "showPaymentSelector"
private let methodUpdatePaymentStatus = "updatePaymentStatus"

private let paymentHandler = PaymentHandler()

Expand All @@ -48,6 +49,10 @@ public class PayPlugin: NSObject, FlutterPlugin {
paymentConfiguration: arguments["payment_profile"] as! String,
paymentItems: arguments["payment_items"] as! [[String: Any?]])

case methodUpdatePaymentStatus:
let isSuccess = call.arguments as! Bool
paymentHandler.updatePaymentStatus(isSuccess: isSuccess)

default:
result(FlutterMethodNotImplemented)
}
Expand Down
15 changes: 14 additions & 1 deletion pay_ios/ios/Classes/PaymentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum PaymentHandlerStatus {
/// paymentHandler.canMakePayments(stringArguments)
/// ```
class PaymentHandler: NSObject {

/// Holds the completion handler so it can be updated later on from the Flutter side.
var completionHandler: PaymentCompletionHandler?

/// Holds the current status of the payment process.
var paymentHandlerStatus: PaymentHandlerStatus!
Expand Down Expand Up @@ -169,6 +172,12 @@ class PaymentHandler: NSObject {

return paymentRequest
}

YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
func updatePaymentStatus(isSuccess: Bool) {
YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
// Call completion handler with the given success status
completionHandler?(isSuccess)
completionHandler = nil
}
}

/// Extension that implements the completion methods in the delegate to respond to user selection.
Expand All @@ -179,6 +188,11 @@ extension PaymentHandler: PKPaymentAuthorizationControllerDelegate {
}

func paymentAuthorizationController(_: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
// Store completion handler
YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
completionHandler = { isSuccess in
let status: PKPaymentAuthorizationStatus = isSuccess ? .success : .failure
completion(PKPaymentAuthorizationResult(status: status, errors: nil))
}

// Collect payment result or error and return if no payment was selected
guard let paymentResultData = try? JSONSerialization.data(withJSONObject: payment.toDictionary()) else {
Expand All @@ -190,7 +204,6 @@ extension PaymentHandler: PKPaymentAuthorizationControllerDelegate {
self.paymentResult(String(decoding: paymentResultData, as: UTF8.self))

paymentHandlerStatus = .authorized
completion(PKPaymentAuthorizationResult(status: PKPaymentAuthorizationStatus.success, errors: nil))
}

func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
Expand Down
6 changes: 6 additions & 0 deletions pay_platform_interface/lib/pay_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ class PayMethodChannel extends PayPlatform {

return jsonDecode(paymentResult);
}

/// Update the payment status with the native platform.
@override
Future<void> updatePaymentStatus(bool isSuccess) async {
return _channel.invokeMethod('updatePaymentStatus', isSuccess);
}
}
3 changes: 3 additions & 0 deletions pay_platform_interface/lib/pay_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ abstract class PayPlatform {
Future<Map<String, dynamic>> showPaymentSelector(
PaymentConfiguration paymentConfiguration,
List<PaymentItem> paymentItems);

/// Update the payment status received from the backend server.
Future<void> updatePaymentStatus(bool isSuccess);
YazeedAlKhalaf marked this conversation as resolved.
Show resolved Hide resolved
}