wayl_pay 0.2.4
wayl_pay: ^0.2.4 copied to clipboard
Typed Dart client for the Wayl merchant API.
example/wayl_pay_example.dart
import 'dart:io';
import 'package:wayl_pay/wayl_pay.dart';
Future<void> main() async {
final apiKey = Platform.environment['WAYL_API_KEY'];
if (apiKey == null || apiKey.isEmpty) {
stderr.writeln('Missing WAYL_API_KEY environment variable.');
stderr.writeln(
'Run with: WAYL_API_KEY=your_key dart run example/wayl_pay_example.dart',
);
exitCode = 64;
return;
}
final webhookUrl = Uri.parse(
Platform.environment['WAYL_WEBHOOK_URL'] ??
'https://example.com/webhooks/wayl',
);
final redirectionUrl = Uri.parse(
Platform.environment['WAYL_REDIRECTION_URL'] ??
'https://example.com/payment-complete',
);
final webhookSecret =
Platform.environment['WAYL_WEBHOOK_SECRET'] ?? 'example-webhook-secret';
final client = WaylPayClient(
apiKey: apiKey,
environment: WaylEnvironment.production,
);
try {
final auth = await client.authentication.verifyKey();
print('Authentication: ${auth.message}');
final referenceId = 'invoice-test-${DateTime.now().millisecondsSinceEpoch}';
final created = await client.links.create(
CreateLinkRequest(
env: LinkEnvironment.test,
referenceId: referenceId,
total: 25000,
customParameter: 'example-test-payment',
webhookUrl: webhookUrl,
webhookSecret: webhookSecret,
redirectionUrl: redirectionUrl,
lineItems: const <LinkLineItem>[
LinkLineItem(
label: 'Test payment',
amount: 25000,
type: LinkLineItemType.increase,
),
],
),
);
print('Created test link: ${created.data.url}');
print('Reference ID: ${created.data.referenceId}');
print('Status: ${created.data.status?.value ?? 'unknown'}');
print('Webhook URL: $webhookUrl');
print('Redirection URL: $redirectionUrl');
final fetched = await client.links.getByReferenceId(referenceId);
print('Fetched link status: ${fetched.data.status?.value ?? 'unknown'}');
print('Open the URL above to complete the test payment flow.');
} on WaylApiException catch (error) {
print('Wayl API error: ${error.message}');
for (final validationError in error.errors) {
print(' - ${validationError.path.join('.')}: ${validationError.message}');
}
} finally {
client.close();
}
}