-
Notifications
You must be signed in to change notification settings - Fork 27
/
BluePay.cs
executable file
·1590 lines (1471 loc) · 65.6 KB
/
BluePay.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BluePay C#.NET Sample code.
*
* Updated: 2021-07-06
*
* This code is Free. You may use it, modify it and redistribute it.
* If you do make modifications that are useful, Bluepay would love it if you donated
* them back to us!
*
*/
using System;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BluePayLibrary
{
/// <summary>
/// This is the BluePay object.
/// </summary>
public class BluePay
{
public const string RELEASE_VERSION = "3.0.7";
// required for every transaction
public string accountID = "";
public string URL = "";
public string secretKey = "";
public string mode = "";
public string api = "";
// required for auth or sale
public string paymentAccount = "";
public string cvv2 = "";
public string cardExpire = "";
public Regex track1And2 = new Regex(@"(%B)[\d\*]{0,19}\^([\w\s]*)\/([\w\s]*)([\s]*)\^[\d\*]{7}[\w*]*\?;[\d\*]{0,19}=[\d\*]{7}[\w*]*\?");
public Regex track2Only = new Regex(@";[\d\*]{0,19}=[\d\*]{7}[\w*]*\?");
public string swipeData = "";
public string routingNum = "";
public string accountNum = "";
public string accountType = "";
public string docType = "";
public string name1 = "";
public string name2 = "";
public string addr1 = "";
public string city = "";
public string state = "";
public string zip = "";
public string country = "";
// optional for auth or sale
public string addr2 = "";
public string phone = "";
public string email = "";
public string country = "";
public string companyName = "";
//Stored Parameters(Optional)
public string storedIndicator = "";
public string storedType = "";
public string storedId= "";
// transaction variables
public string amount = "";
public string transType = "";
public string paymentType = "";
public string masterID = "";
public string rebillID = "";
public string newCustToken = "";
public string custToken = "";
// rebill variables
public string doRebill = "";
public string rebillAmount = "";
public string rebillFirstDate = "";
public string rebillExpr = "";
public string rebillCycles = "";
public string rebillNextAmount = "";
public string rebillNextDate = "";
public string rebillStatus = "";
public string templateID = "";
// level2 variables
public string customID1 = "";
public string customID2 = "";
public string invoiceID = "";
public string orderID = "";
public string amountTip = "";
public string amountTax = "";
public string amountFood = "";
public string amountMisc = "";
public string memo = "";
// Generating Simple Hosted Payment Form URL fields
public string dba = "";
public string returnURL = "";
public string discoverImage = "";
public string amexImage = "";
public string protectAmount = "";
public string rebillProtect = "";
public string protectCustomID1 = "";
public string protectCustomID2 = "";
public string shpfFormID = "";
public string receiptFormID = "";
public string remoteURL = "";
public string shpfTpsHashType = "";
public string receiptTpsHashType = "";
public string cardTypes = "";
public string receiptTpsDef = "";
public string receiptTpsString = "";
public string receiptTamperProofSeal = "";
public string receiptURL = "";
public string bp10emuTpsDef = "";
public string bp10emuTpsString = "";
public string bp10emuTamperProofSeal = "";
public string shpfTpsDef = "";
public string shpfTpsString = "";
public string shpfTamperProofSeal = "";
// report fields
public string reportStartDate = "";
public string reportEndDate = "";
public string doNotEscape = "";
public string queryBySettlement = "";
public string queryByHierarchy = "";
public string excludeErrors = "";
public string backendID = "";
public string origin = "";
public string response = "";
public string TPS = "";
public string BPheaderstring = "";
public int numRetries = 0;
public string tpsHashType = "HMAC_SHA512";
// level 2 processing field
public Dictionary<string, string> level2Info = new Dictionary<string, string>();
// level 3 processing field
public List<Dictionary<string, string>> lineItems = new List<Dictionary<string, string>>();
public BluePay(string accountID, string secretKey, string mode)
{
this.accountID = accountID;
this.secretKey = secretKey;
this.mode = mode;
}
/// <summary>
/// Sets Customer Information
/// </summary>
/// <param name="name1"></param>
/// <param name="name2"></param>
/// <param name="addr1"></param>
/// <param name="city"></param>
/// <param name="state"></param>
/// <param name="zip"></param>
/// <param name="country"></param>
/// <param name="phone"></param>
/// <param name="email"></param>
/// <param name="companyName"></param>
/// <param name="storedIndicator"></param>
/// <param name="storedType"></param>
/// <param name="storedId"></param>
public void SetCustomerInformation(string firstName, string lastName, string address1 = null, string address2 = null, string city = null, string state = null, string zip = null, string country = null, string phone = null, string email = null, string companyName = null, string storedIndicator = null, string storedType = null, string storedId = null)
{
this.name1 = firstName;
this.name2 = lastName;
this.addr1 = address1;
this.addr2 = address2;
this.city = city;
this.state = state;
this.zip = zip;
this.country = country;
this.phone = phone;
this.email = email;
this.companyName = companyName;
this.storedIndicator = storedIndicator;
this.storedType = storedType;
this.storedId = storedId;
}
/// <summary>
/// Sets Credit Card Information
/// </summary>
/// <param name="ccNumber"></param>
/// <param name="ccExpiration"></param>
/// <param name="cvv2"></param>
public void SetCCInformation(string ccNumber = null, string ccExpiration = null, string cvv2 = null)
{
this.paymentType = "CREDIT";
this.paymentAccount = ccNumber;
this.cardExpire = ccExpiration;
this.cvv2 = cvv2;
}
/// <summary>
/// Sets Swipe Information Using Either Both Track 1 2, Or Just Track 2
/// </summary>
/// <param name="swipe"></param>
public void Swipe(string swipe)
{
this.paymentType = "CREDIT";
this.swipeData = swipe;
}
/// <summary>
/// Sets ACH Information
/// </summary>
/// <param name="routingNum"></param>
/// <param name="accountNum"></param>
/// <param name="accountType"></param>
/// <param name="docType"></param>
public void SetACHInformation(string routingNum, string accountNum, string accountType, string docType = null)
{
this.paymentType = "ACH";
this.routingNum = routingNum;
this.accountNum = accountNum;
this.accountType = accountType;
this.docType = docType;
}
/// <summary>
/// Adds information required for level 2 processing.
/// </summary>
public void AddLevel2Information(string taxRate = null, string goodsTaxRate = null, string goodsTaxAmount = null, string shippingAmount = null, string discountAmount = null, string custPO = null, string goodsTaxID = null, string taxID = null, string customerTaxID = null, string dutyAmount = null, string supplementalData = null, string cityTaxRate = null, string cityTaxAmount = null, string countyTaxRate = null, string countyTaxAmount = null, string stateTaxRate = null, string stateTaxAmount = null, string buyerName = null, string customerReference = null, string customerNumber = null, string shipName = null, string shipAddr1 = null, string shipAddr2 = null, string shipCity = null, string shipState = null, string shipZip = null, string shipCountry = null)
{
this.level2Info.Add("LV2_ITEM_TAX_RATE", taxRate);
this.level2Info.Add("LV2_ITEM_GOODS_TAX_RATE", goodsTaxRate);
this.level2Info.Add("LV2_ITEM_GOODS_TAX_AMOUNT", goodsTaxAmount);
this.level2Info.Add("LV2_ITEM_SHIPPING_AMOUNT", shippingAmount);
this.level2Info.Add("LV2_ITEM_DISCOUNT_AMOUNT", discountAmount);
this.level2Info.Add("LV2_ITEM_CUST_PO", custPO);
this.level2Info.Add("LV2_ITEM_GOODS_TAX_ID", goodsTaxID);
this.level2Info.Add("LV2_ITEM_TAX_ID", taxID);
this.level2Info.Add("LV2_ITEM_CUSTOMER_TAX_ID", customerTaxID);
this.level2Info.Add("LV2_ITEM_DUTY_AMOUNT", dutyAmount);
this.level2Info.Add("LV2_ITEM_SUPPLEMENTAL_DATA", supplementalData);
this.level2Info.Add("LV2_ITEM_CITY_TAX_RATE", cityTaxRate);
this.level2Info.Add("LV2_ITEM_CITY_TAX_AMOUNT", cityTaxAmount);
this.level2Info.Add("LV2_ITEM_COUNTY_TAX_RATE", countyTaxRate);
this.level2Info.Add("LV2_ITEM_COUNTY_TAX_AMOUNT", countyTaxAmount);
this.level2Info.Add("LV2_ITEM_STATE_TAX_RATE", stateTaxRate);
this.level2Info.Add("LV2_ITEM_STATE_TAX_AMOUNT", stateTaxAmount);
this.level2Info.Add("LV2_ITEM_BUYER_NAME", buyerName);
this.level2Info.Add("LV2_ITEM_CUSTOMER_REFERENCE", customerReference);
this.level2Info.Add("LV2_ITEM_CUSTOMER_NUMBER", customerNumber);
this.level2Info.Add("LV2_ITEM_SHIP_NAME", shipName);
this.level2Info.Add("LV2_ITEM_SHIP_ADDR1", shipAddr1);
this.level2Info.Add("LV2_ITEM_SHIP_ADDR2", shipAddr2);
this.level2Info.Add("LV2_ITEM_SHIP_CITY", shipCity);
this.level2Info.Add("LV2_ITEM_SHIP_STATE", shipState);
this.level2Info.Add("LV2_ITEM_SHIP_ZIP", shipZip);
this.level2Info.Add("LV2_ITEM_SHIP_COUNTRY", shipCountry);
}
/// <summary>
/// Adds a line item for level 3 processing. Repeat method for each item up to 99 items.
/// For Canadian and AMEX processors, ensure required Level 2 information is present.
/// </summary>
public void AddLineItem(string unitCost, string quantity, string itemSKU = null, string descriptor = null, string commodityCode = null, string productCode = null, string measureUnits = null, string itemDiscount = null, string taxRate = null, string goodsTaxRate = null, string taxAmount = null, string goodsTaxAmount = null, string cityTaxRate = null, string cityTaxAmount = null, string countyTaxRate = null, string countyTaxAmount = null, string stateTaxRate = null, string stateTaxAmount = null, string custSKU = null, string custPO = null, string supplementalData = null, string glAccountNumber = null, string divisionNumber = null, string poLineNumber = null, string lineItemTotal = null)
{
int i = this.lineItems.Count + 1;
string prefix = $"LV3_ITEM{i}_";
Dictionary<string, string> item = new Dictionary<string, string>
{
{ prefix + "UNIT_COST", unitCost },
{ prefix + "QUANTITY", quantity },
{ prefix + "ITEM_SKU", itemSKU },
{ prefix + "ITEM_DESCRIPTOR", descriptor },
{ prefix + "COMMODITY_CODE", commodityCode },
{ prefix + "PRODUCT_CODE", productCode },
{ prefix + "MEASURE_UNITS", measureUnits },
{ prefix + "ITEM_DISCOUNT", itemDiscount },
{ prefix + "TAX_RATE", taxRate },
{ prefix + "GOODS_TAX_RATE", goodsTaxRate },
{ prefix + "TAX_AMOUNT", taxAmount },
{ prefix + "GOODS_TAX_AMOUNT", goodsTaxAmount },
{ prefix + "CITY_TAX_RATE", cityTaxRate },
{ prefix + "CITY_TAX_AMOUNT", cityTaxAmount },
{ prefix + "COUNTY_TAX_RATE", countyTaxRate },
{ prefix + "COUNTY_TAX_AMOUNT", countyTaxAmount },
{ prefix + "STATE_TAX_RATE", stateTaxRate },
{ prefix + "STATE_TAX_AMOUNT", stateTaxAmount },
{ prefix + "CUST_SKU", custSKU },
{ prefix + "CUST_PO", custPO },
{ prefix + "SUPPLEMENTAL_DATA", supplementalData },
{ prefix + "GL_ACCOUNT_NUMBER", glAccountNumber },
{ prefix + "DIVISION_NUMBER", divisionNumber },
{ prefix + "PO_LINE_NUMBER", poLineNumber },
{ prefix + "LINE_ITEM_TOTAL", lineItemTotal }
};
this.lineItems.Add(item);
}
/// <summary>
/// Sets Rebilling Cycle Information. To be used with other functions to create a transaction.
/// </summary>
/// <param name="rebAmount"></param>
/// <param name="rebFirstDate"></param>
/// <param name="rebExpr"></param>
/// <param name="rebCycles"></param>
public void SetRebillingInformation(string rebAmount, string rebFirstDate, string rebExpr, string rebCycles)
{
this.doRebill = "1";
this.rebillFirstDate = rebFirstDate;
this.rebillExpr = rebExpr;
this.rebillCycles = rebCycles;
this.rebillAmount = rebAmount;
}
/// <summary>
/// Updates Rebilling Cycle
/// </summary>
/// <param name="rebillID"></param>
/// <param name="rebNextDate"></param>
/// <param name="rebExpr"></param>
/// <param name="rebCycles"></param>
/// <param name="rebAmount"></param>
/// <param name="rebNextAmount"></param>
/// <param name="templateID"></param>
public void UpdateRebillingInformation(string rebillID, string rebNextDate, string rebExpr, string rebCycles, string rebAmount, string rebNextAmount, string templateID = null)
{
this.transType = "SET";
this.api = "bp20rebadmin";
this.rebillID = rebillID;
this.rebillNextDate = rebNextDate;
this.rebillExpr = rebExpr;
this.rebillCycles = rebCycles;
this.rebillAmount = rebAmount;
this.rebillNextAmount = rebNextAmount;
this.templateID = templateID;
}
/// <summary>
/// Updates a rebilling cycle's payment information
/// </summary>
/// <param name="templateID"></param>
public void UpdateRebillPaymentInformation(string templateID)
{
this.templateID = templateID;
}
/// <summary>
/// Cancels Rebilling Cycle
/// </summary>
/// <param name="rebillID"></param>
public void CancelRebilling(string rebillID)
{
this.transType = "SET";
this.rebillStatus = "stopped";
this.rebillID = rebillID;
this.api = "bp20rebadmin";
}
/// <summary>
/// Gets a existing rebilling cycle's status
/// </summary>
/// <param name="rebillID"></param>
public void GetRebillStatus(string rebillID)
{
this.transType = "GET";
this.rebillID = rebillID;
this.api = "bp20rebadmin";
}
/// <summary>
/// Gets Report of Transaction Data
/// </summary>
/// <param name="reportStart"></param>
/// <param name="reportEnd"></param>
/// <param name="subaccountsSearched"></param>
/// <param name="doNotEscape"></param>
/// <param name="errors"></param>
/// <param name="backendID"></param>
/// <param name="origin"></param>
public void GetTransactionReport(string reportStartDate, string reportEndDate, string subaccountsSearched,
string doNotEscape = null, string excludeErrors = null, string backendID, string origin)
{
this.queryBySettlement = "0";
this.reportStartDate = reportStartDate;
this.reportEndDate = reportEndDate;
this.queryByHierarchy = subaccountsSearched;
this.backendID = backendID;
this.origin = origin;
this.doNotEscape = doNotEscape;
this.excludeErrors = excludeErrors;
this.api = "bpdailyreport2";
}
/// <summary>
/// Gets Report of Settled Transaction Data
/// </summary>
/// <param name="reportStart"></param>
/// <param name="reportEnd"></param>
/// <param name="subaccountsSearched"></param>
/// <param name="doNotEscape"></param>
/// <param name="errors"></param>
public void GetTransactionSettledReport(string reportStartDate, string reportEndDate, string subaccountsSearched,
string doNotEscape = null, string excludeErrors = null)
{
this.queryBySettlement = "1";
this.reportStartDate = reportStartDate;
this.reportEndDate = reportEndDate;
this.queryByHierarchy = subaccountsSearched;
this.doNotEscape = doNotEscape;
this.excludeErrors = excludeErrors;
this.api = "bpdailyreport2";
}
/// <summary>
/// Gets Details of a Transaction
/// </summary>
/// <param name="reportStart"></param>
/// <param name="reportEnd"></param>
/// <param name="errors"></param>
public void GetSingleTransQuery(string transactionID, string reportStartDate, string reportEndDate, string errors = null)
{
this.reportStartDate = reportStartDate;
this.reportEndDate = reportEndDate;
this.excludeErrors = errors;
this.masterID = transactionID;
this.api = "stq";
}
/// <summary>
/// Queries by Transaction ID. To be used with getSingleTransQuery
/// </summary>
/// <param name="transID"></param>
public void QueryByTransactionID(string transID)
{
this.masterID = transID;
}
/// <summary>
/// Queries by Payment Type. To be used with getSingleTransQuery
/// </summary>
/// <param name="payType"></param>
public void QueryByPaymentType(string payType)
{
this.paymentType = payType;
}
/// <summary>
/// Queries by Transaction Type. To be used with getSingleTransQuery
/// </summary>
/// <param name="transType"></param>
public void QueryBytransType(string transType)
{
this.transType = transType;
}
/// <summary>
/// Queries by Transaction Amount. To be used with getSingleTransQuery
/// </summary>
/// <param name="amount"></param>
public void QueryByAmount(string amount)
{
this.amount = amount;
}
/// <summary>
/// Queries by First Name (NAME1) . To be used with getSingleTransQuery
/// </summary>
/// <param name="name1"></param>
public void QueryByName1(string name1)
{
this.name1 = name1;
}
/// <summary>
/// Queries by Last Name (NAME2) . To be used with getSingleTransQuery
/// </summary>
/// <param name="name2"></param>
public void QueryByName2(string name2)
{
this.name2 = name2;
}
/// <summary>
/// Runs a Sale Transaction
/// </summary>
/// <param name="amount"></param>
/// <param name="masterID"></param>
public void Sale(string amount, string masterID = null, string customerToken = "")
{
this.transType = "SALE";
this.api = "bp10emu";
this.amount = amount;
this.masterID = masterID;
this.custToken = customerToken;
}
/// <summary>
/// Runs an Auth Transaction
/// </summary>
/// <param name="amount"></param>
/// <param name="masterID"></param>
public void Auth(string amount, string masterID = null, string newCustomerToken = "", string customerToken = "")
{
this.transType = "AUTH";
this.api = "bp10emu";
this.amount = amount;
this.masterID = masterID;
if (newCustomerToken != "" && newCustomerToken.ToLower() != "false")
{
this.newCustToken = newCustomerToken.ToLower() == "true" ? randomString() : newCustomerToken;
}
this.custToken = customerToken;
}
/// <summary>
/// Creates a random alphanumeric string
/// </summary>
/// <param name="amount"></param>
/// <param name="masterID"></param>
public string randomString(int length = 16)
{
Random random = new Random();
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmopqrstuv0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
/// <summary>
/// Runs a Refund Transaction
/// </summary>
/// <param name="masterID"></param>
/// <param name="amount"></param>
public void Refund(string masterID, string amount = null)
{
this.transType = "REFUND";
this.api = "bp10emu";
this.masterID = masterID;
this.amount = amount;
}
/// <summary>
/// Updates a Transaction
/// </summary>
/// <param name="masterID"></param>
/// <param name="amount"></param>
public void Update(string masterID, string amount = null)
{
this.transType = "UPDATE";
this.api = "bp10emu";
this.masterID = masterID;
this.amount = amount;
}
/// <summary>
/// Voids a transaction
/// </summary>
/// <param name="masterID"></param>
public void Void(string masterID)
{
this.transType = "VOID";
this.api = "bp10emu";
this.masterID = masterID;
}
/// <summary>
/// Runs a Capture Transaction
/// </summary>
/// <param name="masterID"></param>
/// <param name="amount"></param>
public void Capture(string masterID, string amount = null)
{
this.transType = "CAPTURE";
this.masterID = masterID;
this.amount = amount;
this.api = "bp10emu";
}
/// <summary>
/// Sets Custom ID Field
/// </summary>
/// <param name="customID1"></param>
public void SetCustomID1(string customID1)
{
this.customID1 = customID1;
}
/// <summary>
/// Sets Custom ID2 Field
/// </summary>
/// <param name="customID2"></param>
public void SetCustomID2(string customID2)
{
this.customID2 = customID2;
}
/// <summary>
/// Sets Invoice ID Field
/// </summary>
/// <param name="invoiceID"></param>
public void SetInvoiceID(string invoiceID)
{
this.invoiceID = invoiceID;
}
/// <summary>
/// Sets Order ID Field
/// </summary>
/// <param name="orderID"></param>
public void SetOrderID(string orderID)
{
this.orderID = orderID;
}
/// <summary>
/// Sets Amount Tip Field
/// </summary>
/// <param name="amountTip"></param>
public void SetAmountTip(string amountTip)
{
this.amountTip = amountTip;
}
/// <summary>
/// Sets Amount Tax Field
/// </summary>
/// <param name="amountTax"></param>
public void SetAmountTax(string amountTax)
{
this.amountTax = amountTax;
}
/// <summary>
/// Sets Amount Food Field
/// </summary>
/// <param name="amountFood"></param>
public void SetAmountFood(string amountFood)
{
this.amountFood = amountFood;
}
/// <summary>
/// Sets Amount Misc Field
/// </summary>
/// <param name="amountMisc"></param>
public void SetAmountMisc(string amountMisc)
{
this.amountMisc = amountMisc;
}
/// <summary>
/// Sets Memo Field
/// </summary>
/// <param name="memo"></param>
public void SetMemo(string memo)
{
this.memo = memo;
}
/// <summary>
/// Sets Phone Field
/// </summary>
/// <param name="Phone"></param>
public void SetPhone(string Phone)
{
this.phone = Phone;
}
/// <summary>
/// Sets Email Field
/// </summary>
/// <param name="Email"></param>
public void SetEmail(string Email)
{
this.email = Email;
}
/// <summary>
/// Sets Company_Name Field
/// </summary>
/// <param name="companyName"></param>
public void SetCompanyName(string companyName)
{
this.companyName = companyName;
}
/// <summary>
/// Calculates TAMPER_PROOF_SEAL for bp20post API
/// </summary>
public void CalcTPS()
{
string tamper_proof_seal = this.accountID
+ this.transType
+ this.amount
+ this.doRebill
+ this.rebillFirstDate
+ this.rebillExpr
+ this.rebillCycles
+ this.rebillAmount
+ this.masterID
+ this.mode;
this.TPS = GenerateTPS(tamper_proof_seal, this.tpsHashType);
}
/// <summary>
/// Calculates TAMPER_PROOF_SEAL for bp20rebadmin API
/// </summary>
public void CalcRebillTPS()
{
string tamper_proof_seal = this.accountID + this.transType + this.rebillID;
this.TPS = GenerateTPS(tamper_proof_seal, this.tpsHashType);
}
/// <summary>
/// Calculates TAMPER_PROOF_SEAL for bpdailyreport2 and stq APIs
/// </summary>
public void CalcReportTPS()
{
string tamper_proof_seal = this.accountID + this.reportStartDate + this.reportEndDate;
this.TPS = GenerateTPS(tamper_proof_seal, this.tpsHashType);
}
/// <summary>
/// Generates the TAMPER_PROOF_SEAL to used to validate each transaction
/// </summary>
public string GenerateTPS(string Message, string HashType)
{
if (this.secretKey == null)
{
return "SECRET KEY NOT PROVIDED";
}
string tpsHash = "";
ASCIIEncoding encode = new ASCIIEncoding();
if (HashType == "HMAC_SHA256")
{
byte[] SecretKeyBytes = encode.GetBytes(this.secretKey);
byte[] MessageBytes = encode.GetBytes(Message);
var Hmac = new HMACSHA256(SecretKeyBytes);
byte[] HashBytes = Hmac.ComputeHash(MessageBytes);
tpsHash = ByteArrayToString(HashBytes);
}
else if (HashType == "SHA512")
{
SHA512 sha512 = new SHA512CryptoServiceProvider();
byte[] buffer = encode.GetBytes(this.secretKey + Message);
byte[] Hash = sha512.ComputeHash(buffer);
tpsHash = ByteArrayToString(Hash);
}
else if (HashType == "SHA256")
{
SHA256 Sha256 = new SHA256CryptoServiceProvider();
byte[] Buffer = encode.GetBytes(this.secretKey + Message);
byte[] Hash = Sha256.ComputeHash(Buffer);
tpsHash = ByteArrayToString(Hash);
}
else if (HashType == "MD5")
{
MD5 Md5 = new MD5CryptoServiceProvider();
byte[] Buffer = encode.GetBytes(this.secretKey + Message);
byte[] Hash = Md5.ComputeHash(Buffer);
tpsHash = ByteArrayToString(Hash);
}
else
{
byte[] SecretKeyBytes = encode.GetBytes(this.secretKey);
byte[] MessageBytes = encode.GetBytes(Message);
var Hmac = new HMACSHA512(SecretKeyBytes);
byte[] HashBytes = Hmac.ComputeHash(MessageBytes);
tpsHash = ByteArrayToString(HashBytes);
}
return tpsHash;
}
//This is used to convert a byte array to a hex string
static string ByteArrayToString(byte[] arrInput)
{
int i;
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}
/// <summary>
/// Calls the methods necessary to generate a SHPF URL
/// Required arguments for generate_url:
/// merchantName: Merchant name that will be displayed in the payment page.
/// returnURL: Link to be displayed on the transacton results page. Usually the merchant's web site home page.
/// transactionType: SALE/AUTH -- Whether the customer should be charged or only check for enough credit available.
/// acceptDiscover: Yes/No -- Yes for most US merchants. No for most Canadian merchants.
/// acceptAmex: Yes/No -- Has an American Express merchant account been set up?
/// amount: The amount if the merchant is setting the initial amount.
/// protectAmount: Yes/No -- Should the amount be protected from changes by the tamperproof seal?
/// rebilling: Yes/No -- Should a recurring transaction be set up?
/// paymentTemplate: Select one of our payment form template IDs or your own customized template ID. If the customer should not be allowed to change the amount, add a 'D' to the end of the template ID. Example: 'mobileform01D'
/// mobileform01 -- Credit Card Only - White Vertical (mobile capable)
/// default1v5 -- Credit Card Only - Gray Horizontal
/// default7v5 -- Credit Card Only - Gray Horizontal Donation
/// default7v5R -- Credit Card Only - Gray Horizontal Donation with Recurring
/// default3v4 -- Credit Card Only - Blue Vertical with card swipe
/// mobileform02 -- Credit Card & ACH - White Vertical (mobile capable)
/// default8v5 -- Credit Card & ACH - Gray Horizontal Donation
/// default8v5R -- Credit Card & ACH - Gray Horizontal Donation with Recurring
/// mobileform03 -- ACH Only - White Vertical (mobile capable)
/// receiptTemplate: Select one of our receipt form template IDs, your own customized template ID, or "remote_url" if you have one.
/// mobileresult01 -- Default without signature line - White Responsive (mobile)
/// defaultres1 -- Default without signature line – Blue
/// V5results -- Default without signature line – Gray
/// V5Iresults -- Default without signature line – White
/// defaultres2 -- Default with signature line – Blue
/// remote_url - Use a remote URL
/// receiptTempRemoteURL: Your remote URL ** Only required if receipt_template = "remote_url".
/// Optional arguments for generate_url:
/// rebProtect: Yes/No -- Should the rebilling fields be protected by the tamperproof seal?
/// rebAmount: Amount that will be charged when a recurring transaction occurs.
/// rebCycles: Number of times that the recurring transaction should occur. Not set if recurring transactions should continue until canceled.
/// rebStartDate: Date (yyyy-mm-dd) or period (x units) until the first recurring transaction should occur. Possible units are DAY, DAYS, WEEK, WEEKS, MONTH, MONTHS, YEAR or YEARS. (ex. 2016-04-01 or 1 MONTH)
/// rebFrequency: How often the recurring transaction should occur. Format is 'X UNITS'. Possible units are DAY, DAYS, WEEK, WEEKS, MONTH, MONTHS, YEAR or YEARS. (ex. 1 MONTH)
/// customID1: A merchant defined custom ID value.
/// protectCustomID1: Yes/No -- Should the Custom ID value be protected from change using the tamperproof seal?
/// customID2: A merchant defined custom ID 2 value.
/// protectCustomID2: Yes/No -- Should the Custom ID 2 value be protected from change using the tamperproof seal?
/// </summary>
/// <param name="merchantName"></param>
/// <param name="returnURL"></param>
/// <param name="transactionType"></param>
/// <param name="acceptDiscover"></param>
/// <param name="acceptAmex"></param>
/// <param name="amount"></param>
/// <param name="protectAmount"></param>
/// <param name="rebilling"></param>
/// <param name="rebProtect"></param>
/// <param name="rebAmount"></param>
/// <param name="rebCycles"></param>
/// <param name="rebStartDate"></param>
/// <param name="rebFrequency"></param>
/// <param name="customID1"></param>
/// <param name="protectCustomID1"></param>
/// <param name="customID2"></param>
/// <param name="protectCustomID2"></param>
/// <param name="paymentTemplate"></param>
/// <param name="receiptTemplate"></param>
/// <param name="receiptTempRemoteURL"></param>
public string GenerateURL(string merchantName, string returnURL, string transactionType, string acceptDiscover, string acceptAmex, string amount = null, string protectAmount = "No", string rebilling = "No", string rebProtect = "Yes", string rebAmount = null, string rebCycles = null, string rebStartDate = null, string rebFrequency = null, string customID1 = null, string protectCustomID1 = "No", string customID2 = null, string protectCustomID2 = "No", string paymentTemplate = "mobileform01", string receiptTemplate = "mobileresult01", string receiptTempRemoteURL = null)
{
this.dba = merchantName;
this.returnURL = returnURL;
this.transType = transactionType;
this.discoverImage = acceptDiscover.ToUpper()[0] == 'Y' ? "discvr.gif" : "spacer.gif";
this.amexImage = acceptAmex.ToUpper()[0] == 'Y' ? "amex.gif" : "spacer.gif";
this.amount = amount;
this.protectAmount = protectAmount;
this.doRebill = rebilling.ToUpper()[0] == 'Y' ? "1" : "0";
this.rebillProtect = rebProtect;
this.rebillAmount = rebAmount;
this.rebillCycles = rebCycles;
this.rebillFirstDate = rebStartDate;
this.rebillExpr = rebFrequency;
this.customID1 = customID1;
this.protectCustomID1 = protectCustomID1;
this.customID2 = customID2;
this.protectCustomID2 = protectCustomID2;
this.shpfFormID = paymentTemplate;
this.receiptFormID = receiptTemplate;
this.remoteURL = receiptTempRemoteURL;
this.shpfTpsHashType = "HMAC_SHA512";
this.receiptTpsHashType = this.shpfTpsHashType;
this.tpsHashType = SetHashType(tpsHashType);
this.cardTypes = SetCardTypes();
this.receiptTpsDef = "SHPF_ACCOUNT_ID SHPF_FORM_ID RETURN_URL DBA AMEX_IMAGE DISCOVER_IMAGE SHPF_TPS_DEF SHPF_TPS_HASH_TYPE";
this.receiptTpsString = SetReceiptTpsString();
this.receiptTamperProofSeal = GenerateTPS(receiptTpsString, this.receiptTpsHashType);
this.receiptURL = SetReceiptURL();
this.bp10emuTpsDef = AddDefProtectedStatus("MERCHANT APPROVED_URL DECLINED_URL MISSING_URL MODE TRANSACTION_TYPE TPS_DEF TPS_HASH_TYPE");
this.bp10emuTpsString = SetBp10emuTpsString();
this.bp10emuTamperProofSeal = GenerateTPS(bp10emuTpsString, this.tpsHashType);
this.shpfTpsDef = AddDefProtectedStatus("SHPF_FORM_ID SHPF_ACCOUNT_ID DBA TAMPER_PROOF_SEAL AMEX_IMAGE DISCOVER_IMAGE TPS_DEF TPS_HASH_TYPE SHPF_TPS_DEF SHPF_TPS_HASH_TYPE");
this.shpfTpsString = SetShpfTpsString();
this.shpfTamperProofSeal = GenerateTPS(shpfTpsString, this.shpfTpsHashType);
return CalcURLResponse();
}
/// <summary>
/// Validates chosen hash type or returns default hash.
/// </summary>
public string SetHashType(string chosen_hash)
{
string default_hash = "HMAC_SHA512";
chosen_hash = chosen_hash.ToUpper();
string result = "";
string[] hashes = { "MD5", "SHA256", "SHA512", "HMAC_SHA256" };
int pos = Array.IndexOf(hashes, chosen_hash);
if (pos > -1){
result = chosen_hash;
} else {
result = default_hash;
}
return result;
}
/// <summary>
/// Sets the types of credit card images to use on the Simple Hosted Payment Form. Must be used with GenerateURL.
/// </summary>
public string SetCardTypes()
{
string creditCards = "vi-mc";
creditCards = discoverImage == "discvr.gif" ? (creditCards + "-di") : creditCards;
creditCards = amexImage == "amex.gif" ? (creditCards + "-am") : creditCards;
return creditCards;
}
/// <summary>
/// Sets the receipt Tamperproof Seal string. Must be used with GenerateURL.
/// </summary>
public string SetReceiptTpsString()
{
return accountID + receiptFormID + returnURL + dba + amexImage + discoverImage + receiptTpsDef + receiptTpsHashType;
}
/// <summary>
/// Sets the bp10emu string that will be used to create a Tamperproof Seal. Must be used with GenerateURL.
/// </summary>
public string SetBp10emuTpsString()
{
string bp10emu = accountID + receiptURL + receiptURL + receiptURL + mode + transType + bp10emuTpsDef + tpsHashType;
return AddStringProtectedStatus(bp10emu);
}
/// <summary>
/// Sets the Simple Hosted Payment Form string that will be used to create a Tamperproof Seal. Must be used with GenerateURL.
/// </summary>
public string SetShpfTpsString()
{
string shpf = shpfFormID + accountID + dba + bp10emuTamperProofSeal + amexImage + discoverImage + bp10emuTpsDef + tpsHashType + shpfTpsDef + shpfTpsHashType;
return AddStringProtectedStatus(shpf);
}
/// <summary>
/// Sets the receipt url or uses the remote url provided. Must be used with GenerateURL.
/// </summary>
public string SetReceiptURL()
{
string output = "";
if (receiptFormID == "remote_url")
output = remoteURL;
else
{
output = "https://secure.bluepay.com/interfaces/shpf?SHPF_FORM_ID=" + receiptFormID +
"&SHPF_ACCOUNT_ID=" + accountID +
"&SHPF_TPS_DEF=" + EncodeURL(receiptTpsDef) +
"&SHPF_TPS_HASH_TYPE=" + EncodeURL(receiptTpsHashType) +
"&SHPF_TPS=" + EncodeURL(receiptTamperProofSeal) +
"&RETURN_URL=" + EncodeURL(returnURL) +
"&DBA=" + EncodeURL(dba) +
"&AMEX_IMAGE=" + EncodeURL(amexImage) +
"&DISCOVER_IMAGE=" + EncodeURL(discoverImage);
}
return output;
}
/// <summary>
/// Adds optional protected keys to a string. Must be used with GenerateURL.
/// </summary>
public string AddDefProtectedStatus(string input)
{
if (protectAmount == "Yes")
input = input + " AMOUNT";
if (rebillProtect == "Yes")
input = input + " REBILLING REB_CYCLES REB_AMOUNT REB_EXPR REB_FIRST_DATE";
if (protectCustomID1 == "Yes")
input = input + " CUSTOM_ID";
if (protectCustomID2 == "Yes")
input = input + " CUSTOM_ID2";
return input;
}
/// <summary>
/// Adds optional protected values to a string. Must be used with GenerateURL.
/// </summary>
public string AddStringProtectedStatus(string input)
{