@@ -4,6 +4,9 @@ public class Script : ScriptBase
44 {
55 switch ( this . Context . OperationId )
66 {
7+ case "CreatePledgePayment" :
8+ return await this . HandleCreatePledgePaymentOperation ( ) . ConfigureAwait ( false ) ;
9+
710 case "ListGiftCustomFields" :
811 return await this . HandleListCustomFieldOperation ( ) . ConfigureAwait ( false ) ;
912 }
@@ -13,6 +16,35 @@ public class Script : ScriptBase
1316 return response ;
1417 }
1518
19+ private async Task < HttpResponseMessage > HandleCreatePledgePaymentOperation ( )
20+ {
21+ // transform the installment_payments property to the applied_payments shape
22+
23+ // first, get the object from the request
24+ var requestContent = await this . Context . Request . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
25+ var requestJson = JObject . Parse ( requestContent ) ;
26+
27+ var installmentPayments = ( JArray ) requestJson [ "installment_payments" ] ;
28+
29+ requestJson . Add ( "apply_payments" , new JArray ( installmentPayments . GroupBy ( item => item [ "pledge_id" ] ) . Select ( group => new JObject ( ) {
30+ [ "parent_id" ] = group . Key ,
31+ [ "installments" ] = new JArray ( group . Select ( item => new JObject ( ) {
32+ [ "installment_id" ] = item [ "installment_id" ] ,
33+ [ "amount_applied" ] = item [ "amount_applied" ]
34+ } ) )
35+ } ) ) ) ;
36+
37+ // remove properties not used by the backend (should be ignored, but let's not risk it)
38+ requestJson . Remove ( "installment_payments" ) ;
39+
40+ var newBody = requestJson ;
41+
42+ // send the request to the backend and return the response
43+ this . Context . Request . Content = CreateJsonContent ( newBody . ToString ( ) ) ;
44+ var response = await this . Context . SendAsync ( this . Context . Request , this . CancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
45+ return response ;
46+ }
47+
1648 private async Task < HttpResponseMessage > HandleListCustomFieldOperation ( )
1749 {
1850 // make the API request to the SKY API backend
0 commit comments