[SalesForce] Rest Response Data Styling

I am using a Force.com site to expose a Visualforce page which calls out to the Stripe API via Javascript for a financial transaction. It's all working perfectly, but I am new to REST and can't quite wrap my head around something. The response data is sent back to my RestResource URL, but it is displaying the data as XML, with a message that the XML file does not appear to have any style information associated with it. This is great for me, but not great for the end user. Is there any way to either display the page without this format (i.e. so that the user receives a custom message that I can format via HTML/CSS or can I redirect the user to another Page Reference (i.e. another Visualforce page).

This is a bare bones example, but here is my code for the RestResource:

@RestResource(urlMapping='/createCustomer/*')
global class JessRest {

    public JessRest(){}

    @HttpPost 
    global static String createCustomer() { 

         Blob theBlob = RestContext.request.requestBody;
         String json = RestContext.request.requestBody.toString(); 
         Map<String,String> chargeProps = new Map<String,String>();
         chargeProps.put('currency', 'usd');

         Map<String,String> chargeMetaData = new Map<String,String>();
         chargeMetaData.put('sid', '12344556666666');
         chargeMetaData.put('softCredit', '9999999999999999');

         StripeCustomer customer = StripeCustomer.create(RestContext.request.params.get('stripeToken'), 'testingjl');
         StripeCharge charge = StripeCharge.create(customer.id, 5000, chargeProps, chargeMetaData);

         return '<p><b>this is</b>html</p>';
    }
}

The Visualforce page that originally makes the call looks like this (again, bare bones…):

<apex:page controller="JessRest" showHeader="false" sidebar="false">
      <form action="https://XXXXXXXXX/jlww/services/apexrest/createCustomer" method="POST">
        <script
            src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="XXXXXXXXXXXXXXXXXXX"
            data-image="/img/documentation/checkout/marketplace.png"
            data-name="XXXXXXXXXXX"
            data-description="XXXXXXXXXX"
            data-amount="800"
            data-locale="auto"
            data-panel-label="Donate"
            data-zip-code="true"
            data-billing-address="true"
            data-email="{!$CurrentPage.parameters.email}"
            data-label="Donate Now"
            data-allow-remember-me="true"
            data-currency="usd">
            </script>
      </form>
  </apex:page>

Any help or insight is greatly appreciated! 🙂

Best Answer

I believe you will need to use the RestContext.response to send data back to the form POST request.

@RestResource(urlMapping='/createCustomer/*')
global class JessRest {

    public JessRest(){}

    @HttpPost 
    global static void createCustomer() { 

         Blob theBlob = RestContext.request.requestBody;
         String json = RestContext.request.requestBody.toString(); 
         Map<String,String> chargeProps = new Map<String,String>();
         chargeProps.put('currency', 'usd');

         Map<String,String> chargeMetaData = new Map<String,String>();
         chargeMetaData.put('sid', '12344556666666');
         chargeMetaData.put('softCredit', '9999999999999999');

         StripeCustomer customer = StripeCustomer.create(RestContext.request.params.get('stripeToken'), 'testingjl');
         StripeCharge charge = StripeCharge.create(customer.id, 5000, chargeProps, chargeMetaData);

         RestResponse res = RestContext.response;
         if (res == null) {
             res = new RestResponse();
             RestContext.response = res;
         }
         // Return something
         res.responseBody = Blob.valueOf('<p><b>this is</b>html</p>');

         // Set the status
         res.statusCode = 200;

    }
}

The Stripe JavaScript in https://checkout.stripe.com/checkout.js is likely submitting the form and then handling the response for the server. Maybe if there isn't a response it dumps the XML?