GPWebpayNet.Sdk 1.3.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package GPWebpayNet.Sdk --version 1.3.2
NuGet\Install-Package GPWebpayNet.Sdk -Version 1.3.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GPWebpayNet.Sdk" Version="1.3.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GPWebpayNet.Sdk --version 1.3.2
#r "nuget: GPWebpayNet.Sdk, 1.3.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GPWebpayNet.Sdk as a Cake Addin
#addin nuget:?package=GPWebpayNet.Sdk&version=1.3.2

// Install GPWebpayNet.Sdk as a Cake Tool
#tool nuget:?package=GPWebpayNet.Sdk&version=1.3.2

<img src="logo.png" align="left" style="width:128px; margin-right: 20px;" />

GPWebpayNet

GPWebpayNet is a helper library for communication with GP webpay payment gateway and providing payments via HTTP. It follows documentation that can be found here.

GP webpay does not provide any SDK to be able to integrate their services in a simple way. They provide sample code in .NET3.5 (really, in 2017), Java and PHP. But only for evaluation that your process of signing payment request generates valid digest. Thats all. Another proble is that these examples are quite outdated - today, there is .NetCore2 that has different assemblies for working with ceritficates.

Version

  • Version 1.3.2 - 2018-10-12

Getting Started/Installing

Install-Package GPWebpayNet.Sdk -Version 1.3.2

.NET Framework Support

This SDK supports:

  • Full .NET Framework 4.6.1
  • .netstandart 1.6
  • .netstandart 2.0

Use Cases

You can use is together with WebAPI and SPA of with ASP MVC. It is up to you.

The base GP webpay process (simplified) is as follows:

  1. Generate payment request
  2. Send it via GET or POST go payment gateway
  3. Process GET request from GP webpay

The process with API and SPA is as follows:

  1. SPA send order request to API
  2. API generates payment request for GET (url) and sends it back to SPA
  3. SPA use url and redirects to it
  4. GP webpay send request to API
  5. API process request and redirect to SPA with corresponding result

The process with MVC is as follows:

  1. Send request to MVC
  2. MVC generates payment request for GET/POST and redirects to GP webpay
  3. GP webpay send request to MVC
  4. MVC process request and redirect to SPA with corresponding result

Project Structure

GPWebpayNet.Example

It contains examples how to use this SDK. For more details you can check GPWebpayNet.SDK.Spec too.

GPWebpayNet.SDK

The main project. The main project class is GPWebpayNet.Sdk.Services.ClientService that provides methods needed for payment process. Other classes are used by this class. Whole project is designed to be used with some IoC framework so classes are decoupled.

GPWebpayNet.Spec

It contains SDK unit tests.

Certificates

A library requires following keys:

  1. pfx key containing your private key and public key
  2. pem key containing GP Webpay public certificate

GP Webpay provides two keys that must be transformed to proper formats:

  1. Your private key file that must be transformed to pfx
  2. GP Webpay public certificate cer in der format
Certificate transformation
  1. Create a public crt from a private key received from GP webpay: openssl req -new -x509 -days 1826 -key PRIVATE_KEY_FROM_GP.key -out GENERATED_CERT_FROM_PRIVATE_KEY.crt
  2. Create pfx from private key got from GP Webpay and self created public crt certificate: openssl pkcs12 -export -out PFX_KEY.pfx -inkey PRIVATE_KEY_FROM_GP.key -in GENERATED_CERT_FROM_PRIVATE_KEY.crt
  3. Create pem certificate from GP public cer certificate needed by .NET Core: openssl x509 -inform der -in GPE_PUBLIC_SIGNING_CERTIFICATE.cer -out GPE_PUBLIC_SIGNING_CERTIFICATE.pem

So in the library use generated PFX_KEY.pfx key as a private key (with proper password) and GPE_PUBLIC_SIGNING_CERTIFICATE.pem as a public key (no password needed)

Usage Samples

