| Branch | Master |
|---|---|
| Build | |
| NuGet |
http://developers.payu.com/en/restapi.html#references_api_signature http://developers.payu.com/en/payu_express.html https://payu21.docs.apiary.io
PayUClientSettings settings = new PayUClientSettings(
PayUContainer.PayUApiUrl.Sandbox, // Url You could use string example from configuration or use const
"v2_1", // api version
"clientId", // clientId from shop configuration
"clientSecret" // clientId from shop configuration
); PayUClientSettings settings = new PayUClientSettings(
PayUContainer.PayUApiUrl.Sandbox,
"v2_1",
"clientId",
"clientSecret"
"PayUHttpClient" // look on IHttpClientFactory example
);PayUClient get or set token into/from cache.
Cache expire time = (Api token expire time - 30 seconds)
Cache key - PayU_auth_token
Thats mean, client credentials token is per client instance.
Cache key - PayU_auth_token_{email}_{extCustomerId}
var trusted = new TrustedMerchant("test@test.com", "2312")PayU_auth_token_test@test.com_2312
Thats mean, every trusted customer have own cache.
Every request what need this token have info in method name, and contains parameter for TrustedMerchant class instance (example)
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2
Now we could create PayUClient
var builder = new ContainerBuilder();
...
builder.RegisterInstance(settings).As<PayUClientSettings>();
builder.RegisterType<PayUClient>().As<IPayUClient>().SingleInstance();
...
var container = builder.Build(); IPayUClient client = new PayUClient(settings, IHttpClientFactory);Remember to create HttpClientHandler for factory, client couldn't work properly without this:
services.AddHttpClient("PayUHttpClient", c =>
{
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}).
ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = false,
SslProtocols = SslProtocols.Tls12
});If you don't use IHttpClientFactory PayUClient will create communication with own HttpClient
Look on PayUApiHttpCommunicator.cs
Now we could create PayUClient
var builder = new ContainerBuilder();
...
builder.RegisterInstance(settings).As<PayUClientSettings>();
builder.RegisterType<PayUClient>().As<IPayUClient>().SingleInstance();
...
var container = builder.Build(); PayUClient client = new PayUClient(settings);Request type are same as api json property type, For example sometimes amount could be number value or text value.
Every request where json object have mandatory properties, have constructor with validator.
Mandatory properties have private setters!!!
Optional json properties have null value handling so, if you don't fill it, they won't send by http.
Look on PayU Api documentation what response properties will return for different request kind.
https://payu21.docs.apiary.io/#introduction
var result = await this.client.GetOrderAsync("123133", default(CancellationToken)); var products = new List<Product>(2);
products.Add(new Product("Wireless mouse", "15000", "1"));
products.Add(new Product("HDMI cable", "6000", "1"));
var request = new OrderRequest("127.0.0.1", "145227", "RTV market", "PLN", "21000", products);
var result = await this.client.PostOrderAsync(request, default(CancellationToken));Or you can use Express request trusted_merchant grant type
http://developers.payu.com/en/payu_express.html
var products = new List<Product>(2);
products.Add(new Product("Wireless mouse", "15000", "1"));
products.Add(new Product("HDMI cable", "6000", "1"));
var request = new OrderRequest("127.0.0.1", "merchantPosId", "RTV market", "PLN", "21000", products);
request.PayMethods = new OrderPayMethodsRequest("CARD_TOKEN", "TOK_XATB7DF8ACXYTVQIPLWTVPFRKQE");
var result = await this.client.PostTrustedOrderAsync(request, new TrustedMerchant("email", "extCustomerId"), default(CancellationToken)); var refund = new RefundRequest("GPNG88VBW6151031GUEST000P01", new RefundRq("Refund", "Amount"));
var result = await this.client.PostRefund("orderId", refund, default(CancellationToken));Full Refund
var refund = new RefundRequest("GPNG88VBW6151031GUEST000P01", new RefundRq("Refund"));
var fullRefundResult = await this.client.PostRefund("orderId", refund, default(CancellationToken)); var result = await this.client.DeleteCancelOrderAsync("orderId", default(CancellationToken)); var result = await this.client.PutUpdateOrderAsync("orderId", new UpdateOrderRequest("orderId", "orderStatus"), default(CancellationToken)); var result = await this.client.GetPayMethodsAsync(default(CancellationToken)); var result = await this.client.PostPayoutAsync(new PayoutRequest("shopId", 422), default(CancellationToken)); var result = await this.client.GetRetrivePayoutAsync("PayoutId", default(CancellationToken)); await this.client.DeleteTokenAsync("TokenId", default(CancellationToken));If some of new type payment will release and you'll need implement it ASAP. You could create you own class definition (you may inheritance BaseOrder)
Example: New Request Definition
public class NewRequestType : BaseOrder
{
public OrderRequest(string customerIp, string merchantPosId, string description, string currencyCode, string totalAmount, IList<Product> products, string newJsonField)
: base(customerIp, merchantPosId, description, currencyCode, totalAmount, products) {}
[JsonProperty("NewFieldJsonPropertyName", NullValueHandling = NullValueHandling.Ignore)]
public string NewField { get; set; }
}now you can make request
this.client.CustomRequest<NewRequestType, OrderResponse>(new Uri("endpointUrl"), `NewRequestType instance`, HttpMethod, default(CancellationToken))if you need trusted_merchant
this.client.TrustedCustomRequest<NewRequestType, OrderResponse>(new Uri("endpointUrl"), `NewRequestType instance`, HttpMethod,new TrustedMerchant("email", "extCustomerId"), default(CancellationToken))