karl.haak pay.sofort

datenbanken programmierung asp.net

pay.sofort Beispiele

Foto von Karl Haak
pay.sofort

Anbei vier Code Beispiele für die Nutzung.


Config File

<?xml version="1.0" encoding="utf-8"?>
<TPaySofortConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Username>pay.sofort</Username>
  <ConnectionString>data source=localhost;initial catalog=Sofort;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework</ConnectionString>
  <Konfigurationsschluessel>11111:2222222:9e81ca8af371e806a9be01a8d120d62b</Konfigurationsschluessel>
  <SofortUeberweisungUrl>https://api.sofort.com/api/xml</SofortUeberweisungUrl>
  <SuccessUrl>http://www.shop.de/confirm.aspx</SuccessUrl>
  <AbortUrl>http://www.shop.de/abort.aspx</AbortUrl>
  <TimeoutUrl>http://www.shop.de/timeout.aspx</TimeoutUrl>
  <Notification_url_1>http://www.shop.de/notify.ashx?received</Notification_url_1>
  <Notification_url_1_notify_on>received</Notification_url_1_notify_on>
  <Notification_url_2>http://www.shop.de/notify.ashx?loss+pending+refunded</Notification_url_2>
  <Notification_url_2_notify_on>loss,pending,refunded</Notification_url_2_notify_on>
</TPaySofortConfig>
      

Read Write Config File

Im Betriebe wird hauptsächlich nur noch die Load Methode benötigt, um die Standardkonfiguration einzulesen.
Auf den Gebrauch der web.config (app.config) wurde bewusst verzichtet, um Neustarts der Applikation im Betrieb zu verhindern.

Der ConnectionString muss für den Betrieb im Webserver angepasst werden. Der Zugriff auf die DB über integrated security=True ist vom Webserver aus in der Regel nicht erfolgreich.

// create neu config object
TPaySofortConfig config = new TPaySofortConfig
{
    Konfigurationsschluessel = "11111:2222222:9e81ca8af371e806a9be01a8d120d62b",
    ConnectionString = "data source=localhost;initial catalog=Sofort;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework",
    SuccessUrl = "http://www.shop.de/confirm.aspx",
    AbortUrl = "http://www.shop.de/abort.aspx",
    TimeoutUrl = "http://www.shop.de/timeout.aspx",
    Notification_url_1 = "http://www.shop.de/notify.ashx?received",
    Notification_url_1_notify_on = "received",
    Notification_url_2 = "http://www.shop.de/notify.ashx?loss+pending+refunded",
    Notification_url_2_notify_on = "loss,pending,refunded"
};
 
// write to file
config.WriteToFile("sofort.xml");
 
// read from file using explicit filename
TPaySofortConfig config2 = TPaySofortConfig.LoadFromFile("sofort.xml");
 
// read from file using default filename (sofort.xml)
TPaySofortConfig config3 = TPaySofortConfig.LoadFromFile();

Initiate Payment

// load config from default location (bin dir)
TPaySofortConfig sofortConfig = TPaySofortConfig.LoadFromFile();
 
// create multipay manager with config values
TPaySofortMultipayManager man = new TPaySofortMultipayManager(sofortConfig);
 
// create temporary paySofortMultipay object (not persisted yet)
PaySofortMultipay paySofortMultipay = man.CreateMultipaySimple(10.0m);
 
// alter default values
paySofortMultipay.reason_1 = "pay.sofort - Test";
paySofortMultipay.reason_2 = string.Format("Zeit {0:HHmmss}", DateTime.Now);
paySofortMultipay.user_variable_1 = "Test";
paySofortMultipay.su_customer_protection = false;
 
// send request to provider sofort.com and persist communication in db
PaySofortNewTransaction paySofortNewTransaction = man.SendMultipay(paySofortMultipay);
 
// return url from sofort.com for further gui interaction
return paySofortNewTransaction.payment_url;

Process Callback

Nach erfolgreicher oder fehlerhafter Transaktion meldet sofortüberweisung eine Statusänderung an an eine der konfigurierten urls zurück.
In diesem Beispiel wird der Antwort von einem Generig Handler (ASHX Page) in ASP.NET aufgefangen.

pay.sofort wird die Notify Nachricht im SQL Server speichern und im Hintergrund sofort eine Statusanfrage zu der Transaktion durchfühen. Diese enthält dann alle Details und steht abschließend über das Objekt PaySofortTransaction zur Verfügung.

Die Verarbeitung der Daten im Webshop erfolgt dann im Anschluss an der mit dem Kommentar markierten Stelle. /TODO: processing shop goes here

<%@ WebHandler Language="C#" Class="notification" %>
 
using System.IO;
using System.Web;
using pay.sofort;
using pay.sofort.Models;
 
public class notification : IHttpHandler {
    
      // example notify message
      // <?xml version="1.0" encoding="UTF-8"?>
      //  <status_notification>
      //      <transaction>99999-53245-5483-4891</transaction>
      //      <time>2010-04-14T19:01:08+02:00</time>
      // </status_notification>
 
    public void ProcessRequest(HttpContext context)
    {
        string basePath = context.Server.MapPath("~");
        string fullPathConfigFile = Path.Combine(basePath, "APP_DATA//sofort.xml");
        TPaySofortConfig config = TPaySofortConfig.LoadFromFile(fullPathConfigFile);
        TPaySofortNotifyManager manager = new TPaySofortNotifyManager(config);
 
        Stream requestStream = context.Request.GetBufferlessInputStream();
 
        StreamReader sr = new StreamReader(requestStream);
        string payload = sr.ReadToEnd();
 
        if (string.IsNullOrWhiteSpace(payload))
        {
            context.Response.Write("no data");
            context.Response.StatusCode = 500;
            return;
        }
        
        PaySofortTransaction paySofortTransaction = manager.ProcessPaySofortNotify(payload, context.Request.QueryString.ToString(), context.Request.RawUrl);
        
        // TODO: processing shop goes here
         
        context.Response.StatusCode = 200;
 
        // HACK: Copies EntityFramework.SqlServer.dll to bin
        var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
 
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}