2

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! :)

  • I think you can play around content type attribute in http header. Check whether this helps developer.salesforce.com/forums/?id=906F00000009AtFIAU – karthikselva Feb 14 '16 at 17:34
  • Who his retuning this XML response? Stripe? – Salesforcesmarty Feb 15 '16 at 0:05
  • Jess,I have the same issue. How do you redirect to a new page after this post? – user28950 Feb 19 '16 at 5:32
  • Thanks to karthikselva and Daniel Ballinger for their help! You need to use RestContext and set the content-type header and response body. Make sure your function is not returning anything (just void). Then, add something like this to the end of your function. I'm using text/html as the content type and so far, it's working well! Hope this helps. RestContext RestContext.response.addHeader('Content-Type', 'text/html'); RestContext.response.responseBody = Blob.valueOf('<b>Incorrect</b> <i>account</i> id'); – Jess Lopez May 15 '16 at 19:26
1

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.