Skip to content

Latest commit

 

History

104 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PayU Client

Branch Master
Build Build Status
NuGet NuGet version

Table of Contents

  1. Docs PayU
  2. Get Started
  3. Convention
  4. Example Requests
  5. Own Request

Docs

http://developers.payu.com/en/restapi.html#references_api_signature http://developers.payu.com/en/payu_express.html https://payu21.docs.apiary.io

Get Started

Settings

For Standard HttpClient

      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
            );

For IHttpClientFactory

Look on

      PayUClientSettings settings = new PayUClientSettings(
                PayUContainer.PayUApiUrl.Sandbox,
                "v2_1",
                "clientId",
                "clientSecret"
                "PayUHttpClient" // look on IHttpClientFactory example
            );

Token

PayUClient get or set token into/from cache.

Cache expire time = (Api token expire time - 30 seconds)

Client Credentials

Cache key - PayU_auth_token

Thats mean, client credentials token is per client instance.

Trusted Merchant

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)


IHttpClientFactory

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2

Now we could create PayUClient

Autofac

  var builder = new ContainerBuilder();
  ...
  builder.RegisterInstance(settings).As<PayUClientSettings>();
  builder.RegisterType<PayUClient>().As<IPayUClient>().SingleInstance();
  ...
  var container = builder.Build();

Create instance

  IPayUClient client = new PayUClient(settings, IHttpClientFactory);

Factory Settings

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
});

Standard HttpClient

If you don't use IHttpClientFactory PayUClient will create communication with own HttpClient Look on PayUApiHttpCommunicator.cs

Now we could create PayUClient

Autofac

  var builder = new ContainerBuilder();
  ...
  builder.RegisterInstance(settings).As<PayUClientSettings>();
  builder.RegisterType<PayUClient>().As<IPayUClient>().SingleInstance();
  ...
  var container = builder.Build();

Create instance

  PayUClient client = new PayUClient(settings);

Conventions

Request Response

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.


Order

Look on PayU Api documentation what response properties will return for different request kind.


Requests Examples

https://payu21.docs.apiary.io/#introduction

Get Order

  var result = await this.client.GetOrderAsync("123133", default(CancellationToken));

Create Order

  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));

Refund Order

  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));

Cancel Order

  var result = await this.client.DeleteCancelOrderAsync("orderId", default(CancellationToken));

Update Order

  var result = await this.client.PutUpdateOrderAsync("orderId", new UpdateOrderRequest("orderId", "orderStatus"),  default(CancellationToken));

PayMethods

  var result = await this.client.GetPayMethodsAsync(default(CancellationToken));

Payout

  var result = await this.client.PostPayoutAsync(new PayoutRequest("shopId", 422), default(CancellationToken));

Retrieve Payout

  var result = await this.client.GetRetrivePayoutAsync("PayoutId", default(CancellationToken));

Delete Token

  await this.client.DeleteTokenAsync("TokenId", default(CancellationToken));

Own Request

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))

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages