How do I post PayPal array response details to database using Laravel - php

How do I post this Paypal response details to database
I have succefully made payment using smart button paypal.
I am able to capture the response details using console log.
Now How do I post the response array to database using laravel.
Onapproval
onApprove: function(data, actions) {
let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
if(details.status == 'COMPLETED'){
return fetch('/pages/save', {
method: 'post',
headers: {
'content-type': 'application/json',
"Accept": "application/json, text-plain, */*",
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-TOKEN": token
},
body: JSON.stringify({
orderId : data.orderID,
id : details.id,
status: details.status,
payerEmail: details.payer.email_address,
})
})
.then(status)
.then(function(response){
// redirect to the completed page if paid
console.log(details)
// window.location.href = '/pages/sucess';
})
.catch(function(error) {
// redirect to failed page if internal error occurs
window.location.href = '/pages/fail?reason=internalFailure';
});
}else{
window.location.href = '/pages/fail?reason=failedToCapture';
}
});
},
Laravel route
Route::get('pages/sucess', [App\Http\Controllers\OrderController::class, 'sucess'])->name('pages.sucess');
Output
{
"id": "9S369747UW261581G",
"intent": "CAPTURE",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "0.50"
},
"shipping": {
"name": {
"full_name": "John Doe"
},
},
"payments": {
"captures": [
{
"id": "5DS42883V93959154",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "0.50"
},
}
]
}
}
],
}
How can one capture PayPal response details and dump the same detail to the Laravel controller so that it can be saved to the database?

Do not use actions.order.create() / .capture() on the client side and then post information to a database.
Instead, change to a proper server-side integration. Make two routes on your server, one for 'Create Order' and one for 'Capture Order', documented here. These routes should return only JSON data (no HTML or text). When a capture response is successful, store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) before sending your return JSON.
Pair those two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

Related

Issue while trying to implement Paypal Create Order API

About the issue
This is about create order api in Paypal. Documentation link is here I am trying to pass below payload, so that the request could have my return and cancel url and everything works perfectly.
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"application_context" => [
"return_url" => "my return url",
"cancel_url" => "my cancel url"
]
Just the return and cancel url has gone deprerated in application_context.
To overcome this problem, I removed application_context from payload and added payment_source like below which has return and cancel url
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"payment_source": {
"paypal": {
"experience_context": {
"return_url": "return Url",
"cancel_url": "cancel Url"
}
}
}
Now, it gives an error message - PAYPAL_REQUEST_ID_REQUIRED
I need to pass return and cancel url and at this stage I only need to create the request to let user go to checkout page. that's it. I really don't have any payment info yet.
To resolve the error message "PAYPAL_REQUEST_ID_REQUIRED", you need to including a unique identifier in the "request_id" field in the payload.
something like:
[
"intent":"CAPTURE",
"purchase_units":[
{
"amount":{
"currency_code":"USD",
"value":"100.00"
}
}
],
"payment_source":{
"paypal":{
"experience_context":{
"return_url":"return Url",
"cancel_url":"cancel Url"
}
}
},
"request_id":"UNIQUE_REQUEST_ID"
]
You may also verify that the return and cancel URLs provided in the payload are valid and accessible.
Inside your payment_source add "request_id": "YOUR_UNIQUE_REQUEST_ID"
Your system can generate this request Id yourself and must be unique to prevent getting error message DUPLICATED_REQUEST_ID
See here in documentation https://developer.paypal.com/api/rest/requests
PayPal-Request-Id
"Optional. Contains a unique user-generated ID that the server stores for a period of time. Use this header to enforce idempotency on REST API POST calls. ...."
You need request_id could you try including reference_id.
{
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"payment_source": {
"paypal": {
"experience_context": {
"return_url": "https://example.com/returnUrl",
"cancel_url": "https://example.com/cancelUrl"
}
}
}
}'
https://developer.paypal.com/api/rest/reference/orders/v2/errors/
PAYPAL_REQUEST_ID_REQUIRED: A PayPal-Request-Id is required if you are trying to process payment for an Order. Please specify a PayPal-Request-Id or Create the Order without a payment_source specified.
Add this param in your headers
PayPal-Request-Id
string [ 1 .. 36 ] characters
The server stores keys for 6 hours. The API callers can request the times to up to 72 hours by speaking to their Account Manager.

