forked from ordercloud-api/ordercloud-dotnet-catalyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlueSnapService.cs
96 lines (86 loc) · 4.41 KB
/
BlueSnapService.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
using OrderCloud.Catalyst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderCloud.Integrations.Payment.BlueSnap
{
public class BlueSnapService : OCIntegrationService, ICreditCardSaver, ICreditCardProcessor
{
public BlueSnapService(BlueSnapConfig defaultConfig) : base(defaultConfig) { }
public async Task<string> GetIFrameCredentialAsync(OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var token = await BlueSnapClient.GetHostedPaymentFieldToken(config);
return token;
}
public async Task<CCTransactionResult> AuthorizeOnlyAsync(AuthorizeCCTransaction transaction, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var trans = BlueSnapTransactionMapper.ToBlueSnapCardTransaction(BlueSnapTransactionType.AUTH_ONLY, transaction);
var result = await BlueSnapClient.CreateCardTransaction(trans, config);
return BlueSnapTransactionMapper.ToCardTransactionResult(result);
}
public async Task<CCTransactionResult> CapturePriorAuthorizationAsync(FollowUpCCTransaction transaction, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var trans = BlueSnapTransactionMapper.ToBlueSnapCardTransaction(BlueSnapTransactionType.CAPTURE, transaction);
var result = await BlueSnapClient.CreateCardTransaction(trans, config);
return BlueSnapTransactionMapper.ToCardTransactionResult(result);
}
public async Task<CCTransactionResult> RefundCaptureAsync(FollowUpCCTransaction transaction, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var refund = BlueSnapRefundMapper.ToBlueSnapRefund(transaction);
var result = await BlueSnapClient.RefundCardTransaction(transaction.TransactionID, refund, config);
return BlueSnapRefundMapper.ToCardTransactionResult(result);
}
public async Task<CCTransactionResult> VoidAuthorizationAsync(FollowUpCCTransaction transaction, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var trans = BlueSnapTransactionMapper.ToBlueSnapCardTransaction(BlueSnapTransactionType.AUTH_REVERSAL, transaction);
var result = await BlueSnapClient.CreateCardTransaction(trans, config);
return BlueSnapTransactionMapper.ToCardTransactionResult(result);
}
public async Task<List<PCISafeCardDetails>> ListSavedCardsAsync(string customerID, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var shopper = await BlueSnapClient.GetVaultedShopper(customerID, config);
var cards = BlueSnapVaultedShopperMapper.ToCardList(shopper);
return cards;
}
public async Task<PCISafeCardDetails> GetSavedCardAsync(string customerID, string cardID, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var shopper = await BlueSnapClient.GetVaultedShopper(customerID, config);
var cards = BlueSnapVaultedShopperMapper.ToCardList(shopper);
return cards.FirstOrDefaultWithID(cardID);
}
public async Task<CardCreatedResponse> CreateSavedCardAsync(PaymentSystemCustomer customer, PCISafeCardDetails card, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var shopper = BlueSnapVaultedShopperMapper.BuildAddCardRequest(customer, card);
if (customer.CustomerAlreadyExists)
{
shopper = await BlueSnapClient.UpdateVaultedShopper(customer.ID, shopper, config);
}
else
{
shopper = await BlueSnapClient.CreateVaultedShopper(shopper, config);
}
card.SavedCardID = BlueSnapVaultedShopperMapper.BuildSavedCardID(card);
return new CardCreatedResponse()
{
CustomerID = shopper.vaultedShopperId,
Card = card
};
}
public async Task DeleteSavedCardAsync(string customerID, string cardID, OCIntegrationConfig overrideConfig = null)
{
var config = ValidateConfig<BlueSnapConfig>(overrideConfig ?? _defaultConfig);
var deleteCard = BlueSnapVaultedShopperMapper.BuildDeleteCardRequest(cardID);
await BlueSnapClient.UpdateVaultedShopper(customerID, deleteCard, config);
}
}
}