Get customer Email in return URL after making payment using PayPal button - paypal

How can i get customer Email in return URL after making payment using PayPal button?
Thanks.

<?php
if(isset($_GET['tx']))
{
$tx = $_GET['tx'];
$your_pdt_identity_token = //token
$request = curl_init();
// Set request options
curl_setopt_array($request, array
(
CURLOPT_URL => 'https://www.sandbox.paypal.com/cgi-bin/webscr', //sandbox!!!
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query(array
(
'cmd' => '_notify-synch',
'tx' => $tx,
'at' => $your_pdt_identity_token,
)),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
// CURLOPT_SSL_VERIFYPEER => TRUE,
// CURLOPT_CAINFO => 'cacert.pem',
));
// Execute request and get response and status code
$response = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
// Close connection
curl_close($request);
}
if($status == 200 AND strpos($response, 'SUCCESS') === 0)
{
$singlequantity = explode("\n",$response);
$email = $singlequantity[15];
$count = null;
$email = preg_replace('"%40"', '#', $email, -1, $count);
preg_match("/payer_email=(.*)/", $email, $email);
}
else
{
echo '<script type="text/javascript">
window.location="cancel.php";
</script>';
}
?>
i know i shoved it in real hard right now , but this might be useful to other users attempting to do this.

Related

Paypal, IPN/Webhooks create invoice in my paypal account, to send money

I want to create via IPN or Webhooks the following situation.
The user want to withdraw some money, when he clicks the button to withdraw, an invoice will be made in my Paypal account where I will have the option to Accept or Deny to send the money. After accept or deny, my app will receive an notification if the money was sent or not.
I am reading their documentation, but I don't find what I want.
I think you're a little bit confused with what PayPal features you would need for this.
IPN and Webhooks are post-transaction processing tools. They wouldn't trigger anything until AFTER an invoice was already created, a payment was received, a dispute was submitted, etc.
Also, you don't want to do this with the actual Invoicing API because PayPal charges higher fees for that.
If you provide your user with a button to withdrawal money you could trigger the payout directly on that action using the Payouts API.
You did not specify what language you are working with, but here's a sample of a PHP script that would trigger a payout:
<?php
$paypal_client_id = "your_client_id";
$paypal_secret = "your_secret";
$payee_email = "payee#example.com";
$amount = 10.00;
$currency = "USD";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . base64_encode("$paypal_client_id:$paypal_secret"),
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$access_token = json_decode($response)->access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/payments/payouts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"sender_batch_header\": {\"sender_batch_id\":\"batch_" . time() . "\",\"email_subject\":\"You have a payment\"},\"items\":[{\"recipient_type\":\"EMAIL\",\"amount\":{\"value\":$amount,\"currency\":\"$currency\"},\"receiver\":\"$payee_email\",\"note\":\"Thank you.\",\"sender_item_id\":\"item_" . time() . "\"}]}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer $access_token"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$payout = json_decode($response);
if ($payout->batch_header->batch_status == "SUCCESS") {
echo "Payout sent successfully!";
} else {
echo "Payout failed: " . $payout->batch_header->failure_reason;
}
}
}
To have your app receive notifications when the Payout was completed you can subscribe to the PAYMENT.PAYOUTSBATCH.SUCCESS Webhook.
Here is a sample of a script that would subcribe that webhook:
<?php
$paypal_client_id = "your_client_id";
$paypal_secret = "your_secret";
$webhook_url = "https://www.example.com/webhooks/payouts_success";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . base64_encode("$paypal_client_id:$paypal_secret"),
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$access_token = json_decode($response)->access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/notifications/webhooks",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"url\":\"$webhook_url\",\"event_types\":[{\"name\":\"PAYMENT.PAYOUTSBATCH.SUCCESS\"}]}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer $access_token"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$webhook = json_decode($response);
if ($webhook->name == "PAYMENT.PAYOUTSBATCH.SUCCESS") {
echo "Webhook subscribed successfully!";
} else {
echo "Webhook subscription failed: " . $webhook->name;
}
}
}
Then you would setup a webhook handler at the webhook URL you provided. Here is an example of how that might look:
<?php
$webhook_data = json_decode(file_get_contents('php://input'), true);
if ($webhook_data["event_type"] == "PAYMENT.PAYOUTSBATCH.SUCCESS") {
// handle the payout success event
$batch_id = $webhook_data["resource"]["batch_header"]["payout_batch_id"];
$status = $webhook_data["resource"]["batch_header"]["batch_status"];
// log the batch ID and status for reference
error_log("Batch ID: $batch_id");
error_log("Status: $status");
// process the successful payout
// ...
} else {
// handle other event types
// ...
}

Drupal7 REST: I am not able to perform POST and PUT methods. Error is :Not Acceptable : Node type is required, Code:406?

