@@ -45,6 +45,133 @@ function handleUserError(userErrors, res) {
45
45
return false ;
46
46
}
47
47
48
+ // Endpoint for fetching a payment customization
49
+ app . get ( "/api/paymentCustomization/:id" , async ( req , res ) => {
50
+ const id = `gid://shopify/PaymentCustomization/${ req . params . id } ` ;
51
+ const graphqlClient = new shopify . api . clients . Graphql ( {
52
+ session : res . locals . shopify . session ,
53
+ } ) ;
54
+
55
+ try {
56
+ const response = await graphqlClient . query ( {
57
+ data : {
58
+ query : `query PaymentCustomization($id: ID!) {
59
+ paymentCustomization(id: $id) {
60
+ id
61
+ metafield(namespace: "payment-customization", key: "function-configuration") {
62
+ value
63
+ }
64
+ }
65
+ }` ,
66
+ variables : {
67
+ id,
68
+ } ,
69
+ } ,
70
+ } ) ;
71
+
72
+ if ( ! response . body . data || ! response . body . data . paymentCustomization ) {
73
+ res . status ( 404 ) . send ( { error : "Payment customization not found" } ) ;
74
+ }
75
+
76
+ const metafieldValue = response . body . data . paymentCustomization . metafield
77
+ ? JSON . parse ( response . body . data . paymentCustomization . metafield . value )
78
+ : { } ;
79
+ const { paymentMethodName, cartTotal } = metafieldValue ;
80
+
81
+ const paymentCustomization = {
82
+ id,
83
+ paymentMethod : paymentMethodName ,
84
+ cartTotal,
85
+ } ;
86
+
87
+ res . status ( 200 ) . send ( paymentCustomization ) ;
88
+ } catch ( error ) {
89
+ console . error ( `Failed to fetch payment customization ${ id } ` , error ) ;
90
+ res . status ( 500 ) . send ( ) ;
91
+ }
92
+ } ) ;
93
+
94
+ app . put ( "/api/paymentCustomization/update" , async ( req , res ) => {
95
+ const payload = req . body ;
96
+ const graphqlClient = new shopify . api . clients . Graphql ( {
97
+ session : res . locals . shopify . session ,
98
+ } ) ;
99
+
100
+ try {
101
+ // Create the payment customization for the provided function ID
102
+ const updateResponse = await graphqlClient . query ( {
103
+ data : {
104
+ query : `mutation PaymentCustomizationUpdate($id: ID!, $input: PaymentCustomizationInput!) {
105
+ paymentCustomizationUpdate(id: $id, paymentCustomization: $input) {
106
+ paymentCustomization {
107
+ id
108
+ }
109
+ userErrors {
110
+ message
111
+ }
112
+ }
113
+ }` ,
114
+ variables : {
115
+ input : {
116
+ functionId : payload . functionId ,
117
+ title : `Hide ${ payload . paymentMethod } if cart total is larger than ${ payload . cartTotal } ` ,
118
+ enabled : true ,
119
+ } ,
120
+ id : payload . id ,
121
+ } ,
122
+ } ,
123
+ } ) ;
124
+ let updateResult = updateResponse . body . data . paymentCustomizationUpdate ;
125
+ if ( handleUserError ( updateResult . userErrors , res ) ) {
126
+ return ;
127
+ }
128
+
129
+ // Populate the function configuration metafield for the payment customization
130
+ const customizationId = updateResult . paymentCustomization . id ;
131
+ const metafieldResponse = await graphqlClient . query ( {
132
+ data : {
133
+ query : `mutation MetafieldsSet($customizationId: ID!, $configurationValue: String!) {
134
+ metafieldsSet(metafields: [
135
+ {
136
+ ownerId: $customizationId
137
+ namespace: "payment-customization"
138
+ key: "function-configuration"
139
+ value: $configurationValue
140
+ type: "json"
141
+ }
142
+ ]) {
143
+ metafields {
144
+ id
145
+ }
146
+ userErrors {
147
+ message
148
+ }
149
+ }
150
+ }` ,
151
+ variables : {
152
+ customizationId,
153
+ configurationValue : JSON . stringify ( {
154
+ paymentMethodName : payload . paymentMethod ,
155
+ cartTotal : payload . cartTotal ,
156
+ } ) ,
157
+ } ,
158
+ } ,
159
+ } ) ;
160
+ let metafieldResult = metafieldResponse . body . data . metafieldsSet ;
161
+ if ( handleUserError ( metafieldResult , res ) ) {
162
+ return ;
163
+ }
164
+ } catch ( error ) {
165
+ // Handle errors thrown by the graphql client
166
+ if ( ! ( error instanceof GraphqlQueryError ) ) {
167
+ throw error ;
168
+ }
169
+ return res . status ( 500 ) . send ( { error : error . response } ) ;
170
+ }
171
+
172
+ return res . status ( 200 ) . send ( ) ;
173
+ } ) ;
174
+
48
175
// Endpoint for the payment customization UI to invoke
49
176
app . post ( "/api/paymentCustomization/create" , async ( req , res ) => {
50
177
const payload = req . body ;
0 commit comments