Skip to content

Instantly share code, notes, and snippets.

@josanua
Last active September 7, 2019 19:59
Show Gist options
  • Select an option

  • Save josanua/f312a2702e6363ae0e958afe4e766f24 to your computer and use it in GitHub Desktop.

Select an option

Save josanua/f312a2702e6363ae0e958afe4e766f24 to your computer and use it in GitHub Desktop.
paypall helper
<?php
// ppl ipn, test paypal
// sitename.com/?action=ipn_handler
add_action( 'init', 'paypal_ipn' );
// Custom IPN
function paypal_ipn(){
if ( isset( $_GET['action'] ) && $_GET['action'] == 'ipn_handler' ){
if ( $_SERVER['REQUEST_METHOD'] != 'POST' ){
header( 'Location: index.php' );
exit();
}
$ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, 'https://ipnpb.paypal.com/cgi-bin/webscr' );
curl_setopt($ch, CURLOPT_URL, 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "cmd=_notify-validate&" . http_build_query( $_POST ));
$response = curl_exec($ch);
curl_close($ch);
$pathtofile = $_SERVER["DOCUMENT_ROOT"] . '/ipn_logs/data.txt';
file_put_contents( $pathtofile, $response);
if ( $response == "VERIFIED" ){
$handle = fopen( $pathtofile, w );
foreach( $_POST as $key => $value ){
fwrite( $handle, "$key => $value \r\n" );
}
fclose($handle);
}
// $pathtofile = $_SERVER["DOCUMENT_ROOT"] . '/ipn_logs/data.txt'; // Should result in something like: /var/www/MySiteName/files/
// $content = 'Blah blah';
// $file = fopen($pathtofile, "w");
// if((fwrite($file, $content) == false) && (!file_exists($pathtofile))) {
// fclose($file);
// echo 'File not written, check permissions!';
// } else {
// fclose($file);
// echo 'Success!';
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment