@@ -121,108 +121,117 @@ public PaymentProviderWorkflow(
121
121
public override WorkflowExecutionStatus Execute ( WorkflowExecutionContext context )
122
122
{
123
123
if ( ! _mappingService . TryParse ( CustomerDetailsMappings , out var mappings ) ) return WorkflowExecutionStatus . Failed ;
124
-
125
- var mappingBuilder = new MappingBuilder ( )
126
- . SetValues ( context . Record , mappings )
127
- . Build ( ) ;
128
124
129
- // step 1. Create or Retrieve Consumer
130
- var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
125
+ try
126
+ {
127
+ var mappingBuilder = new MappingBuilder ( )
128
+ . SetValues ( context . Record , mappings )
129
+ . Build ( ) ;
131
130
132
- // step 1. Create Consumer
133
- var createConsumerTask = Task . Run ( async ( ) => await _consumerService . Create ( consumer ) ) ;
131
+ // step 1. Create or Retrieve Consumer
132
+ var consumer = new ConsumerDto { Email = mappingBuilder . Email } ;
134
133
135
- var result = createConsumerTask . Result ;
136
- if ( result . Status . Contains ( "error" ) && result . Code != Constants . ErrorCode . ConsumerExists )
137
- {
138
- _logger . LogError ( $ "Failed to create consumer: { result . TechnicalMessage } .") ;
134
+ // step 1. Create Consumer
135
+ var createConsumerTask = Task . Run ( async ( ) => await _consumerService . Create ( consumer ) ) ;
139
136
140
- return WorkflowExecutionStatus . Failed ;
141
- }
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 } .") ;
142
141
143
- if ( result . Code == Constants . ErrorCode . ConsumerExists )
144
- {
145
- // step 1.1. Get Consumer
146
- var retrieveConsumerTask = Task . Run ( async ( ) => await _consumerService . Retrieve ( consumer ) ) ;
147
- consumer = retrieveConsumerTask . Result ;
148
- }
149
- else
150
- {
151
- consumer . Id = result . Id ;
152
- }
142
+ return WorkflowExecutionStatus . Failed ;
143
+ }
153
144
154
- // step 2. Create Payment
155
- var random = new Random ( ) ;
156
- var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
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
+ }
157
155
158
- var formHelper = new FormHelper ( context . Record ) ;
156
+ // step 2. Create Payment
157
+ var random = new Random ( ) ;
158
+ var transactionId = $ "uc-{ random . Next ( 1000000 , 999999999 ) } ";
159
159
160
- var formId = formHelper . GetFormId ( ) ;
161
- var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
160
+ var formHelper = new FormHelper ( context . Record ) ;
162
161
163
- var uniqueIdKey = UniqueId ;
164
- var statusKey = RecordStatus ;
162
+ var formId = formHelper . GetFormId ( ) ;
163
+ var recordUniqueId = formHelper . GetRecordUniqueId ( ) ;
165
164
166
- var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
167
- ? 0
168
- : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
165
+ var uniqueIdKey = UniqueId ;
166
+ var statusKey = RecordStatus ;
169
167
170
- var payment = new PaymentDto
171
- {
172
- TransactionId = transactionId . ToString ( ) ,
173
- Usage = _paymentProviderSettings . Usage ,
174
- NotificationUrl = $ "{ _paymentProviderSettings . UmbracoBaseUrl } umbraco/api/paymentprovider/notifypayment" +
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
168
+ var numberOfItems = string . IsNullOrEmpty ( NumberOfItems )
169
+ ? 0
170
+ : int . Parse ( formHelper . GetRecordFieldValue ( NumberOfItems ) ) ;
171
+
172
+ var payment = new PaymentDto
187
173
{
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
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" )
199
213
{
200
- TransactionTypes = _parser . AsEnumerable ( nameof ( PaymentProviderSettings . TransactionTypes ) )
201
- . Select ( p => new TransactionTypeRecordDto { TransactionType = p } )
202
- . ToList ( )
203
- }
204
- } ;
214
+ // add unique ID and status to record
215
+ formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
216
+ formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
205
217
206
- var createPaymentTask = Task . Run ( async ( ) => await _paymentService . Create ( payment ) ) ;
218
+ _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
207
219
208
- var createPaymentResult = createPaymentTask . Result ;
220
+ return WorkflowExecutionStatus . Completed ;
221
+ }
209
222
210
- if ( createPaymentResult . Status != "error" )
211
- {
212
- // add unique ID and status to record
213
- formHelper . UpdateRecordFieldValue ( uniqueIdKey , createPaymentResult . UniqueId ) ;
214
- formHelper . UpdateRecordFieldValue ( statusKey , createPaymentResult . Status ) ;
223
+ formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
215
224
216
- _httpContextAccessor . HttpContext . Items [ Core . Constants . ItemKeys . RedirectAfterFormSubmitUrl ] = createPaymentResult . RedirectUrl ;
225
+ _logger . LogError ( $ "Failed to create payment: { createPaymentResult . TechnicalMessage } ." ) ;
217
226
218
- return WorkflowExecutionStatus . Completed ;
227
+ return WorkflowExecutionStatus . Failed ;
219
228
}
229
+ catch ( Exception ex )
230
+ {
231
+ _logger . LogError ( $ "Workflow failed: { ex . Message } .") ;
220
232
221
- formHelper . UpdateRecordFieldValue ( statusKey , "error" ) ;
222
-
223
- _logger . LogError ( $ "Failed to create payment: { createPaymentResult . TechnicalMessage } .") ;
224
-
225
- return WorkflowExecutionStatus . Failed ;
233
+ return WorkflowExecutionStatus . Failed ;
234
+ }
226
235
}
227
236
228
237
public override List < Exception > ValidateSettings ( )
0 commit comments