PayPal Smart buttons patch order via PHP API

I'm implementing Smart Buttons with Express checkout, so customers can select the delivery address on Paypal's popup. As agreed with Paypal support, I'm doing the communication with Paypal servers via PHP instead of Javascript. So in the onShippingChange event, I'm calling my server to calculate the delivery price and patch the order so Paypal updates the delivery price. The success path works fine, I need your help on error case(s).
How the patch request shall look like to instrument Paypal to display the warning that my webshop is not delivering to the selected location? (When implementing on Javascript, this was the return actions.reject in the onShippingChange.)
Thanks!
This is the patch request of a successful patch:
{
"path": "/v2/checkout/orders/xxxxxxxxxxxxxxxxxxxxx?",
"body": [
{
"op": "replace",
"path": "/intent",
"value": "CAPTURE"
},
{
"op": "replace",
"path": "/purchase_units/#reference_id=='default'/amount",
"value": {
"currency_code": "GBP",
"value": 265.95,
"breakdown": {
"item_total": {
"currency_code": "GBP",
"value": 236
},
"shipping": {
"currency_code": "GBP",
"value": 29.95
}
}
}
}
],
"verb": "PATCH",
"headers": {
"Content-Type": "application/json"
}
}
in the onShippingChange event, I'm calling my server to calculate the delivery price and patch the order
Great. But if the address is unsupported, your server must return the failed status in its response to that call. Based on that response, onShippingChange must return actions.reject() to the calling PayPal JS.

How to get an email address and other info like name and phone together after getting access token LinkedIn signup API with PHP

I am able to gent email using https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~)) and able to get name and profile pic using https://api.linkedin.com/v2/me.My question is that how can i get it together.Is there any single API to call and get all profile info together?
The documentation says something about lite and basic profile permissions but unfortunately there doesn't seem to be a way to get both in one query.
Profile Json Response:
{
"firstName":{
"localized":{
"en_US":"Bob"
},
"preferredLocale":{
"country":"US",
"language":"en"
}
},
"localizedFirstName": "Bob",
"headline":{
"localized":{
"en_US":"API Enthusiast at LinkedIn"
},
"preferredLocale":{
"country":"US",
"language":"en"
}
},
"localizedHeadline": "API Enthusiast at LinkedIn",
"vanityName": "bsmith",
"id":"yrZCpj2Z12",
"lastName":{
"localized":{
"en_US":"Smith"
},
"preferredLocale":{
"country":"US",
"language":"en"
}
},
"localizedLastName": "Smith",
"profilePicture": {
"displayImage": "urn:li:digitalmediaAsset:C4D00AAAAbBCDEFGhiJ"
}
}
Contact Json Response:
{
"elements": [
{
"handle": "urn:li:emailAddress:3775708763",
"handle~": {
"emailAddress": "ding_wei_stub#example.com"
},
"primary": true,
"type": "EMAIL"
},
{
"handle": "urn:li:phoneNumber:6146249836070047744",
"handle~": {
"phoneNumber": {
"number": "158****1473"
}
},
"primary": true,
"type": "PHONE"
}
]
}
The reason might be that there are separated APIs, Contact API and Profile API.

Docusign not hitting webhook URL

