-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhonePeHelper.cs
173 lines (140 loc) · 4.92 KB
/
PhonePeHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Nop.Core.Domain.Payments;
using System;
using System.Security.Cryptography;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text;
namespace Nop.Plugin.Payments.PhonePe
{
/// <summary>
/// Represents PhonePe helper
/// </summary>
public class PhonePeHelper
{
#region Properties
/// <summary>
/// Get nopCommerce partner code
/// </summary>
public static string NopCommercePartnerCode => "nopCommerce_SP";
/// <summary>
/// Get the generic attribute name that is used to store an order total that actually sent to PhonePe (used to PDT order total validation)
/// </summary>
public static string OrderTotalSentToPhonePe => "OrderTotalSentToPhonePe";
#endregion
#region Methods
public static string SignRequest(string payload)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] payloadBytes = Encoding.UTF8.GetBytes(payload);
byte[] hashBytes = sha256.ComputeHash(payloadBytes);
// Convert the hash to a hexadecimal string
StringBuilder hashStringBuilder = new StringBuilder();
foreach (byte b in hashBytes)
{
hashStringBuilder.Append(b.ToString("x2"));
}
return hashStringBuilder.ToString();
}
}
public static string sha256_hash(String value)
{
StringBuilder Sb = new StringBuilder();
using (SHA256 hash = SHA256Managed.Create())
{
Encoding enc = Encoding.UTF8;
Byte[] result = hash.ComputeHash(enc.GetBytes(value));
foreach (Byte b in result)
Sb.Append(b.ToString("x2"));
}
return Sb.ToString();
}
/// <summary>
/// Gets a payment status
/// </summary>
/// <param name="paymentStatus">PhonePe payment status</param>
/// <param name="pendingReason">PhonePe pending reason</param>
/// <returns>Payment status</returns>
///
public static PaymentStatus GetPaymentStatus(string paymentStatus, string pendingReason)
{
var result = PaymentStatus.Pending;
if (paymentStatus == null)
paymentStatus = string.Empty;
if (pendingReason == null)
pendingReason = string.Empty;
switch (paymentStatus.ToUpper())
{
case "PAYMENT_PENDING":
result = (pendingReason.ToLowerInvariant()) switch
{
"authorization" => PaymentStatus.Authorized,
_ => PaymentStatus.Pending,
};
break;
case "PAYMENT_SUCCESS":
result = PaymentStatus.Paid;
break;
default:
break;
}
return result;
}
#endregion
}
public partial class ResponseData
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("data")]
public Data Data { get; set; }
}
public partial class Data
{
[JsonProperty("merchantId")]
public string MerchantId { get; set; }
[JsonProperty("merchantTransactionId")]
public string MerchantTransactionId { get; set; }
[JsonProperty("instrumentResponse")]
public InstrumentResponse InstrumentResponse { get; set; }
}
public partial class InstrumentResponse
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("redirectInfo")]
public RedirectInfo RedirectInfo { get; set; }
}
public partial class RedirectInfo
{
[JsonProperty("url")]
public Uri Url { get; set; }
[JsonProperty("method")]
public string Method { get; set; }
}
public partial class Welcome7
{
public static Welcome7 FromJson(string json) => JsonConvert.DeserializeObject<Welcome7>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome7 self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}