1.4.3
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 1.4.3 - 2020-10-01
- Fix of the missing MD parameter in PaymentResponse.
- Fix of the missing LANG parameter in url.
- More currencies codes added.
- Fix of the Issue #2. Set numeric values (OrderNumber, Amount) and constants to unsigned types to have bigger positive range and remove possibility to set negative values that have no sense here.
Install-Package GPWebpayNet.Sdk -Version 1.4.3
This SDK supports:
- Full .NET Framework 4.6.1
- .netstandart 1.6
- .netstandart 2.0
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:
- Generate payment request
- Send it via GET or POST go payment gateway
- Process GET request from GP webpay
The process with API and SPA is as follows:
- SPA send order request to API
- API generates payment request for GET (url) and sends it back to SPA
- SPA use url and redirects to it
- GP webpay send request to API
- API process request and redirect to SPA with corresponding result
The process with MVC is as follows:
- Send request to MVC
- MVC generates payment request for GET/POST and redirects to GP webpay
- GP webpay send request to MVC
- MVC process request and redirect to SPA with corresponding result
It contains examples how to use this SDK. For more details you can check GPWebpayNet.SDK.Spec too.
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.
It contains SDK unit tests.
A library requires following keys:
- pfx key containing your private key and public key
- pem key containing GP Webpay public certificate
GP Webpay provides two keys that must be transformed to proper formats:
- Your private key file that must be transformed to pfx
- GP Webpay public certificate cer in der format
- 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
- 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
- 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)
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);
}
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);
}
Pull requests, bug reports, and feature requests are welcome.
- Marek Polak - Initial work - marazt
© 2017-2018 Marek Polak. This project is licensed under the MIT License - see the LICENSE.md file for details.
- Enjoy it!
- If you want, you can support this project too.
TODO Paypal Donate