Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using SwishApi;
- using SwishApi.Models;
- using Environment = System.Environment;
- namespace SwishPayout.Pages
- {
- public class PayoutModel : PageModel
- {
- [BindProperty]
- public string? PayoutTo { get; set; }
- [BindProperty]
- public string? Personnummer { get; set; }
- [BindProperty]
- public decimal Amount { get; set; }
- [BindProperty]
- public string? Message { get; set; }
- public bool ShowSuccessMessage { get; set; }
- public bool ShowErrorMessage { get; set; }
- public string CallbackErrorMessage { get; set; } = "";
- public void OnPost()
- {
- var clientCertificate = new SwishApi.Models.ClientCertificate()
- {
- CertificateFilePath = "Cert//Swish_Merchant_TestCertificate_1234679304.pfx",
- Password = "swish"
- };
- string certificatePath = Environment.CurrentDirectory + "\\Cert\\Swish_Merchant_TestSigningCertificate_1234679304.pfx";
- var payoutClient = new SwishApi.PayoutClient(clientCertificate, "https://eofvqci6optquip.m.pipedream.net", "1234", "1234679304", true, SwishApi.Environment.Emulator);
- string instructionUUID = Guid.NewGuid().ToString("N").ToUpper();
- // Anropa MakePayoutRequest med formulärvärden.
- var response = payoutClient.MakePayoutRequest(PayoutTo, Personnummer, Amount, Message, instructionUUID, "7d70445ec8ef4d1e3a713427e973d097", new SwishApi.Models.ClientCertificate() { CertificateFilePath = certificatePath, Password = "swish" });
- if (string.IsNullOrEmpty(response.Error))
- {
- Console.WriteLine("Location: " + response.Location);
- // Vänta 5 sekunder så att betalningsförfrågan har behandlats.
- System.Threading.Thread.Sleep(5000);
- // Kontrollera betalningsstatusen.
- var statusResponse = payoutClient.CheckPayoutStatus(response.Location);
- if (string.IsNullOrEmpty(statusResponse.errorCode))
- {
- // Betalningen har lyckats.
- Console.WriteLine("Status: " + statusResponse.status);
- ShowSuccessMessage = true;
- }
- else
- {
- // Fel vid betalning.
- Console.WriteLine("CheckPayoutResponse: " + statusResponse.errorCode + " - " + statusResponse.errorMessage);
- ShowErrorMessage = true;
- CallbackErrorMessage = statusResponse.errorMessage;
- }
- }
- else
- {
- // Fel vid betalningsförfrågan.
- Console.WriteLine("MakePayoutRequest - ERROR: " + response.Error);
- ShowErrorMessage = true;
- CallbackErrorMessage = response.Error;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement