-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (55 loc) · 2.59 KB
/
Copy pathProgram.cs
File metadata and controls
64 lines (55 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Demonstrates wiring up the Monnify SDK outside ASP.NET Core (a worker service, a CLI tool,
// a console job, etc.) via the generic host, then calling a couple of typed clients.
//
// Before running, set these environment variables to your own sandbox credentials:
// MONNIFY_API_KEY, MONNIFY_SECRET_KEY, MONNIFY_CONTRACT_CODE
// (find ApiKey/SecretKey/ContractCode on your Monnify dashboard under Settings > API Keys)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Monnify;
using Monnify.Banks;
using Monnify.Collections;
using Monnify.Exceptions;
var builder = Host.CreateApplicationBuilder(args);
var apiKey = Environment.GetEnvironmentVariable("MONNIFY_API_KEY")
?? throw new InvalidOperationException("Set the MONNIFY_API_KEY environment variable.");
var secretKey = Environment.GetEnvironmentVariable("MONNIFY_SECRET_KEY")
?? throw new InvalidOperationException("Set the MONNIFY_SECRET_KEY environment variable.");
var contractCode = Environment.GetEnvironmentVariable("MONNIFY_CONTRACT_CODE")
?? throw new InvalidOperationException("Set the MONNIFY_CONTRACT_CODE environment variable.");
builder.Services.AddMonnify(options =>
{
options.ApiKey = apiKey;
options.SecretKey = secretKey;
options.Environment = MonnifyEnvironment.Sandbox;
});
using var host = builder.Build();
var banks = host.Services.GetRequiredService<IMonnifyBanksClient>();
var collections = host.Services.GetRequiredService<IMonnifyCollectionsClient>();
try
{
var allBanks = await banks.GetBanksAsync();
Console.WriteLine($"Monnify knows about {allBanks.Count} banks. First five:");
foreach (var bank in allBanks.Take(5))
{
Console.WriteLine($" {bank.Code} - {bank.Name}");
}
var checkout = await collections.InitializeTransactionAsync(new InitializeTransactionRequest
{
Amount = 1000,
CustomerName = "Jane Doe",
CustomerEmail = "jane.doe@example.com",
PaymentReference = $"sample-{Guid.NewGuid():N}",
PaymentDescription = "Sample checkout from Monnify.Samples.ConsoleApp",
ContractCode = contractCode,
});
Console.WriteLine();
Console.WriteLine($"Checkout initialized: {checkout.TransactionReference}");
Console.WriteLine($"Redirect the customer to: {checkout.CheckoutUrl}");
}
catch (MonnifyApiException ex)
{
// Loud by default: Monnify rejected the request, and ex carries enough to act on it
// (ResponseCode/ResponseMessage for logic, RawResponseBody if you need to see everything).
Console.WriteLine($"Monnify rejected the request: {ex.ResponseCode} - {ex.ResponseMessage}");
}