Skip to content

Commit 2dff380

Browse files
authored
Merge pull request #25 from umbraco/feature/payment-provider-integration
Feature/payment provider integration
2 parents 4feb2bc + 90ece4d commit 2dff380

File tree

11 files changed

+112
-55
lines changed

11 files changed

+112
-55
lines changed

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/customer-details-mapper.controller.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function CustomerDetailsMapperController($scope, $routeParams, pickerResource, notificationsService, umbracoFormsIntegrationsCommerceEMerchantPayResource) {
1+
function CustomerDetailsMapperController($scope, notificationsService, emerchantpayService) {
22

33
var vm = this;
44

@@ -22,27 +22,25 @@
2222
vm.selectedField = "";
2323
}
2424

25-
vm.deleteMapping = function(index) {
26-
vm.mappings.splice(index);
25+
vm.deleteMapping = function (index) {
26+
27+
vm.mappings.splice(index, 1);
2728

2829
$scope.setting.value = JSON.stringify(vm.mappings);
2930
}
3031

3132

3233
function init() {
33-
vm.customerProperties = ["Email", "FirstName", "LastName", "Phone", "Address", "ZipCode", "City", "State", "Country", "Status", "UniqueId"];
34+
vm.customerProperties = ["Email", "FirstName", "LastName", "Phone", "Address", "ZipCode", "City", "State", "Country"];
3435

3536
vm.fields = [];
36-
vm.selectedCustomerProperty = "";
37-
vm.selectedField = "";
3837

39-
var formId = $routeParams.id;
38+
emerchantpayService.getFormFields(function (response) {
39+
vm.fields = response;
40+
});
4041

41-
if (formId !== -1) {
42-
pickerResource.getAllFields($routeParams.id).then(function (response) {
43-
vm.fields = response.data;
44-
});
45-
}
42+
vm.selectedCustomerProperty = "";
43+
vm.selectedField = "";
4644

4745
vm.mappings = $scope.setting.value
4846
? JSON.parse($scope.setting.value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function emerchantpayService($routeParams, pickerResource) {
2+
return {
3+
getFormFields: function (callback) {
4+
var formId = $routeParams.id;
5+
6+
if (formId !== -1) {
7+
pickerResource.getAllFields(formId).then(function (response) {
8+
callback(response.data);
9+
});
10+
} else callback([]);
11+
}
12+
};
13+
}
14+
15+
angular.module("umbraco.services")
16+
.factory("emerchantpayService", emerchantpayService)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function FieldPickerController($scope, emerchantpayService) {
2+
3+
var vm = this;
4+
5+
init();
6+
7+
vm.saveField = function () {
8+
$scope.setting.value = vm.selectedField;
9+
}
10+
11+
function init() {
12+
13+
vm.fields = [];
14+
15+
emerchantpayService.getFormFields(function (response) {
16+
vm.fields = response;
17+
});
18+
19+
if ($scope.setting && $scope.setting.value) vm.selectedField = $scope.setting.value;
20+
}
21+
}
22+
23+
angular.module("umbraco")
24+
.controller("UmbracoForms.Integrations.Commerce.eMerchantPay.FieldPickerController", FieldPickerController);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div ng-controller="UmbracoForms.Integrations.Commerce.eMerchantPay.FieldPickerController as vm">
2+
3+
<div>
4+
<select ng-model="vm.selectedField" ng-click="vm.saveField()">
5+
<option value="">Select field</option>
6+
<option ng-repeat="field in vm.fields" value="{{ field.id }}">{{ field.value }}</option>
7+
</select>
8+
</div>
9+
10+
</div>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"javascript": [
33
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/customer-details-mapper.controller.js",
4-
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currency.controller.js",
5-
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/emerchantpay.resource.js"
4+
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currency.controller.js",
5+
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/emerchantpay.resource.js",
6+
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/emerchantpay.service.js",
7+
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/field-picker.controller.js"
68
],
79
"css": [ "" ]
810
}

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Builders/MappingBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44

55
using Umbraco.Forms.Core.Persistence.Dtos;
6-
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers;
76
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
87

98
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Builders
@@ -23,16 +22,17 @@ public MappingBuilder SetValues(Record record, List<Mapping> mappings)
2322
Values.State = GetValue(nameof(MappingValues.State), record, mappings);
2423
Values.Country = GetValue(nameof(MappingValues.Country), record, mappings);
2524
Values.Phone = GetValue(nameof(MappingValues.Phone), record, mappings);
26-
Values.Status = GetValue(nameof(MappingValues.Status), record, mappings);
27-
Values.UniqueId = GetValue(nameof(MappingValues.UniqueId), record, mappings);
2825

2926
return this;
3027
}
3128

3229
private string GetValue(string propertyName, Record record, List<Mapping> mappings)
3330
{
34-
var key = mappings.First(p => p.CustomerProperty == propertyName).Field.Id;
35-
return record.RecordFields[Guid.Parse(key)].ValuesAsString();
31+
var mapping = mappings.FirstOrDefault(p => p.CustomerProperty == propertyName);
32+
33+
return mapping != null
34+
? record.RecordFields[Guid.Parse(mapping.Field.Id)].ValuesAsString()
35+
: string.Empty;
3636
}
3737

3838
public MappingValues Build() => Values;

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Constants.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ public class Constants
55
{
66
public const string WorkflowId = "b2731255-2e48-4345-9ae5-aaf5b8bfb10a";
77

8-
/// <summary>
9-
/// Fields mandatory for mapping:
10-
/// 1. FirstName
11-
/// 2. LastName
12-
/// 3. Address
13-
/// 4. ZipCode
14-
/// 5. City
15-
/// 6. State
16-
/// 7. Country
17-
/// 8. Email
18-
/// 9. Phone
19-
/// 10. Status
20-
/// 11. UniqueId
21-
/// </summary>
22-
public const int RequiredMappingsNo = 11;
23-
248
public static class Configuration
259
{
2610
public const string Settings = "Umbraco:Forms:Integrations:Commerce:eMerchantPay:Settings";

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/ExtensionMethods/CustomerDetailsMappingsExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
2+
using System.Linq;
33
using Newtonsoft.Json;
44

55
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
@@ -9,7 +9,7 @@ namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.ExtensionMethods
99
public static class CustomerDetailsMappingsExtensions
1010
{
1111
/// <summary>
12-
/// Parse consumer mappings and validate the correct number of mappings
12+
/// Parse consumer mappings and validate that at least email property is mapped.
1313
/// </summary>
1414
/// <param name="serializedDetails"></param>
1515
/// <param name="mappings"></param>
@@ -22,7 +22,7 @@ public static bool TryParseMappings(this string serializedDetails, out List<Mapp
2222

2323
mappings = JsonConvert.DeserializeObject<List<Mapping>>(serializedDetails);
2424

25-
return mappings.Count == Constants.RequiredMappingsNo;
25+
return mappings.Count > 0 && mappings.Any(p => p.CustomerProperty == nameof(MappingValues.Email));
2626
}
2727
}
2828
}

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Models/Dtos/MappingValues.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,5 @@ public class MappingValues
2020
public string Email { get; set; }
2121

2222
public string Phone { get; set; }
23-
24-
public string Status { get; set; }
25-
26-
public string UniqueId { get; set; }
2723
}
2824
}

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/PaymentProviderWorkflow.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,22 @@ public class PaymentProviderWorkflow : WorkflowType
5151
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currency.html")]
5252
public string Currency { get; set; }
5353

54-
[Core.Attributes.Setting("CustomerDetails",
54+
[Core.Attributes.Setting("Number of Items",
55+
Description = "Map number of items with form field. If selected, final amount will be Amount x NumberOfItems.",
56+
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/field-picker.html")]
57+
public string NumberOfItems { get; set; }
58+
59+
[Core.Attributes.Setting("Record Status",
60+
Description = "Map payment record status with form field",
61+
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/field-picker.html")]
62+
public string RecordStatus { get; set; }
63+
64+
[Core.Attributes.Setting("Record Payment Unique ID",
65+
Description = "Map payment unique ID with form field",
66+
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/field-picker.html")]
67+
public string UniqueId { get; set; }
68+
69+
[Core.Attributes.Setting("Customer Details",
5570
Description = "Map customer details with form fields",
5671
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/customer-details-mapper.html")]
5772
public string CustomerDetailsMappings { get; set; }
@@ -138,18 +153,29 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
138153
var formId = record.Form;
139154
var recordUniqueId = record.UniqueId;
140155
#endif
141-
var uniqueIdKey = mappings.First(p => p.CustomerProperty == nameof(MappingValues.UniqueId)).Field.Id;
142-
var statusKey = mappings.First(p => p.CustomerProperty == nameof(MappingValues.Status)).Field.Id;
156+
var uniqueIdKey = UniqueId;
157+
var statusKey = RecordStatus;
158+
159+
160+
var numberOfItems = string.IsNullOrEmpty(NumberOfItems)
161+
? 0
162+
#if NETCOREAPP
163+
: int.Parse(context.Record.RecordFields[Guid.Parse(NumberOfItems)].ValuesAsString());
164+
#else
165+
: int.Parse(record.RecordFields[Guid.Parse(NumberOfItems)].ValuesAsString());
166+
#endif
143167

144168
var payment = new PaymentDto
145169
{
146170
TransactionId = transactionId.ToString(),
147171
Usage = _paymentProviderSettings.Usage,
148-
NotificationUrl = $"{_paymentProviderSettings.UmbracoBaseUrl}/umbraco/api/paymentprovider/notifypayment?formId={formId}&recordUniqueId={recordUniqueId}&statusFieldId={statusKey}",
172+
NotificationUrl = $"{_paymentProviderSettings.UmbracoBaseUrl}umbraco/api/paymentprovider/notifypayment?formId={formId}&recordUniqueId={recordUniqueId}&statusFieldId={statusKey}",
149173
ReturnSuccessUrl = _urlHelper.GetPageUrl(int.Parse(SuccessUrl)),
150174
ReturnFailureUrl = _urlHelper.GetPageUrl(int.Parse(FailureUrl)),
151175
ReturnCancelUrl = _urlHelper.GetPageUrl(int.Parse(CancelUrl)),
152-
Amount = int.Parse(Amount),
176+
Amount = numberOfItems != 0
177+
? numberOfItems * int.Parse(Amount)
178+
: int.Parse(Amount),
153179
Currency = Currency,
154180
ConsumerId = consumer.Id,
155181
CustomerEmail = consumer.Email,
@@ -191,14 +217,13 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
191217
record.RecordFields[Guid.Parse(statusKey)].Values.Add(createPaymentResult.Status);
192218
#endif
193219

194-
// TODO - update after Forms patch applied
195-
_httpContextAccessor.HttpContext.Items["FormsRedirectAfterFormSubmitUrl"] = createPaymentResult.RedirectUrl;
220+
_httpContextAccessor.HttpContext.Items[Core.Constants.ItemKeys.RedirectAfterFormSubmitUrl] = createPaymentResult.RedirectUrl;
196221

197222
return WorkflowExecutionStatus.Completed;
198223
}
199224

200225
#if NETCOREAPP
201-
context.Record.RecordFields[Guid.Parse(statusKey)].Values.Add("error");
226+
context.Record.RecordFields[Guid.Parse(statusKey)].Values.Add("error");
202227
#else
203228
record.RecordFields[Guid.Parse(statusKey)].Values.Add("error");
204229
#endif
@@ -215,8 +240,12 @@ public override List<Exception> ValidateSettings()
215240

216241
if (string.IsNullOrEmpty(Currency)) list.Add(new Exception("Currency field is required."));
217242

243+
if (string.IsNullOrEmpty(RecordStatus)) list.Add(new Exception("Record Status field is required."));
244+
245+
if (string.IsNullOrEmpty(UniqueId)) list.Add(new Exception("Payment Unique ID field is required."));
246+
218247
if (!CustomerDetailsMappings.TryParseMappings(out _))
219-
list.Add(new Exception("Customer details mappings are required."));
248+
list.Add(new Exception("Customer email mapping is required."));
220249

221250
if (!SuccessUrl.IsContentValid(nameof(SuccessUrl), out var successError))
222251
list.Add(new Exception(successError));

0 commit comments

Comments
 (0)