Easily accept payments through Apple Pay or Credit Card (with managed 3DS step) in your Flutter app with Moyasar.
Use this plugin to support:
- Apple Pay: Quickly and safely accept Apple Pay payments.
- Credit Card: Easily accept many card companies while not worrying about managing the required 3DS step.
Complete the following steps to easily accept Apple Pay payments:
- Follow this guide to setup your Apple developer account and integrate it with Moyasar.
- Follow this guide to enable accepting Apple Pay in your application using xCode.
Due to depending on the flutter_webview
package to manage the 3DS step, make sure to set the correct minSdkVersion in android/app/build.gradle if it was previously lower than 19:
android {
defaultConfig {
minSdkVersion 19
}
}
flutter pub add moyasar
import 'package:flutter/material.dart';
import 'package:moyasar/moyasar.dart';
class PaymentMethods extends StatelessWidget {
PaymentMethods({super.key});
final paymentConfig = PaymentConfig(
publishableApiKey: 'YOUR_API_KEY',
amount: 25758, // SAR 257.58
description: 'order #1324',
metadata: {'size': '250g'},
creditCard: CreditCardConfig(saveCard: true, manual: false),
applePay: ApplePayConfig(merchantId: 'YOUR_MERCHANT_ID', label: 'YOUR_STORE_NAME', manual: false),
);
void onPaymentResult(result) {
if (result is PaymentResponse) {
switch (result.status) {
case PaymentStatus.paid:
// handle success.
break;
case PaymentStatus.failed:
// handle failure.
break;
}
}
}
@override
Widget build(BuildContext context) {
return Column(
children: [
ApplePay(
config: paymentConfig,
onPaymentResult: onPaymentResult,
),
const Text("or"),
CreditCard(
config: paymentConfig,
onPaymentResult: onPaymentResult,
)
],
);
}
}
// Unified config for Moyasar API
final paymentConfig = PaymentConfig(
publishableApiKey: 'YOUR_API_KEY',
amount: 25758, // SAR 257.58
description: 'order #1324',
metadata: {'size': '250g'},
creditCard: CreditCardConfig(saveCard: true, manual: false),
applePay: ApplePayConfig(
merchantId: 'YOUR_MERCHANT_ID',
label: 'YOUR_STORE_NAME',
manual: false),
);
// Callback once the user clicks on the custom Apple Pay widget
void onSubmitApplePay(applePay) async {
final source = ApplePayPaymentRequestSource(
applePay['token'], (paymentConfig.applePay as ApplePayConfig).manual);
final paymentRequest = PaymentRequest(paymentConfig, source);
final result = await Moyasar.pay(
apiKey: paymentConfig.publishableApiKey,
paymentRequest: paymentRequest);
onPaymentResult(result);
}
// Callback once the user fills & submit their CC information using the custom form widget
void onSubmitCcForm() async {
final source = CardPaymentRequestSource(
creditCardData: CardFormModel(
name: 'John Doe',
number: '4111111111111111',
month: '05',
year: '2025'),
tokenizeCard: (paymentConfig.creditCard as CreditCardConfig).saveCard,
manualPayment: (paymentConfig.creditCard as CreditCardConfig).manual);
final paymentRequest = PaymentRequest(paymentConfig, source);
final result = await Moyasar.pay(
apiKey: paymentConfig.publishableApiKey,
paymentRequest: paymentRequest);
onPaymentResult(result);
}
// Unified payment result processor
void onPaymentResult(result) {
if (result is PaymentResponse) {
switch (result.status) {
case PaymentStatus.initiated:
// handle 3DS redirection.
break;
case PaymentStatus.paid:
// handle success.
break;
case PaymentStatus.failed:
// handle failure.
break;
}
}
}
Moyasar provides a sandbox environment for testing credit card payments without charging any real money. This allows you to test your integration and ensure that everything is working correctly before going live with actual payments. Learn more about our testing cards here
When using Apple Pay in the sandbox environment, you can try to make payments, but they will always fail. You can make sure that the integration has been completed once you go to Moyasar Dashboard and see the payment. Make sure to use a real device, not a simulator.
it's recommended that you test Apple Pay using our live environment, and voiding the payment after.
This upgrade changes how Apple Pay is configured. Do the following changes to complete the upgrade:
- Delete the
default_payment_profile_apple_pay.json
file under your assets file. - Update the
paymentConfig
instance to include the newapplePay
configuration.
final paymentConfig = PaymentConfig(
publishableApiKey: 'YOUR_API_KEY',
amount: 25758, // SAR 257.58
description: 'order #1324',
metadata: {'size': '250g'},
+ applePay: ApplePayConfig(merchantId: 'YOUR_MERCHANT_ID', label: 'YOUR_STORE_NAME'),
);