Subversion Repository Public Repository

ChrisCompleteCodeTrunk

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Specialized;
using SecureSubmit.Services;
using System.Configuration;
using SecureSubmit.Entities;
using SecureSubmit.Fluent.Services;

namespace Action_Tire_Payment_Processor
{
    public class Heartland_Process
    {
        /*
        public static readonly HpsServicesConfig HeartlandConfig = new HpsServicesConfig()
        {
            SecretApiKey = ConfigurationManager.AppSettings["HpsSecretApiKey"].ToString()
        };
        */

        public static readonly HpsServicesConfig HeartlandConfig = new HpsServicesConfig()
        {
            SiteId = Convert.ToInt32(ConfigurationManager.AppSettings["HpsSiteId"].ToString()),
            LicenseId = Convert.ToInt32(ConfigurationManager.AppSettings["HpsLicenseId"].ToString()),
            DeviceId = Convert.ToInt32(ConfigurationManager.AppSettings["HpsDeviceId"].ToString()),
            UserName = ConfigurationManager.AppSettings["HpsUserName"].ToString(),
            Password = ConfigurationManager.AppSettings["HpsPassword"].ToString(),
            DeveloperId = ConfigurationManager.AppSettings["HpsDeveloperId"].ToString(),
            VersionNumber = ConfigurationManager.AppSettings["HpsVersionNumber"].ToString(),
            ServiceUrl = ConfigurationManager.AppSettings["HpsServiceUrl"].ToString()
        };

        public static readonly HpsCreditService CreditService = new HpsCreditService(
            Heartland_Process.HeartlandConfig
        );

        public static int ProcessSalePayment(CardInfoForm cif, ref string resp, ref string authCode, ref string responseText, ref string transID)
        {
            
            int recID = HeartlandDbLog(cif);
            
            String firstname = cif.tbFirstName.Text.Trim();
            String lastname = cif.tbLastName.Text.Trim();
            String address1 = cif.tbAddress.Text.Trim();
            String city = cif.tbCity.Text.Trim();
            String state = cif.cmbStates.SelectedItem.ToString();
            String zip = cif.tbZip.Text.Trim();
            String amt = cif.tbAmount.Text.Trim();
            String cardNum = cif.tbCardNumber.Text.Trim();
            String cardExp = cif.tbExpirationDate.Text.Trim();
            String cardCVV = cif.tbCVV.Text.Trim();

            CardInfo cardInfo = new CardInfo(firstname, lastname, address1, city, state, zip, amt, cardNum, cardExp, cardCVV);

            if (cif.TransactionType.Equals("refund"))
            {
                transID = cif.tbTransactionID.Text.ToString();
                SqlConnection cn = new SqlConnection(Database.ConnectionString);
                cn.Open();
                SqlCommand cmd = new SqlCommand("SELECT CreationDate FROM Transactions WHERE [TransID] = @TransID", cn);
                cmd.Parameters.AddWithValue("@TransID", transID);                
                DateTime transactionDate = DateTime.Parse(cmd.ExecuteScalar().ToString());
                cmd.Dispose();
                cn.Close();
                cn.Dispose();
                bool useRefund = DateTime.Now.Date > transactionDate;    

                HpsTransaction hpsTransaction = null;

                switch(useRefund)
                {
                    case true:
                        {
                            try
                            {
                                hpsTransaction = CreditService.Refund(cardInfo.Amount, "USD", System.Convert.ToInt64(transID));
                            } catch
                            {
                                try
                                {
                                    hpsTransaction = CreditService.Void(System.Convert.ToInt64(transID));
                                }
                                catch { }
                            }
                            break;
                        }
                    case false:
                        {
                            try
                            {
                                hpsTransaction = CreditService.Void(System.Convert.ToInt64(transID));
                            } catch
                            {
                                try
                                {
                                    hpsTransaction = CreditService.Refund(cardInfo.Amount, "USD", cardInfo.ToHpsCreditCard());
                                }
                                catch { }
                            }
                            break;
                        }
                }
                string result = String.Format(
                    "response={0}&responsetext={1}&authcode={2}&transactionid={3}&avsresponse={4}&cvvresponse={5}&orderid={6}&type={7}&response_code={8}",
                    new object[]
                    {
                        "1",
                        hpsTransaction.ResponseText,
                        String.Empty,
                        hpsTransaction.TransactionId,
                        String.Empty,
                        String.Empty,
                        String.Empty,
                        "refund",
                        hpsTransaction.ResponseCode
                    }
                );
                resp = HeartlandDbLogPost(recID, result, ref authCode, ref responseText, ref transID, false);

            }
            else if (cif.TransactionType.Equals("sale") || cif.TransactionType.Equals("roa"))
            {
                HpsFluentCreditService service = new HpsFluentCreditService(Heartland_Process.HeartlandConfig);

                /*

                var auth = service.Authorize(cardInfo.Amount)
                    .WithCurrency("usd")
                    .WithCard(cardInfo.ToHpsCreditCard())
                    .WithCardHolder(cardInfo.ToHpsCardHolder())
                    .WithCardPresent(true)
                    .WithReaderPresent(true)
                    .WithAllowDuplicates(true)
                    .Execute();

                var details = service.Capture(auth.TransactionId).Execute();

                */

                var charge = service.Charge(cardInfo.Amount)
                    .WithCard(cardInfo.ToHpsCreditCard())
                    .WithCardHolder(cardInfo.ToHpsCardHolder())
                    .WithCardPresent(true)
                    .WithReaderPresent(true)
                    .WithAllowDuplicates(true)
                    .Execute();



                /*
                var authResponse = service.Authorize(cardInfo.Amount, "usd", cardInfo.ToHpsCreditCard(), cardInfo.ToHpsCardHolder());
                var tranactionDetails = CreditService.Capture(authResponse.TransactionId);                

                var charge = CreditService.Charge(
                    cardInfo.Amount, 
                    "usd", 
                    cardInfo.ToHpsCreditCard(), 
                    cardInfo.ToHpsCardHolder()
                );
                */


                string result = String.Format(
                    "response={0}&responsetext={1}&authcode={2}&transactionid={3}&avsresponse={4}&cvvresponse={5}&orderid={6}&type={7}&response_code={8}",
                    new object[]
                    {
                        "1",
                        charge.ResponseText,
                        charge.AuthorizationCode,
                        charge.TransactionId,
                        "Y",
                        "M",
                        String.Empty,
                        cif.TransactionType,
                        charge.ResponseCode
                    }
                );

                resp = HeartlandDbLogPost(recID, result, ref authCode, ref responseText, ref transID, false);

            }

            return recID;
        }



