Smart Payment Buttons custom integration problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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¤cy=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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have pretty much exactly the same issue using the python sdk. It works fine for me with the JS smart buttons but when following the documentation on https://developer.paypal.com/docs/checkout/integrate/#5-capture-the-transaction to replace it with server-side calls I get:
SyntaxError: Unexpected token < in JSON at position 0Error: Unexpected token < in JSON at position 0 at jr.error (https://www.paypal.com/sdk/js?currency=GBP&client-id=XXX) at Object.<anonymous> (https://www.paypal.com/sdk/js?currency=GBP&client-id=XXX
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- Need help to integrate PayPal Checkout Advanced (PPCP) with WooCommerce in PayPal Upgrade Community
- How to integrate paypal smart buttons on cart page? in REST APIs
- Error: "Unable to change shipping method. Please try again." in PayPal popup in REST APIs
- Simple Paypal button integration results in 404 error in Sandbox Environment
- Integrating buttons into my website 2024 in PayPal Payments Standard