I'm trying to use webhooks, but no events are being sent to my application via the webhook url. So far I was able to configure and send correctly envelopes with enough information to monitor status, but when things changes in the envelopes, nothing happens, I mean, no requests are made to my webhook URL, at all.
My app is doing good, so if I manually hit (GET) https://subdomain.app.com/docusign/webhook, it works fine and it shows both on my app log and Nginx log. But viewing, signing and completing documents/envelopes are not generating events to the webhook url.
I noticed that, in the examples, the events are capitalized for recientEvents, but not for envelopeEvents, is this right?
Is there anything else to be configured?
Is is possible to see this information in the Docusign web interface (https://account-d.docusign.com/logout#/username)? I would like to check if this data is correctly set in the envelope.
Here's the envelope request (minus some data):
{
"documents": [{
"documentId": 1,
"name": "XXXXXXXXX.pdf",
"documentBase64": "XXXXXXX"
}],
"recipients": {
"signers": [{
"tabs": {
"signHereTabs": [{
"documentId": 1,
"recipientId": 1,
"pageNumber": 1,
"anchorString": "recipient_signature"
}]
},
"name": "XXXXXX",
"email": "XXXX#XXXX.co",
"recipientId": 1,
"clientUserId": XXXX
}]
},
"eventNotification": {
"url": "https:\/\/subdomain.app.com\/docusign\/webhook",
"loggingEnabled": "true",
"envelopeEvents": [{
"envelopeEventStatusCode": "sent"
}, {
"envelopeEventStatusCode": "delivered"
}, {
"envelopeEventStatusCode": "completed"
}, {
"envelopeEventStatusCode": "declined"
}, {
"envelopeEventStatusCode": "voided"
}, {
"envelopeEventStatusCode": "sent"
}, {
"envelopeEventStatusCode": "sent"
}],
"recipientEvents": [{
"recipientEventStatusCode": "Sent"
}, {
"recipientEventStatusCode": "Delivered"
}, {
"recipientEventStatusCode": "Completed"
}, {
"recipientEventStatusCode": "Declined"
}, {
"recipientEventStatusCode": "AuthenticationFailed"
}, {
"recipientEventStatusCode": "AutoResponded"
}]
},
"status": "sent",
"emailSubject": "XXXXXX",
"brandId": "XXXXXXXXXX"
}
EDIT:
Entering Connect -> Log/Failures looks like the system is not really performing as it should, because sometimes I get
And some other times I get an empty list. Going in the publish option, when it's working I get a list of documents/envelopes, and I see the last envelope I sent there, which looks fine.
You can view your recent connect logs/failures at the Docusign Admin web application. See instructions to use the Admin site here
If your connect messages were not sent, to the listener URL you provided, they should show up in the failures section.
API : You can also view your connect logs/failure using the connectEvents api's
Here is some documentation for troubleshooting connect issues.
The capitalization of status codes is not an issue. They are case insensitive.

PayPal - PHP API, custom webform, unsuccessful payment without any errors

I've got a webform, sending data (and the customer) to PayPal.
My JSON request:
{
"intent":"sale",
"payer":{
"payment_method":"paypal"
},
"redirect_urls":{
"return_url":"http://dev1.url.de/payment/execute?method=paypal&price=1&order_number=123465&success=true",
"cancel_url":"http://dev1.url.de/payment/execute?method=paypal&price=1&order_number=123465&success=false"
},
"transactions":[
{
"amount":{
"currency":"EUR",
"total":"1"
},
"item_list":{
"items":[
{
"name":"123465",
"currency":"EUR",
"quantity":1,
"price":"1"
}
]
},
"invoice_number":"123465"
}
]
}
and a PayPal response:
{
"id":"PAY-0YV50781X7702245FKVDWC5A",
"create_time":"2015-05-04T12:09:24Z",
"update_time":"2015-05-04T12:09:24Z",
"state":"created",
"intent":"sale",
"payer":{
"payment_method":"paypal",
"payer_info":{
"shipping_address":{
}
}
},
"transactions":[
{
"amount":{
"total":"1.00",
"currency":"EUR",
"details":{
"subtotal":"1.00"
}
},
"invoice_number":"123465",
"item_list":{
"items":[
{
"name":"123465",
"price":"1.00",
"currency":"EUR",
"quantity":"1"
}
]
},
"related_resources":[
]
}
],
"links":[
{
"href":"https://api.paypal.com/v1/payments/payment/PAY-0YV70781X7192245FKVDWC5A",
"rel":"self",
"method":"GET"
},
{
"href":"https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-38Y65870AE087743W",
"rel":"approval_url",
"method":"REDIRECT"
},
{
"href":"https://api.paypal.com/v1/payments/payment/PAY-0YV50781X7170245FKVDWC5A/execute",
"rel":"execute",
"method":"POST"
}
]
}
And going through the payment process on the PayPal website seems all to be fine.
I can login, I can see all correct details and after I click on send payment, it redirects me to my form (return_url) and it says I paid.
The thing is .. it's not paid.
I can't see the money sent and it never arrives - not transaction is triggered. How can I figure out the problem? Or does anybody have a clue?

Categories