I am having the issue sending the JSON with first name and last name whenever name has single quote "'". i.e O'Mally or O'Keef.
Single quote breaks the JSON. How do I pass fistname and lastname variables with single quote in it?
The only way I could get it work by replacing single quote with space using firstname.replace(''', ' '). I have tried other solutions but none of them worked.
Here's code I have.
String jsonString =
'{' +
'"getHostedPaymentPageRequest": {' +
'"merchantAuthentication": {' +
'"name": "{!$Credential.authorize_external_' + communityAbbreviation + '.api_login}"'+',' +
'"transactionKey": "{!$Credential.authorize_external_' + communityAbbreviation + '.transaction_key}"'+',' +
'},' +
'"refId":"'+recordId+'",'+
'"transactionRequest": {' +
'"transactionType": "authCaptureTransaction",' +
'"amount":'+recordDetail.Gift_Amount__c+',' +
'"order": {'+
'"invoiceNumber":"'+recordDetail.Donation_Invoice_Number__c +'"'+
'},'+
'"customer": {' +
'"email": "'+recordDetail.Email__c+'"'+
'},' +
'"billTo": {' +
'"firstName":"'+recordDetail.First_Name__c+'",' +
'"lastName":"'+recordDetail.Last_Name__c+'",' +
'"company": "",' +
'"address":"'+address+'",'+
'"city":"'+recordDetail.City__c+'",' +
'"state":"'+recordDetail.State_Province__c+'",' +
'"zip":"'+recordDetail.ZIP_Postal_Code__c+'",' +
'"country":"'+recordDetail.Country__c+'",' +
'"phoneNumber":"'+recordDetail.Phone__c+'"' +
'}' +
'},' +
'"hostedPaymentSettings": {' +
'"setting": [' +
'{' +
'"settingName": "hostedPaymentReturnOptions",' +
'"settingValue": "{\\"showReceipt\\": true, \\"url\\": \\"'+successPage+'\\", \\"urlText\\": \\"Continue\\", \\"cancelUrl\\": \\"'+errorPage+'\\", \\"cancelUrlText\\": \\"Cancel\\"}"' +
'},' +
'{' +
'"settingName": "hostedPaymentButtonOptions",' +
'"settingValue": "{\\"text\\": \\"Pay\\"}"' +
'},' +
'{' +
'"settingName": "hostedPaymentStyleOptions",' +
'"settingValue": "{\\"bgColor\\": \\"blue\\"}"' +
'},' +
'{' +
'"settingName": "hostedPaymentPaymentOptions",' +
'"settingValue": "{\\"cardCodeRequired\\": false, \\"showCreditCard\\": true, \\"showBankAccount\\": false}"' +
'},' +
'{' +
'"settingName": "hostedPaymentSecurityOptions",' +
'"settingValue": "{\\"captcha\\": true}"' +
'},' +
'{' +
'"settingName": "hostedPaymentShippingAddressOptions",' +
'"settingValue": "{\\"show\\": false, \\"required\\": false}"' +
'},' +
'{' +
'"settingName": "hostedPaymentBillingAddressOptions",' +
'"settingValue": "{\\"show\\": true, \\"required\\": false}"' +
'},' +
'{' +
'"settingName": "hostedPaymentCustomerOptions",' +
'"settingValue": "{\\"showEmail\\": false, \\"requiredEmail\\": false, \\"addPaymentProfile\\": true}"' +
'},' +
'{' +
'"settingName": "hostedPaymentOrderOptions",' +
'"settingValue": "{\\"show\\": false, \\"merchantName\\": \\"'+donorName+'\\"}"' +
'},' +
'{' +
'"settingName": "hostedPaymentIFrameCommunicatorUrl",' +
'"settingValue": "{\\"url\\": \\"https://mysite.com/special\\"}"' +
'}' +
']' +
'}' +
'}' +
'}';
req.setBody(jsonString);
Http http = new Http();
HttpResponse res = http.send(req);
Error:
Response: {"refId":"a1kRK0000XXXXXX","messages":{"resultCode":"Error","message":[{"code":"E00013","text":"Invalid Setting Value. hostedPaymentOrderOptionsmerchantName: has invalid characters."}]}}