        public static int HeartlandDbLog(CardInfoForm cif)
        {
            string query = "INSERT INTO Transactions (StoreID, PrinterIP, OrderID, TransType, Amount, CardNbr, CardType, " +
            "CardTypeGroup, CardHolder, FirstName, LastName, ExpDate, Address, City, State, Zip, Phone, Fax, Email, DateCreated) VALUES (" +
            "@StoreID, @PrinterIP, @OrderID, @TransType, @Amount, @CardNbr, @CardType, " +
            "@CardTypeGroup, @CardHolder, @FirstName, @LastName, @ExpDate, @Address, @City, @State, @Zip, @Phone, @Fax, @Email, GETDATE()) " +
            "SELECT IDENT_CURRENT('Transactions') AS RecID";

            SqlCommand cmd = new SqlCommand(query, Database.sql);

            cmd.Parameters.AddWithValue("@StoreID", Settings.GetOption("STORE"));
            cmd.Parameters.AddWithValue("@PrinterIP", Settings.GetOption("PRINTERIP"));
            cmd.Parameters.AddWithValue("@OrderID", cif.tbOrderNumber.Text.Equals("") ? DBNull.Value : (object)cif.tbOrderNumber.Text);
            cmd.Parameters.AddWithValue("@TransType", cif.TransactionType);
            cmd.Parameters.AddWithValue("@Amount", cif.tbAmount.Text);
            cmd.Parameters.AddWithValue("@CardNbr", cif.tbCardNumber.Text.Equals("") || cif.tbCardNumber.Text.Length <= 4 ? "" : cif.tbCardNumber.Text.Substring(cif.tbCardNumber.Text.Length - 4, 4));
            cmd.Parameters.AddWithValue("@CardType", cif.CardType == null ? DBNull.Value : (object)cif.CardType);
            cmd.Parameters.AddWithValue("@CardTypeGroup", cif.CardTypeGroup == null ? DBNull.Value : (object)cif.CardTypeGroup);
            cmd.Parameters.AddWithValue("@CardHolder", cif.CardHolder == null ? DBNull.Value : (object)cif.CardHolder);
            cmd.Parameters.AddWithValue("@FirstName", cif.tbFirstName.Text.Equals("") ? DBNull.Value : (object)cif.tbFirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", cif.tbLastName.Text.Equals("") ? DBNull.Value : (object)cif.tbLastName.Text);
            cmd.Parameters.AddWithValue("@ExpDate", cif.tbExpirationDate.Text);
            cmd.Parameters.AddWithValue("@Address", cif.tbAddress.Text.Equals("") ? (DBNull.Value) : (object)cif.tbAddress.Text);
            cmd.Parameters.AddWithValue("@City", cif.tbCity.Text.Equals("") ? (DBNull.Value) : (object)cif.tbCity.Text);
            cmd.Parameters.AddWithValue("@State", cif.cmbStates.Text.Equals("") ? (DBNull.Value) : (object)cif.cmbStates.Text);
            cmd.Parameters.AddWithValue("@Zip", cif.tbZip.Text.Equals("") ? (DBNull.Value) : (object)cif.tbZip.Text);
            cmd.Parameters.AddWithValue("@Phone", cif.tbPhone.Text.Equals("") ? (DBNull.Value) : (object)cif.tbPhone.Text);
            cmd.Parameters.AddWithValue("@Fax", cif.tbFax.Text.Equals("") ? (DBNull.Value) : (object)cif.tbFax.Text);
            cmd.Parameters.AddWithValue("@Email", cif.tbEmail.Text.Equals("") ? (DBNull.Value) : (object)cif.tbEmail.Text);

            DataTable result = Database.Query(cmd);

            int RecID = -1;

            if (result.Rows.Count > 0)
            {
                RecID = int.Parse(result.Rows[0]["RecID"].ToString());
            }

            result.Dispose();

            return RecID;
        }

        public static string HeartlandDbLogPost(int RecID, string results, ref string authCode, ref string responseText, ref string transID, bool isFake)
        {
            if (isFake)
            {
                string query = "UPDATE Transactions SET Response = @Response, Response_Code = @ResponseCode, " +
                    "Response_Text = @ResponseText, AuthCode = @AuthCode, TransID = @TransID, AVS_Response = @AVSResponse, CVV_Response = @CVVResponse " +
                    "WHERE ID = @RecID";

                string transId = Environment.TickCount.ToString();
                SqlCommand cmd = new SqlCommand(query, Database.sql);
                cmd.Parameters.AddWithValue("@Response", "1");
                cmd.Parameters.AddWithValue("@ResponseCode", "100");
                cmd.Parameters.AddWithValue("@ResponseText", "SUCCESS");
                cmd.Parameters.AddWithValue("@AuthCode", "987654");
                cmd.Parameters.AddWithValue("@TransID", transId);
                cmd.Parameters.AddWithValue("@AVSResponse", "N");
                cmd.Parameters.AddWithValue("@CVVResponse", "M");
                cmd.Parameters.AddWithValue("@RecID", RecID);

                Database.NonQuery(cmd);

                authCode = "987654";
                responseText = "SUCCESS";
                transID = transId;
                return "1";
            }
            else
            {
                NameValueCollection whats = HttpUtility.ParseQueryString(results);

                string query = "UPDATE Transactions SET Response = @Response, Response_Code = @ResponseCode, " +
                    "Response_Text = @ResponseText, AuthCode = @AuthCode, TransID = @TransID, AVS_Response = @AVSResponse, CVV_Response = @CVVResponse " +
                    "WHERE ID = @RecID";

                SqlCommand cmd = new SqlCommand(query, Database.sql);
                cmd.Parameters.AddWithValue("@Response", whats["response"].ToString().Equals("") ? DBNull.Value : (object)whats["response"].ToString());
                cmd.Parameters.AddWithValue("@ResponseCode", whats["response_code"].ToString().Equals("") ? DBNull.Value : (object)whats["response_code"].ToString());
                cmd.Parameters.AddWithValue("@ResponseText", whats["responsetext"].ToString().Equals("") ? DBNull.Value : (object)whats["responsetext"].ToString());
                cmd.Parameters.AddWithValue("@AuthCode", whats["authcode"].ToString().Equals("") ? DBNull.Value : (object)whats["authcode"].ToString());
                cmd.Parameters.AddWithValue("@TransID", whats["transactionid"].ToString().Equals("") ? DBNull.Value : (object)whats["transactionid"].ToString());
                cmd.Parameters.AddWithValue("@AVSResponse", whats["avsresponse"].ToString().Equals("") ? DBNull.Value : (object)whats["avsresponse"].ToString());
                cmd.Parameters.AddWithValue("@CVVResponse", whats["cvvresponse"].ToString().Equals("") ? DBNull.Value : (object)whats["cvvresponse"].ToString());
                cmd.Parameters.AddWithValue("@RecID", RecID);

                Database.NonQuery(cmd);

                authCode = whats["authcode"].ToString().Trim();
                responseText = whats["responsetext"].ToString().Trim();
                transID = whats["transactionid"].ToString().Trim();
                return whats["response"].ToString().Trim();
            }
        }
    }
}

Commits for ChrisCompleteCodeTrunk/ATTP/ATPP_New/Heartland_Process.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000