cancel
Showing results for 
Search instead for 
Did you mean: 

Smart Payment Buttons custom integration problem

Contributor

Smart Payment Buttons custom integration problem

Hi all,

I'm facing an integration with smart payment buttons with server-side setup because my data is filled dynamically and also I want to store the payment in my own database.

 

If I try this code it works successfully:

<script>  paypal.Buttons({
    createOrder: function(data, actions) {
      // Set up the transaction
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
     style: {
       color: 'blue',
       shape: 'rect',
       label: 'paypal',
       height: 40
    }
  }).render('#ppBtn');</script>

 

But when I try to define a server method call to achieve what I need I got an error:

client-side

 

<script>
			paypal.Buttons({
				createOrder: function() {
					return fetch('${pageContext.request.contextPath}/createOrder', {
						method: 'post',
					    headers: {'content-type': 'application/json'}
					}).then(function(res) {
						return res.json();
					}).then(function(data) {
					    return data.orderID; // Use the same key name for order ID on the client and server
					});
			    },
				style: {
	                color:  'blue',
	                shape:  'rect',
	                label:  'paypal',
	                height: 40
	            }
			}).render('#ppBtn');
		</script>

 

 server-side

 

public HttpResponse<Order> createOrder(boolean debug) throws IOException {
	    OrdersCreateRequest request = new OrdersCreateRequest();
	    request.prefer("return=representation");
	    request.requestBody(buildRequestBody());
	    //3. Call PayPal to set up a transaction
	    HttpResponse<Order> response = client().execute(request);
	    if (debug) {
	      if (response.statusCode() == 201) {
	        System.out.println("Status Code: " + response.statusCode());
	        System.out.println("Status: " + response.result().status());
	        System.out.println("Order ID: " + response.result().id());
	        System.out.println("Intent: " + response.result().intent());
	        System.out.println("Links: ");
	        for (LinkDescription link : response.result().links()) {
	          System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
	        }
	        System.out.println("Total Amount: " + response.result().purchaseUnits().get(0).amount().currencyCode()
	            + " " + response.result().purchaseUnits().get(0).amount().value());
	      }
	    }
	    return response;
	  }

 

 

And the error message is the following:

unhandled_error
Object { err: "JSON.parse: unexpected character at line 26 column 1 of the JSON data\ncreateOrder@http://localhost:8081/ROOT/:437:8\nhr/</</</<@https://www.paypal.com/sdk/js?client-id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&currency=EUR&disable-funding=...", timestamp: "1571062868630", referer: "www.sandbox.paypal.com", uid: "19a7b17404_mti6mjg6mdu", env: "sandbox" }

 

It does not make any sense for me and it never gets method in server side.

 

Could you kindly help me with something that can be useful to solve this problem?

 

Thank you to all.