How can I forward PayPal IPN to another PHP file? - php

We are updating our system and the old IPNs need to be processed differently. However, I cannot change the original IPN send-to addresses. So I have to "bounce" the IPN message off of old_IPNProcessor.php and forward it to new_IPNProcessor.php. I cannot modify old_IPNProcessor.php for technical reasons that go beyond the scope of this question.
From what I have read, I have to use cURL to accomplish this. I have seen some code for this, but I am unsure as to how it's implemented. For instance, the $post var in the code below (from https://stackoverflow.com/a/6213693/1390395) is the raw json straight from PayPal. Does it have to be in a PHP array for this to work?
function forwardIPN($post){
$url = "new_IPNProcessor.php";
// $post is the ipn message from PayPal
$content = $post;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

Related

UPDATE records using REST api

I am working on PHP and Salesforce integration and I want to update records using PHP.
I want to update records in Salesforce when clicking on the Button "Follow up".
And also try the below code.
function update_account($id, $followup, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Contact/$id";
$content = json_encode(array("shivendra__FollowUp__c" => $followup));
echo $content;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token",
"Content-type: application/json"));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 204 ) {
die("Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
echo "<b>update_contact function</b>";
echo "HTTP status $status updating Contact<br/><br/>";
curl_close($curl);
}
But getting an error:
{"shivendra__FollowUp__c":null}
[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET,POST"}]
Error: call to URL https://shivendra-dev-ed.my.salesforce.com/services/data/v20.0/sobjects/Contact/ failed with status 405, curl_error , curl_errno 0
Try the post method with cURL, similar to this:
curl_setopt($ch, CURLOPT_POST, 1);

Paypal Subscription : Things don't appear to be working at the moment. Please try again later

I am currently working with the setup of PayPal Subscription through REST API in sandbox environment.
I am able to add product, add plan and also add subcription with APIs.
But at the payment page ( on the PayPal side ), where it asks to click for subscription, error occurs.
Error occurs at the page: https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-0T473376T5204322K, when i hit the Login to subscribe button.
Error Page Image
When i check the console, here's the response that it triggers on the page:
Request URL : https://www.sandbox.paypal.com/webapps/billing/api/billagmt/BA-0T473376T5204322K/createCart
Headers
Request Method: POST
Status Code: 400 Bad Request
Response
{"ack":"contingency","contingency":"VALIDATION_ERROR","meta":{"calc":"...some string...","rlog":"...some string.."},"server":"...some string..."}
I am unable to do any kind of debug as it occurs over PayPal Page.
Kindly help me to find if what is going wrong at my side (i.e., API) ?
My Code
API Call ( Add Subscription )
$request = json_encode($req);
$header = [ 'Content-Type: application/json', 'Authorization: Bearer '.$this->getOauthToken() ];
$method = 'POST';
$url = $this->paypal_url.'billing/subscriptions';
$response = json_decode($this->callAPI($header, $method, $request, $url));
Helpers
// Call API
private function callAPI($header, $method, $request, $url)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($request)
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($request)
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
break;
default:
if ($request)
$url = sprintf("%s?%s", $url, http_build_query($request));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){ die("Connection Failure"); }
curl_close($curl);
return $result;
}
// Fetch Oauth Token
private function getOauthToken()
{
$url = $this->paypal_url.'oauth2/token';
$header = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($this->client_id.':'.$this->client_secret)
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if(!$result) { die("Connection Failure"); }
curl_close($curl);
$response = json_decode($result);
return $response->access_token;
}
It gives me HATEOAS link like: https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-0T473376T5204322K, where the error occurs.
Also my data is complete OK as per API requirements.
Because i have already modified all possible errors regarding the data incompatibilty.
If it's complete description about problem, then please try to help me not by just triggering hold, but figuring out the issue.
We had a similar problem. Some subscription payments were going through and others not. The form payload looked the same.
When it was returning the error, we realised that the value for the number of times that subscription payments recur (srt) was set to 1. The docs point out that the minimum value shoudbe 2 and maximum of 52.

Second request to API results in empty string

First request to API:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Account');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
$response = json_decode($response, true);
I get accounts info as expected. But when i try to do next request after some manipulations with accounts, in response i get empty string.
There is an example of next request:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Contacts');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
Doing requests to API from browser works properly.
Problem were in login/pass data. I don't know exactly how, but login/pass vars were changed before executing second request. Thanks for assistance!

sending a response to another php file using $curl returns an error

I am trying to send JSON data to another php file for now for learning purpose (actually meant to be sent to a react-native app) but this returns an error saying
Error: call to URL http://localhost/hhh.php failed with status 200, response , curl_error , curl_errno 0
if($cliSeq==0)
{
$welcomearr= array("welcome");
$url = "http://localhost/hhh.php";
$welcomeJSON = json_encode($welcomearr);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $welcomeJSON);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

Issue with PHP curl / json POST

I am running into an issue trying to issue a POST to the chargify API log payments from within a form.
However, it appears that the POST is actually being sent as a GET for some reason as it's returning an error.
Can anyone offer any advice on what I may be doing wrong? Postman is working just fine sending a POST with the JSON as the body, but I can't get it to work from PHP.
(note: the <urlmaskedforprivacy> is obviously not in my actual code)
//find invoice id # using invoice number search form
$invoiceNumber = fRequest::get('invoicenumber');
$json = file_get_contents('https://<maskedforprivacy>.chargify.com/invoices.json?number=' . $invoiceNumber);
$returnedInvData = fJSON::decode($json);
$invoiceId = $returnedInvData[0]->invoice->id;
// grab form data as payment info to send to chargify
$paymentAmount = fRequest::Get('amount');
$memo = fRequest::Get('memo');
if ((isset($invoiceNumber,$paymentAmount,$memo))) {
$url = 'https://<maskedforprivacy>.chargify.com/subscriptions/' . $invoiceId . '/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = FJSON::encode($params);
// send to chargify
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}
I've used this one code, but it returns 401 error and asks for Basic auth. in json_response variable. May be you've missed it?
$paymentAmount = 10;
$memo = 'test';
$url = 'https://test.chargify.com/subscriptions/1/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = json_encode($params);
// send to chargify
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);

Categories