I'm using drupal7. my drupal_http_request() for get and delete are working fine for authenticated users, but the post and put methods are not working.
The error is :Not Acceptable : Node type is required, and http error code is :406. My code is below:
function ws_form_post_auth() {
$base_url = 'http://localhost/drupalws/api/v1';
$data = array(
'username' => 'student1',
'password' => 'welcome',
);
$data = http_build_query($data, '', '&');
$options = array(
'headers' => array(
'Accept' => 'application/json',
),
'method' => 'POST',
'data' => $data
);
$response = drupal_http_request($base_url . '/user/login', $options);
$data = json_decode($response->data);
// Check if login was successful
if ($response->code == 200) {
$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
$options['headers']['X-CSRF-Token'] = $data->token;
$data = array(
'title' => 'First forum post',
'type'=> 'forum',
'body'=> array(
'und'=>array(
0=> array(
'value'=>'This is my first forum post via httprequest.'
)
)
)
);
$data = json_encode($data);
$options['data'] = $data;
$options['method'] = 'POST';
$response = drupal_http_request($base_url . '/node', $options);
return $response->status_message;
}
return $response->status_message;
}
I got the solution for my issue,I just missed a Content-Type in Headers.
[....]
if ($response->code == 200) {
$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
$options['headers']['X-CSRF-Token'] = $data->token;
$options['headers']['Content-Type'] = 'application/json';
[....]

Error 100 schedule a post on Facebook Page

I've used the following code. It works fine without 'scheduled_publish_time', otherwise I get this error "(#100) You cannot specify a scheduled publish time on a published post".
I've previously registered my app with another piece of code. It's so weird.
include_once("inc/facebook.php"); //include facebook SDK
$appId = '21xxxxxxxxxxx'; //Facebook App ID
$appSecret = '6b8f4bxxxxxxxxxxxxxd56'; // Facebook App Secret
$return_url = 'http://localhost:8888/...'; //return url (url to script)
$homeurl = 'http://localhost:8888/...'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true,
'fileUpload' => true
));
$accounts = $facebook->api('/me/accounts');
$PAGE_ID = get_option('fb_post_cron_page'); // it is an option saved in WordPress
foreach($accounts['data'] as $account){
if($account['id'] == $PAGE_ID){
$ACCESS_TOKEN = $account['access_token'];
}
}
$post_url = '/'.$PAGE_ID.'/photos';
$upload_dir = wp_upload_dir();
$upload_dir= $upload_dir['path'];
$timezone= 'Europe/Rome';
$date = new DateTime($dateStr, new DateTimeZone($timezone));
//posts message on page statues
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => true,
'scheduled_publish_time' => $date->getTimestamp()
);
try {
$postResult = $facebook->api($post_url, 'post', $args );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
you have to set 'published' to false
$args = array(
'access_token' => $ACCESS_TOKEN,
'source' => '#' . $image_abs_path,
'message' => $post_message,
'published' => false,
'scheduled_publish_time' => $date->getTimestamp()
);

Real-Time Checkins for a page?

cannot figure out how to get page checkins working through the real time api. Facebook says that page checkins are available through the API, but i'm not seeing how the appid connects with pageids. I do have my application added to the page i want to track. Here is the code I'm working against:
// Please make sure to REPLACE the value of VERIFY_TOKEN 'abc' with
// your own secret string. This is the value to pass to Facebook
// when add/modify this subscription.
define('VERIFY_TOKEN', 'acheckin');
$method = $_SERVER['REQUEST_METHOD'];
// In PHP, dots and spaces in query parameter names are converted to
// underscores automatically. So we need to check "hub_mode" instead
// of "hub.mode".
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
$_GET['hub_verify_token'] == VERIFY_TOKEN)
{
echo $_GET['hub_challenge'];
exit;
} else if ($method == 'POST')
{
//$updates = json_decode(file_get_contents("php://input"), true);
$message = 'wedidit!';//file_get_contents("php://input");
mail('ivan#ivanmayes.com', 'test', $message);
error_log('updates = ' . print_r($updates, true));
exit;
} else if ($method == 'GET' && $_GET['check_subscription'] == 'true' )
{
require_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '284647438227103',
'secret' => '162817ff51aacfb7c0d1420ee0f687ef'
));
$access_token = $facebook->getAccessToken();
$param = array('access_token' => $access_token);
$subs = $facebook->api('/284647438227103/subscriptions', $param);
var_dump($subs);
$message = 'checkemail';//file_get_contents("php://input");
mail('ivan#ivanmayes.com', 'test', $message);
error_log('checkingerrorlog');
exit;
}
require_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '284647438227103',
'secret' => '162817ff51aacfb7c0d1420ee0f687ef'
));
$access_token = $facebook->getAccessToken();
$param = array('access_token' => $access_token,
'object' => 'page',
'fields' => 'checkins',
'callback_url' => 'http://www.ivanmayes.com/arduino/checkins/checkins.php',
'verify_token' => 'acheckin'
);
$subs = $facebook->api('/284647438227103/subscriptions', 'POST', $param);

(iphone) inAppPurchase verifyReceipt using MKStoreKit

I am testing in app purchase with MKStoreKit.
I'm getting response's status 21002 and wonder why.
Do I need to set up a certificate or something to talk to apple server?
Below is the php code that MKStoreKit uses
<?php
$devmode = TRUE; // change this to FALSE after testing in sandbox
$receiptdata = $_POST['receiptdata'];
$udid = $_POST['udid'];
if($devmode)
{
$appleURL = "https://sandbox.itunes.apple.com/verifyReceipt";
}
else
{
$appleURL = "https://buy.itunes.apple.com/verifyReceipt";
}
$receipt = json_encode(array("receipt-data" => $receiptdata));
$response_json = do_post_request($appleURL, $receipt);
$response = json_decode($response_json);
file_put_contents('php://stderr', print_r($response->{'status'}, true));
file_put_contents('php://stderr', print_r($udid, true));
if($response->{'status'} == 0)
{
file_put_contents('php://stderr', print_r("yes", true));
error_log('udid: %s', $udid);
error_log('quantity: %d', $response->{'receipt'}->quantity);
echo ('YES');
}
else
{
echo ('NO');
}
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
Please check Verify Purchase
MKStore Kit has a bug with sending receiptdata to server
You should base64 encode receiptData not asciiStringEncoding.
Used the following link's code to base64 and I get status 0.
Verify receipt for in App purchase