I am getting it through this way:
let options = {
method: 'post', headers: {'content-type':'application/json','Access-Control-Allow-Credentials':true},
auth:{'username':process.env.PAYPALID,'password':process.env.PAYPALPASSWORD},
url: 'https://api.paypal.com/v1/oauth2/token',
data: 'grant_type=client_credentials',
}
axios(options).then((response)=>{let paypaltoken=response.data.access_token
axios.get('https://api.paypal.com/v1/payments/billing-agreements/'+agreementid+'/transactions?start_date=2018-01-01&end_date=2019-07-07', { headers: { 'Authorization':'Bearer '+paypaltoken, 'Content-Type':'application/json', } })
.then((transaction)=>{console.log(transaction.data)})
.catch(err => {console.error(err);console.log('err: '+JSON.stringify(err)); res.send (err) })
})
.catch(err => {console.error(err);console.log('err: '+JSON.stringify(err)); res.send (err) })
then if you get just the transaction.data, you will get a series of transaction objects, whose status
is == Completed
only if the transaction went ok, that is, it has not been cancelled, so just check the last for plan controlling purposes.
When status
is == Canceled
you know the agreement is not active anymore.
Another way to do it if you receive monthly payments is to set the first date to 2 months from "now()" and the second date to "now()". If you get no transactions then the status could be not active, but double-check: there is the random possibility there could have been some credit card problem. In that case I suppose status
could be == to delayed
or something else, but I had no possibilities to test it so I don't know. The idea came from this question and the relative second answer which deserves my gratitude as well as Cyril ALFARO.
Note that according to your case you may need to add 'Access-Control-Allow-Credentials':true
in the headers instead of other withCredentials: true
or similar in the request.