1414
1515using Microsoft . AspNetCore . Http ;
1616using Microsoft . Extensions . Options ;
17+ using Microsoft . Extensions . Logging ;
1718
1819namespace Umbraco . Forms . Integrations . Commerce . Emerchantpay
1920{
@@ -33,6 +34,8 @@ public class PaymentProviderWorkflow : WorkflowType
3334
3435 private readonly ISettingsParser _parser ;
3536
37+ private readonly ILogger < PaymentProviderWorkflow > _logger ;
38+
3639 #region WorkflowSettings
3740
3841 [ Core . Attributes . Setting ( "Amount" ,
@@ -84,9 +87,13 @@ public class PaymentProviderWorkflow : WorkflowType
8487
8588 #endregion
8689
87- public PaymentProviderWorkflow ( IOptions < PaymentProviderSettings > paymentProviderSettings , IHttpContextAccessor httpContextAccessor ,
90+ public PaymentProviderWorkflow (
91+ IOptions < PaymentProviderSettings > paymentProviderSettings ,
92+ IHttpContextAccessor httpContextAccessor ,
8893 ConsumerService consumerService , PaymentService paymentService , UrlHelper urlHelper ,
89- IMappingService < Mapping > mappingService , ISettingsParser parser )
94+ IMappingService < Mapping > mappingService ,
95+ ISettingsParser parser ,
96+ ILogger < PaymentProviderWorkflow > logger )
9097
9198 {
9299 Id = new Guid ( Constants . WorkflowId ) ;
@@ -107,104 +114,124 @@ public PaymentProviderWorkflow(IOptions<PaymentProviderSettings> paymentProvider
107114 _parser = parser ;
108115
109116 _paymentProviderSettings = paymentProviderSettings . Value ;
117+
118+ _logger = logger ;
110119 }
111120
112121 public override WorkflowExecutionStatus Execute ( WorkflowExecutionContext context )
113122 {
114123 if ( ! _mappingService . TryParse ( CustomerDetailsMappings , out var mappings ) ) return WorkflowExecutionStatus . Failed ;
115124
116- var mappingBuilder = new MappingBuilder ( )
117- . SetValues ( context . Record , mappings )
118- . Build ( ) ;
125+ try
126+ {
127+ var mappingBuilder = new MappingBuilder ( )
128+ . SetValues ( context . Record , mappings )
129+ . Build ( ) ;
119130
120- // step 1. Create or Retrieve Consumer
121- var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
131+ // step 1. Create or Retrieve Consumer
132+ var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
122133
123- // step 1. Create Consumer
124- var createConsumerTask = Task . Run ( async ( ) => await _consumerService . Create ( consumer ) ) ;
134+ // step 1. Create Consumer
135+ var createConsumerTask = Task . Run ( async ( ) => await _consumerService . Create ( consumer ) ) ;
125136
126- var result = createConsumerTask . Result ;
127- if ( result . Code == Constants . ErrorCode . ConsumerExists )
128- {
129- // step 1.1. Get Consumer
130- var retrieveConsumerTask = Task . Run ( async ( ) => await _consumerService . Retrieve ( consumer ) ) ;
131- consumer = retrieveConsumerTask . Result ;
132- }
133- else
134- {
135- consumer . Id = result . Id ;
136- }
137+ var result = createConsumerTask . Result ;
138+ if ( result . Status . Contains ( "error" ) && result . Code != Constants . ErrorCode . ConsumerExists )
139+ {
140+ _logger . LogError ( $ "Failed to create consumer: { result . TechnicalMessage } .") ;
141+
142+ return WorkflowExecutionStatus . Failed ;
143+ }
144+
145+ if ( result . Code == Constants . ErrorCode . ConsumerExists )
146+ {
147+ // step 1.1. Get Consumer
148+ var retrieveConsumerTask = Task . Run ( async ( ) => await _consumerService . Retrieve ( consumer ) ) ;
149+ consumer = retrieveConsumerTask . Result ;
150+ }
151+ else
152+ {
153+ consumer . Id = result . Id ;
154+ }
137155
138- // step 2. Create Payment
139- var random = new Random ( ) ;
140- var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
156+ // step 2. Create Payment
157+ var random = new Random ( ) ;
158+ var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
141159
142- var formHelper = new FormHelper ( context . Record ) ;
160+ var formHelper = new FormHelper ( context . Record ) ;
143161
144- var formId = formHelper . GetFormId ( ) ;
145- var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
162+ var formId = formHelper . GetFormId ( ) ;
163+ var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
146164
147- var uniqueIdKey = UniqueId ;
148- var statusKey = RecordStatus ;
165+ var uniqueIdKey = UniqueId ;
166+ var statusKey = RecordStatus ;
149167
150- var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
151- ? 0
152- : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
168+ var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
169+ ? 0
170+ : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
153171
154- var payment = new PaymentDto
155- {
156- TransactionId = transactionId . ToString ( ) ,
157- Usage = _paymentProviderSettings . Usage ,
158- NotificationUrl = $ "{ _paymentProviderSettings . UmbracoBaseUrl } umbraco/api/paymentprovider/notifypayment" +
159- $ "?formId={ formId } &recordUniqueId={ recordUniqueId } &statusFieldId={ statusKey } &approve={ ( bool . TryParse ( Approve , out bool approve ) ? approve : false ) } ",
160- ReturnSuccessUrl = _urlHelper . GetPageUrl ( int . Parse ( SuccessUrl ) ) ,
161- ReturnFailureUrl = _urlHelper . GetPageUrl ( int . Parse ( FailureUrl ) ) ,
162- ReturnCancelUrl = _urlHelper . GetPageUrl ( int . Parse ( CancelUrl ) ) ,
163- Amount = numberOfItems != 0
164- ? numberOfItems * int . Parse ( Amount )
165- : int . Parse ( Amount ) ,
166- Currency = Currency ,
167- ConsumerId = consumer . Id ,
168- CustomerEmail = consumer . Email ,
169- CustomerPhone = mappingBuilder . Phone ,
170- BillingAddress = new AddressDto
172+ var payment = new PaymentDto
171173 {
172- FirstName = mappingBuilder . FirstName ,
173- LastName = mappingBuilder . LastName ,
174- Address1 = mappingBuilder . Address ,
175- Address2 = string . Empty ,
176- ZipCode = mappingBuilder . ZipCode ,
177- City = mappingBuilder . City ,
178- State = mappingBuilder . State ,
179- Country = mappingBuilder . Country
180- } ,
181- BusinessAttribute = new BusinessAttribute { NameOfTheSupplier = _paymentProviderSettings . Supplier } ,
182- TransactionTypes = new TransactionTypeDto
174+ TransactionId = transactionId . ToString ( ) ,
175+ Usage = _paymentProviderSettings . Usage ,
176+ NotificationUrl = $ "{ _paymentProviderSettings . UmbracoBaseUrl } umbraco/api/paymentprovider/notifypayment" +
177+ $ "?formId={ formId } &recordUniqueId={ recordUniqueId } &statusFieldId={ statusKey } &approve={ ( bool . TryParse ( Approve , out bool approve ) ? approve : false ) } ",
178+ ReturnSuccessUrl = _urlHelper . GetPageUrl ( int . Parse ( SuccessUrl ) ) ,
179+ ReturnFailureUrl = _urlHelper . GetPageUrl ( int . Parse ( FailureUrl ) ) ,
180+ ReturnCancelUrl = _urlHelper . GetPageUrl ( int . Parse ( CancelUrl ) ) ,
181+ Amount = numberOfItems != 0
182+ ? numberOfItems * int . Parse ( Amount )
183+ : int . Parse ( Amount ) ,
184+ Currency = Currency ,
185+ ConsumerId = consumer . Id ,
186+ CustomerEmail = consumer . Email ,
187+ CustomerPhone = mappingBuilder . Phone ,
188+ BillingAddress = new AddressDto
189+ {
190+ FirstName = mappingBuilder . FirstName ,
191+ LastName = mappingBuilder . LastName ,
192+ Address1 = mappingBuilder . Address ,
193+ Address2 = string . Empty ,
194+ ZipCode = mappingBuilder . ZipCode ,
195+ City = mappingBuilder . City ,
196+ State = mappingBuilder . State ,
197+ Country = mappingBuilder . Country
198+ } ,
199+ BusinessAttribute = new BusinessAttribute { NameOfTheSupplier = _paymentProviderSettings . Supplier } ,
200+ TransactionTypes = new TransactionTypeDto
201+ {
202+ TransactionTypes = _parser . AsEnumerable ( nameof ( PaymentProviderSettings . TransactionTypes ) )
203+ . Select ( p => new TransactionTypeRecordDto { TransactionType = p } )
204+ . ToList ( )
205+ }
206+ } ;
207+
208+ var createPaymentTask = Task . Run ( async ( ) => await _paymentService . Create ( payment ) ) ;
209+
210+ var createPaymentResult = createPaymentTask . Result ;
211+
212+ if ( createPaymentResult . Status != "error" )
183213 {
184- TransactionTypes = _parser . AsEnumerable ( nameof ( PaymentProviderSettings . TransactionTypes ) )
185- . Select ( p => new TransactionTypeRecordDto { TransactionType = p } )
186- . ToList ( )
187- }
188- } ;
214+ // add unique ID and status to record
215+ formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
216+ formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
189217
190- var createPaymentTask = Task . Run ( async ( ) => await _paymentService . Create ( payment ) ) ;
218+ _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
191219
192- var createPaymentResult = createPaymentTask . Result ;
220+ return WorkflowExecutionStatus . Completed ;
221+ }
193222
194- if ( createPaymentResult . Status != "error" )
195- {
196- // add unique ID and status to record
197- formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
198- formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
223+ formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
199224
200- _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
225+ _logger . LogError ( $ "Failed to create payment: { createPaymentResult . TechnicalMessage } ." ) ;
201226
202- return WorkflowExecutionStatus . Completed ;
227+ return WorkflowExecutionStatus . Failed ;
203228 }
229+ catch ( Exception ex )
230+ {
231+ _logger . LogError ( $ "Workflow failed: { ex . Message } .") ;
204232
205- formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
206-
207- return WorkflowExecutionStatus . Failed ;
233+ return WorkflowExecutionStatus . Failed ;
234+ }
208235 }
209236
210237 public override List < Exception > ValidateSettings ( )
0 commit comments