Skip to content

Commit 5fce7c1

Browse files
author
Andy Neil
committed
Allow for additional fields to be passed from a custom workflow
1 parent 42c87a4 commit 5fce7c1

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/Umbraco.Forms.Integrations.Crm.Hubspot/App_Plugins/UmbracoForms.Integrations/Crm/Hubspot/hubspot-field-mapper-template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<p>Umbraco Forms is not configured with a HubSpot CRM account.</p>
99
<p>To do this you can either create and save an API key into the <i>UmbracoForms.config</i> file.</p>
1010
<p>Or you can click <a ng-click="vm.openAuth()" style="text-decoration: underline">here</a> to complete an OAuth connection.</p>
11-
<p><em>If your browser is unable to process the automated connection, paste the provided authorization code below and click to complete the authentication.</em></dd>
11+
<p><em>If your browser is unable to process the automated connection, paste the provided authorization code below and click to complete the authentication.</em>
1212
<input type="text" placeholder="Enter authorization code" ng-model="vm.authorizationCode" />
1313
<umb-button type="button" disabled="vm.authorizationCode === ''" action="vm.authorize()" label="Authorize"></umb-button>
1414
</div>

src/Umbraco.Forms.Integrations.Crm.Hubspot/HubspotWorkflow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
4141
return WorkflowExecutionStatus.NotConfigured;
4242
}
4343

44-
var commandResult = _contactService.PostContactAsync(record, fieldMappings).GetAwaiter().GetResult();
44+
var commandResult = _contactService.PostContactAsync(record, fieldMappings, null).GetAwaiter().GetResult();
4545
switch (commandResult)
4646
{
4747
case CommandResult.NotConfigured:

src/Umbraco.Forms.Integrations.Crm.Hubspot/Services/HubspotContactService.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Umbraco.Forms.Integrations.Crm.Hubspot.Services
2222
public class HubspotContactService : IContactService
2323
{
2424
// Using a static HttpClient (see: https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/).
25-
private readonly static HttpClient s_client = new HttpClient();
25+
private static readonly HttpClient s_client = new HttpClient();
2626

2727
// Access to the client within the class is via ClientFactory(), allowing us to mock the responses in tests.
2828
internal static Func<HttpClient> ClientFactory = () => s_client;
@@ -35,7 +35,7 @@ public class HubspotContactService : IContactService
3535
private const string CrmApiBaseUrl = "https://api.hubapi.com/crm/v3/";
3636
private const string InstallUrlFormat = "https://app-eu1.hubspot.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}";
3737
private const string OAuthScopes = "oauth%20forms%20crm.objects.contacts.read%20crm.objects.contacts.write";
38-
private const string OAauthClientId = "1a04f5bf-e99e-48e1-9d62-6c25bf2bdefe";
38+
private const string OAuthClientId = "1a04f5bf-e99e-48e1-9d62-6c25bf2bdefe";
3939

4040
private const string OAuthBaseUrl = "https://hubspot-forms-auth.umbraco.com/"; // For local testing: "https://localhost:44364/"
4141
private static string OAuthRedirectUrl = OAuthBaseUrl;
@@ -55,13 +55,13 @@ public HubspotContactService(IFacadeConfiguration configuration, ILogger logger,
5555

5656
public AuthenticationMode IsAuthorizationConfigured() => GetConfiguredAuthenticationDetails().Mode;
5757

58-
public string GetAuthenticationUrl() => string.Format(InstallUrlFormat, OAauthClientId, OAuthRedirectUrl, OAuthScopes);
58+
public string GetAuthenticationUrl() => string.Format(InstallUrlFormat, OAuthClientId, OAuthRedirectUrl, OAuthScopes);
5959

6060
public async Task<AuthorizationResult> AuthorizeAsync(string code)
6161
{
6262
var tokenRequest = new GetTokenRequest
6363
{
64-
ClientId = OAauthClientId,
64+
ClientId = OAuthClientId,
6565
RedirectUrl = OAuthRedirectUrl,
6666
AuthorizationCode = code,
6767
};
@@ -127,7 +127,7 @@ public async Task<IEnumerable<Property>> GetContactPropertiesAsync()
127127
return properties.OrderBy(x => x.Label);
128128
}
129129

130-
public async Task<CommandResult> PostContactAsync(Record record, List<MappedProperty> fieldMappings)
130+
public async Task<CommandResult> PostContactAsync(Record record, List<MappedProperty> fieldMappings, Dictionary<string, string> additionalFields = null)
131131
{
132132
var authenticationDetails = GetConfiguredAuthenticationDetails();
133133
if (authenticationDetails.Mode == AuthenticationMode.Unauthenticated)
@@ -155,6 +155,15 @@ public async Task<CommandResult> PostContactAsync(Record record, List<MappedProp
155155
}
156156
}
157157

158+
if (additionalFields != null)
159+
{
160+
// Add any extra fields that got passed (from a custom workflow)
161+
foreach (var additionalField in additionalFields)
162+
{
163+
postData.Properties.Add(additionalField.Key, additionalField.Value);
164+
}
165+
}
166+
158167
// POST data to hubspot
159168
// https://api.hubapi.com/crm/v3/objects/contacts?hapikey=YOUR_HUBSPOT_API_KEY
160169
var requestUrl = $"{CrmApiBaseUrl}objects/contacts";
@@ -223,7 +232,7 @@ private async Task RefreshOAuthAccessToken(string refreshToken)
223232
{
224233
var tokenRequest = new RefreshTokenRequest
225234
{
226-
ClientId = OAauthClientId,
235+
ClientId = OAuthClientId,
227236
RedirectUrl = OAuthRedirectUrl,
228237
RefreshToken = refreshToken,
229238
};

src/Umbraco.Forms.Integrations.Crm.Hubspot/Services/IContactService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public interface IContactService
2626

2727
Task<IEnumerable<Property>> GetContactPropertiesAsync();
2828

29-
Task<CommandResult> PostContactAsync(Record record, List<MappedProperty> fieldMappings);
29+
Task<CommandResult> PostContactAsync(Record record, List<MappedProperty> fieldMappings, Dictionary<string, string> additionalFields);
3030
}
3131
}

0 commit comments

Comments
 (0)