@@ -118,123 +118,119 @@ public PaymentProviderWorkflow(
118118 _logger = logger ;
119119 }
120120
121- public override WorkflowExecutionStatus Execute ( WorkflowExecutionContext context )
122- {
123- if ( ! _mappingService . TryParse ( CustomerDetailsMappings , out var mappings ) ) return WorkflowExecutionStatus . Failed ;
124-
125- try
126- {
127- var mappingBuilder = new MappingBuilder ( )
128- . SetValues ( context . Record , mappings )
129- . Build ( ) ;
130-
131- // step 1. Create or Retrieve Consumer
132- var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
133-
134- // step 1. Create Consumer
135- var createConsumerTask = Task . Run ( async ( ) => await _consumerService . Create ( consumer ) ) ;
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- }
155-
156- // step 2. Create Payment
157- var random = new Random ( ) ;
158- var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
159-
160- var formHelper = new FormHelper ( context . Record ) ;
161-
162- var formId = formHelper . GetFormId ( ) ;
163- var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
164-
165- var uniqueIdKey = UniqueId ;
166- var statusKey = RecordStatus ;
167-
168- var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
169- ? 0
170- : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
171-
172- var payment = new PaymentDto
173- {
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" )
213- {
214- // add unique ID and status to record
215- formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
216- formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
217-
218- _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
219-
220- return WorkflowExecutionStatus . Completed ;
221- }
222-
223- formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
224-
225- _logger . LogError ( $ "Failed to create payment: { createPaymentResult . TechnicalMessage } .") ;
226-
227- return WorkflowExecutionStatus . Failed ;
228- }
229- catch ( Exception ex )
230- {
231- _logger . LogError ( $ "Workflow failed: { ex . Message } .") ;
232-
233- return WorkflowExecutionStatus . Failed ;
234- }
235- }
236-
237- public override List < Exception > ValidateSettings ( )
121+ public override async Task < WorkflowExecutionStatus > ExecuteAsync ( WorkflowExecutionContext context )
122+ {
123+ if ( ! _mappingService . TryParse ( CustomerDetailsMappings , out var mappings ) ) return WorkflowExecutionStatus . Failed ;
124+
125+ try
126+ {
127+ var mappingBuilder = new MappingBuilder ( )
128+ . SetValues ( context . Record , mappings )
129+ . Build ( ) ;
130+
131+ // step 1. Create or Retrieve Consumer
132+ var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
133+
134+ // step 1. Create Consumer
135+ var createConsumerResult = await _consumerService . Create ( consumer ) ;
136+
137+ if ( createConsumerResult . Status . Contains ( "error" ) && createConsumerResult . Code != Constants . ErrorCode . ConsumerExists )
138+ {
139+ _logger . LogError ( $ "Failed to create consumer: { createConsumerResult . TechnicalMessage } .") ;
140+
141+ return WorkflowExecutionStatus . Failed ;
142+ }
143+
144+ if ( createConsumerResult . Code == Constants . ErrorCode . ConsumerExists )
145+ {
146+ // step 1.1. Get Consumer
147+ consumer = await _consumerService . Retrieve ( consumer ) ;
148+ }
149+ else
150+ {
151+ consumer . Id = createConsumerResult . Id ;
152+ }
153+
154+ // step 2. Create Payment
155+ var random = new Random ( ) ;
156+ var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
157+
158+ var formHelper = new FormHelper ( context . Record ) ;
159+
160+ var formId = formHelper . GetFormId ( ) ;
161+ var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
162+
163+ var uniqueIdKey = UniqueId ;
164+ var statusKey = RecordStatus ;
165+
166+ var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
167+ ? 0
168+ : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
169+
170+ var payment = new PaymentDto
171+ {
172+ TransactionId = transactionId . ToString ( ) ,
173+ Usage = _paymentProviderSettings . Usage ,
174+ NotificationUrl = $ "{ _paymentProviderSettings . UmbracoBaseUrl } umbraco/api/paymentprovider/notifypaymentasync" +
175+ $ "?formId={ formId } &recordUniqueId={ recordUniqueId } &statusFieldId={ statusKey } &approve={ ( bool . TryParse ( Approve , out bool approve ) ? approve : false ) } ",
176+ ReturnSuccessUrl = _urlHelper . GetPageUrl ( int . Parse ( SuccessUrl ) ) ,
177+ ReturnFailureUrl = _urlHelper . GetPageUrl ( int . Parse ( FailureUrl ) ) ,
178+ ReturnCancelUrl = _urlHelper . GetPageUrl ( int . Parse ( CancelUrl ) ) ,
179+ Amount = numberOfItems != 0
180+ ? numberOfItems * int . Parse ( Amount )
181+ : int . Parse ( Amount ) ,
182+ Currency = Currency ,
183+ ConsumerId = consumer . Id ,
184+ CustomerEmail = consumer . Email ,
185+ CustomerPhone = mappingBuilder . Phone ,
186+ BillingAddress = new AddressDto
187+ {
188+ FirstName = mappingBuilder . FirstName ,
189+ LastName = mappingBuilder . LastName ,
190+ Address1 = mappingBuilder . Address ,
191+ Address2 = string . Empty ,
192+ ZipCode = mappingBuilder . ZipCode ,
193+ City = mappingBuilder . City ,
194+ State = mappingBuilder . State ,
195+ Country = mappingBuilder . Country
196+ } ,
197+ BusinessAttribute = new BusinessAttribute { NameOfTheSupplier = _paymentProviderSettings . Supplier } ,
198+ TransactionTypes = new TransactionTypeDto
199+ {
200+ TransactionTypes = _parser . AsEnumerable ( nameof ( PaymentProviderSettings . TransactionTypes ) )
201+ . Select ( p => new TransactionTypeRecordDto { TransactionType = p } )
202+ . ToList ( )
203+ }
204+ } ;
205+
206+ var createPaymentResult = await _paymentService . Create ( payment ) ;
207+
208+ if ( createPaymentResult . Status != "error" )
209+ {
210+ // add unique ID and status to record
211+ formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
212+ formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
213+
214+ _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
215+
216+ return WorkflowExecutionStatus . Completed ;
217+ }
218+
219+ formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
220+
221+ _logger . LogError ( $ "Failed to create payment: { createPaymentResult . TechnicalMessage } .") ;
222+
223+ return WorkflowExecutionStatus . Failed ;
224+ }
225+ catch ( Exception ex )
226+ {
227+ _logger . LogError ( $ "Workflow failed: { ex . Message } .") ;
228+
229+ return WorkflowExecutionStatus . Failed ;
230+ }
231+ }
232+
233+ public override List < Exception > ValidateSettings ( )
238234 {
239235 var list = new List < Exception > ( ) ;
240236
@@ -261,5 +257,5 @@ public override List<Exception> ValidateSettings()
261257
262258 return list ;
263259 }
264- }
260+ }
265261}
0 commit comments