Get Redirect URL for Payment Request
public string GetRedirectUrl()
{
    var loggerFactory = new LoggerFactory()
        .AddConsole();

    const string url = Constants.GPWebpayUrlTest;
    const string privateCertificateFile = "certs/client.pfx";
    const string privateCertificateFilePassword = "test";
    const string publicCertificateFile = "certs/server_pub.pem";
    const string publicCertificateFilePassword = null;
    var doc = new XmlDocument();
    doc.AppendChild(doc.CreateElement("Info"));

    var request = new PaymentRequest
    {
        MerchantNumber = "235235",
        OrderNumber = 2412,
        Amount = 65460,
        Currency = CurrencyCodeEnum.Eur,
        DepositFlag = 1,
        MerOrderNumber = "MerOrderNumber",
        Url = "https://www.example.org",
        Description = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
        MD = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
        PaymentMethod = PaymentMethodEnum.Mps,
        DisabledPaymentMethod = PaymentMethodEnum.Crd,
        PaymentMethods = new[] {PaymentMethodEnum.Mcm, PaymentMethodEnum.NotSet},
        Email = "user@example.org",
        ReferenceNumber = "77987",
        AddInfo = doc.DocumentElement,
        Lang = "CZ"
    };

    var encodingLogger = loggerFactory.CreateLogger<EncodingService>();
    var clientServiceLogger = loggerFactory.CreateLogger<ClientService>();
    var clientService = new ClientService(new EncodingService(encodingLogger),
        new PaymentRequestTransformer(), new PaymentResponseTransformer(), clientServiceLogger);

    return clientService.GenerateGPWebPayRedirectUrl(
        url,
        request,
        privateCertificateFile,
        privateCertificateFilePassword,
        publicCertificateFile, 
        publicCertificateFilePassword);
        
    // In case of `X509Certificate2` error on IIS or Azure while calling `GenerateGPWebPayRedirectUrl` or `PostRequestAsync`
    // call such method with different *keyStorageFlags* argument:
    // return clientService.GenerateGPWebPayRedirectUrl(
    //     url,
    //     request,
    //     privateCertificateFile,
    //     privateCertificateFilePassword,
    //     publicCertificateFile, 
    //     publicCertificateFilePassword,
    //     Encoding.DefaultEncoding,
    //     X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet);

}
Process Incomming GP webpay request
public void ProcessIncommingGPWPRequest()
{
    var loggerFactory = new LoggerFactory()
        .AddConsole();
            
    // Args from Request object
    var queryArgs = new QueryCollection(new Dictionary<string, StringValues>()
    {
        {"OPERATION", new StringValues("Operation")},
        {"ORDERNUMBER", new StringValues("12332")},
        {"PRCODE", new StringValues("0")},
        {"SRCODE", new StringValues("0")},
        {"RESULTTEXT", new StringValues("ResultText")},
        {"USERPARAM1", new StringValues("UserParam1")},
        {"DIGEST", new StringValues("Digest")},
        {"DIGEST1", new StringValues("Digest1")},
    });
            
    const string merchantNumber = "25236236";
    const string publicCertificateFile = "certs/server_pub.pem";
    const string publicCertificateFilePassword = null;
        
            
    var encodingLogger = loggerFactory.CreateLogger<EncodingService>();
    var clientServiceLogger = loggerFactory.CreateLogger<ClientService>();
    var clientService = new ClientService(new EncodingService(encodingLogger),
        new PaymentRequestTransformer(), new PaymentResponseTransformer(), clientServiceLogger);
    
    // Service will creates PaymentResponse from incomming args and validate response digest with 
    // public certificate provided by GPWP and then check if "PRCODE" and "SRCODE" values have correct or error values
    // ReSharper disable once UnusedVariable
    var paymentResponse = clientService.ProcessGPWebPayResponse(queryArgs, merchantNumber, publicCertificateFile, password);
}

Contributing

Pull requests, bug reports, and feature requests are welcome.

Authors

  • Marek Polak - Initial work - marazt

License

© 2017-2018 Marek Polak. This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

  • Enjoy it!
  • If you want, you can support this project too.

TODO Paypal Donate

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.3 1,030 10/3/2020
1.4.1 5,893 7/17/2020
1.3.3 868 10/18/2018
1.3.2 731 10/12/2018
1.3.1 749 10/8/2018
1.3.0 770 9/21/2018
1.2.0 996 3/16/2018
1.1.0 955 3/1/2018
1.0.0 965 2/24/2018

Added validation for input parameters DESCRIPTION and MD.