Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> PayPal Sandbox - jak testować?
ZaqU
post 7.01.2015, 23:29:29
Post #1





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Witajcie,

Natknąłem się na pewien problem z którym nie mogę sobie poradzić. Pytanie do osób, które miały już styczność z podłączaniem i testowaniem płatności PayPal. Mam do dyspozycji skrypt systemu depozytów z codecanyon
http://codecanyon.net/item/deposit-system/59753
i chciałbym go dostosować do własnych potrzeb. Działa on na takiej zasadzie, że wysyła następujący formularz:
  1. <form name="depositform" id="depositform" method="post" action="<?php echo self::payment_action(); ?>">
  2. <input type="hidden" name="rm" value="2"/>
  3. <input type="hidden" name="cmd" value="_xclick"/>
  4. <input type="hidden" name="business" value="<?php echo $paypalmail; ?>"/>
  5. <input type="hidden" name="item_name" value="Deposit cash"/>
  6. <input type="hidden" name="no_shipping" value="1"/>
  7. <input type="hidden" name="return" value="<?php echo $siteurl; ?>deposit_example.php?action=success"/>
  8. <input type="hidden" name="notify_url" value="<?php echo $siteurl; ?>includes/depositipn.php"/>
  9. <input type="hidden" name="cancel_return" value="<?php echo $siteurl; ?>deposit_example.php?action=cancel"/>
  10. <input type="hidden" name="custom" value="<?php echo $useridentify; ?>" />
  11. <input type="hidden" name="amount" value="<?php echo $amount; ?>" />
  12. <p><?php echo $depositconfirm; ?></p>
  13. <input type="submit" name="checkout" value="Proceed with payment" />
  14. </form>

na adres: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_flow&SESSION=...&dispatch=...

Niestety nie wiem co dalej. Po wypełnieniu formularza pojawia się informacja że płatność została zaakceptowana, na podany adres e-mail przyjdzie potwierdzenie, a niestety nie działa to tak jak powinno, tj. nie wysyła maila i nie zapisuje w systemie płatności. Muszę przetestować czy ten system działa, ale jak na razie nie wiem jak. To czego potrzebuję, to informacja na temat tego jakie API wykorzystuje podana wyżej metoda i gdzie szukać dokumentacji do niej (PayPal oferuje dziesiątki różnych API i trudno się połapać, a nigdy wcześniej nie korzystałem z tego systemu).
Go to the top of the page
+Quote Post
irytek
post 8.01.2015, 12:57:05
Post #2





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


akurat to co używasz to PayPal Standard : http://irytek.com/paypal_standard_html.html i nie wymaga ani nie wykorzystuje żadnych wywołań API.

Wracając do Twojego problemu to:
1. wejdź na stronę: developer.paypal.com, zaloguj się tam używając swojego konta PayPal i stwórz 2 konta sandboxowe: business oraz personal. Adresy mailowe nie muszą istnieć gdyż maile przychodzą na wirtualną skrzynkę w sandboxe.

2. w twoim formularzu pod business podaj adres email twojego sandboxowego konta businessowego

3. płatność dokonaj twoim sandboxowym kontem personal

Wiadomości przesłane po zapłacie znajdziesz wchodząc na developer.paypal.com -> sandbox accoutns -> kliknij na adres mailowy konta które chcesz sprawdzić -> nofitications

dokumentacja paypal jest również pod linkiem developer.paypal.com

Po dokonaniu płatności na adres wskazany w notify_url przyjdzie powiadomienie o płatności jako $_POST (http://irytek.com/IPN_instant_payment_notification.html)

daj znać jeśli potrzebujesz więcej wskazówek.

Ten post edytował irytek 8.01.2015, 13:06:42
Go to the top of the page
+Quote Post
ZaqU
post 8.01.2015, 21:07:58
Post #3





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Świetnie, to było coś czego szukałem!

Zastanawiam się jednak jak obsłużyć i zweryfikować przesłane przez PayPal dane. Chodzi przede wszystkim o bezpieczeństwo, poprzez weryfikację czy nadesłane dane pochodzą ze strony PayPal czy nie. W poniższym kodzie program dochodzi wyłącznie do BREAKPOINTów 1 i 6 (linie: 31 i 81), pozostałe omija, nie wiem dlaczego...


  1. <?php
  2. include("../config/config.inc.php");
  3. include("../config/siteinfo.php");
  4. include("../classes/Site.php");
  5.  
  6. //paypal or sandbox?
  7. if($testmode === true)
  8. {
  9. $paypalaction = "ssl://www.sandbox.paypal.com";
  10. } else {
  11. $paypalaction = "ssl://www.paypal.com";
  12. }
  13. // read the post from PayPal system and add 'cmd'
  14. $req = 'cmd=_notify-validate';
  15. foreach ($_POST as $key => $value)
  16. {
  17. $value = urlencode(stripslashes(mysqli_real_escape_string($link, $value)));
  18. $req .= "&$key=$value";
  19. }
  20.  
  21. // post back to PayPal system to validate
  22. $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
  23. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  24. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  25. $fp = fsockopen ($paypalaction, 443, $errno, $errstr, 30);
  26.  
  27. // assign posted variables to local variables
  28. $user_id = mysqli_real_escape_string($link, $_POST['custom']);
  29. $txn_id = mysqli_real_escape_string($link, $_POST['txn_id']);
  30. $payment_amount = mysqli_real_escape_string($link, $_POST['mc_gross']);
  31. $receiver_email = mysqli_real_escape_string($link, $_POST['receiver_email']);
  32. $payer_email = mysqli_real_escape_string($link, $_POST['payer_email']);
  33.  
  34. if ($fp)
  35. {
  36. fputs ($fp, $header . $req);
  37. while (!feof($fp))
  38. {
  39. // [BREAKPOINT 1]
  40. $res = fgets ($fp, 1024);
  41. if (strcmp ($res, "VERIFIED") == 0)
  42. {
  43. //allready exist
  44. $check = mysqli_num_rows(mysqli_query($link, "SELECT * FROM deposit_history WHERE unique_code='".$txn_id."'"));
  45. if($check == 0)
  46. {
  47. $params = array(
  48. 'userid' => $_POST['custom'],
  49. 'txn_id' => $_POST['ipn_track_id'],
  50. 'amount' => $_POST['payment_gross'],
  51. 'payermail' => $_POST['payer_email'],
  52. );
  53. // [BREAKPOINT 2]
  54. Site::processPayment($params);
  55. // [BREAKPOINT 3]
  56. }
  57. else
  58. {
  59. $headers = "From:".$adminmail."\r\n";
  60. $mailmessage = Site::loadMailTemplate('transaction_duplicate.html', array(
  61. 'username' => $depositcash->$username,
  62. 'sitename' => $sitename,
  63. ));
  64. mail($payer_email,"Your deposit failed",$mailmessage,$headers);
  65. // [BREAKPOINT 4]
  66. }
  67. }
  68. elseif (strcmp ($res, "INVALID") == 0)
  69. {
  70. // PAYMENT INVALID & INVESTIGATE MANUALY!
  71. $headers = "From:".$adminmail."\r\n";
  72. $mailmessage = Site::loadMailTemplate('deposit_error.html', array(
  73. 'username' => $depositcash->$username,
  74. 'amount' => $amount,
  75. 'sitename' => $sitename,
  76. ));
  77. mail($payer_email,"Failure of your deposit",$mailmessage,$headers);
  78. // [BREAKPOINT 5]
  79. }
  80. }
  81. // [BREAKPOINT 6]
  82. fclose ($fp);
  83. }


Ten post edytował ZaqU 8.01.2015, 21:08:26
Go to the top of the page
+Quote Post
irytek
post 9.01.2015, 11:56:14
Post #4





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


Hejki,
to jest bardzo stary listener. Powinien zadziałać jeśli zmienisz HTTP NA 1.1 w linii 22 oraz 41 $res zmień na trim($res).

Jednakże proponuje wykorzystać curla i oficjalny kod listnera:
https://github.com/paypal/ipn-code-samples/.../paypal_ipn.php

Sprawdzi tylko czy masz curl włączony.

  1. <?php
  2. // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
  3. // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
  4. // Set this to 0 once you go live or don't require logging.
  5. define("DEBUG", 1);
  6. // Set to 0 once you're ready to go live
  7. define("USE_SANDBOX", 1);
  8. define("LOG_FILE", "./ipn.log");
  9. // Read POST data
  10. // reading posted data directly from $_POST causes serialization
  11. // issues with array data in POST. Reading raw POST data from input stream instead.
  12. $raw_post_data = file_get_contents('php://input');
  13. $raw_post_array = explode('&', $raw_post_data);
  14. $myPost = array();
  15. foreach ($raw_post_array as $keyval) {
  16. $keyval = explode ('=', $keyval);
  17. if (count($keyval) == 2)
  18. $myPost[$keyval[0]] = urldecode($keyval[1]);
  19. }
  20. // read the post from PayPal system and add 'cmd'
  21. $req = 'cmd=_notify-validate';
  22. if(function_exists('get_magic_quotes_gpc')) {
  23. $get_magic_quotes_exists = true;
  24. }
  25. foreach ($myPost as $key => $value) {
  26. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  27. $value = urlencode(stripslashes($value));
  28. } else {
  29. $value = urlencode($value);
  30. }
  31. $req .= "&$key=$value";
  32. }
  33. // Post IPN data back to PayPal to validate the IPN data is genuine
  34. // Without this step anyone can fake IPN data
  35. if(USE_SANDBOX == true) {
  36. $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  37. } else {
  38. $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
  39. }
  40. $ch = curl_init($paypal_url);
  41. if ($ch == FALSE) {
  42. return FALSE;
  43. }
  44. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  45. curl_setopt($ch, CURLOPT_POST, 1);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  49. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  50. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  51. if(DEBUG == true) {
  52. curl_setopt($ch, CURLOPT_HEADER, 1);
  53. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  54. }
  55. // CONFIG: Optional proxy configuration
  56. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
  57. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  58. // Set TCP timeout to 30 seconds
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  61. // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
  62. // of the certificate as shown below. Ensure the file is readable by the webserver.
  63. // This is mandatory for some environments.
  64. //$cert = __DIR__ . "./cacert.pem";
  65. //curl_setopt($ch, CURLOPT_CAINFO, $cert);
  66. $res = curl_exec($ch);
  67. if (curl_errno($ch) != 0) // cURL error
  68. {
  69. if(DEBUG == true) {
  70. error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
  71. }
  72. curl_close($ch);
  73. } else {
  74. // Log the entire HTTP response if debug is switched on.
  75. if(DEBUG == true) {
  76. error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
  77. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  78. }
  79. curl_close($ch);
  80. }
  81. // Inspect IPN validation result and act accordingly
  82. // Split response headers and payload, a better way for strcmp
  83. $tokens = explode("\r\n\r\n", trim($res));
  84. $res = trim(end($tokens));
  85. if (strcmp ($res, "VERIFIED") == 0) {
  86. // check whether the payment_status is Completed
  87. // check that txn_id has not been previously processed
  88. // check that receiver_email is your PayPal email
  89. // check that payment_amount/payment_currency are correct
  90. // process payment and mark item as paid.
  91. // assign posted variables to local variables
  92. //$item_name = $_POST['item_name'];
  93. //$item_number = $_POST['item_number'];
  94. //$payment_status = $_POST['payment_status'];
  95. //$payment_amount = $_POST['mc_gross'];
  96. //$payment_currency = $_POST['mc_currency'];
  97. //$txn_id = $_POST['txn_id'];
  98. //$receiver_email = $_POST['receiver_email'];
  99. //$payer_email = $_POST['payer_email'];
  100.  
  101. if(DEBUG == true) {
  102. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  103. }
  104. } else if (strcmp ($res, "INVALID") == 0) {
  105. // log for manual investigation
  106. // Add business logic here which deals with invalid IPN messages
  107. if(DEBUG == true) {
  108. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  109. }
  110. }
  111. ?>
Go to the top of the page
+Quote Post
ZaqU
post 9.01.2015, 12:52:32
Post #5





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Sądzę że lepszym rozwiązaniem będzie wykorzystanie oficjalnego kodu listenera.

Chyba powoli dochodzimy do sedna problemu. Funkcja curl_error() zgłasza mi taki oto błąd: "Unsupported protocol: ssl" - co może być przyczyną i czy można temu jakoś zaradzić? Problem naprawiony wink.gif

Ten post edytował ZaqU 9.01.2015, 12:57:53
Go to the top of the page
+Quote Post
irytek
post 9.01.2015, 12:57:35
Post #6





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


wywal tel liniki:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

powinno pomóc

I sprawdź jeszcze czy na twoim serwerze obsługiwany jest TLS gdyź SSL PayPal już nie obsługuje.

Ten post edytował irytek 9.01.2015, 12:59:31
Go to the top of the page
+Quote Post
ZaqU
post 9.01.2015, 13:00:03
Post #7





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Pomieszałem trochę starego kodu z nowym i nie zauważyłem, że protokoły są inne. W starej wersji było "ssl://www.sandbox.paypal.com", w nowej "https://www.sandbox.paypal.com". Podmieniłem oba linki i pomogło.
Go to the top of the page
+Quote Post
irytek
post 9.01.2015, 13:06:16
Post #8





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


pięknie, życzę wielu sukcesów smile.gif
Go to the top of the page
+Quote Post
ZaqU
post 10.01.2015, 15:17:48
Post #9





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Pojawił się jeszcze jeden (ostatni mam nadzieję) problem. Mianowicie, po wykonaniu cURL'a, komenda curl_exec() zwraca (moim zdaniem) coś zupełnie nieoczekiwanego. W każdym bądź razie, kod w liniach 6-7 nie działa tak jak powinien, nie rozdziela odpowiedzi cURL do tablicy i przez to nie może uzyskać ostatniego elementu tablicy. Pierwsze dwa error_log'i zwracają dokładnie tą samą wartość, zaś kolejne (ostatnie) dwa nie wykonują się w ogóle.

Kod PHP:
  1. $res = curl_exec($ch);
  2. error_log(date('[Y-m-d H:i e] '). "Res:" . $res . PHP_EOL, 3, LOG_FILE);
  3.  
  4. // ...
  5.  
  6. $tokens = explode("\r\n\r\n", trim($res));
  7. $res = trim(end($tokens));
  8.  
  9. error_log(date('[Y-m-d H:i e] '). "Res (end) (trim): " . $res . PHP_EOL, 3, LOG_FILE);
  10.  
  11. if(strcmp ($res, "VERIFIED") == 0) {
  12. // ...
  13.  
  14. if($testmode == true) {
  15. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  16. }
  17. } else if (strcmp ($res, "INVALID") == 0) {
  18. // log for manual investigation
  19. // Add business logic here which deals with invalid IPN messages
  20. if($testmode == true) {
  21. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  22. }
  23. }

Error_log (czyt. zawartość zmiennej $res):
  1. HTTP/1.1 301 Moved Permanently
  2. Date: Sat, 10 Jan 2015 12:42:01 GMT
  3. Server: Apache
  4. X-Frame-Options: SAMEORIGIN
  5. Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=CeY3DFkK4kmUWbEDXYM7uueRiUOFPa-84KUAovXIG_fsXe8Hh2D3poYB0juYEko72ZHJDP-BBSzGZ3dsAMAK7YCnGpJNNeyx_8mtlj3kfzin2oMxAS4ybmGyMzEebF9tbDE_qn4zbwU8xnQ9Uk5cxjW
    Wg9J29JQBa4Ejgnp_nd2K407qtd5fwLmmiDuRJdBKS5XyIISWVBE2yzMk6H4F3-YBzcFyd3N4uij0EB4qF8sLHFNR9YxjHHUK1EO; domain=.paypal.com; path=/; Secure; HttpOnly
  6. Set-Cookie: cookie_check=yes; expires=Tue, 07-Jan-2025 12:42:01 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
  7. Set-Cookie: Apache=10.72.108.11.1420893721319433; path=/; expires=Mon, 02-Jan-45 12:42:01 GMT
  8. Vary: Accept-Encoding,User-Agent
  9. Connection: close
  10. Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.WEB.1%26silo_version%3D880%26app%3Dslingshot%26TIME%3D421441876; domain=.paypal.com; path=/; Secure; HttpOnly
  11. Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
  12. Set-Cookie: Apache=10.72.128.11.1420893721308461; path=/; expires=Mon, 02-Jan-45 12:42:01 GMT
  13. Location: [url="https://www.sandbox.paypal.com/pl/cgi-bin/webscr?cmd=_home&country_lang.x=true"]https://www.sandbox.paypal.com/pl/cgi-bin/w...try_lang.x=true[/url]
  14. Strict-Transport-Security: max-age=14400
  15. Transfer-Encoding: chunked
  16. Content-Type: text/html; charset=ISO-8859-1


Czy ktoś domyśla się w czym może leżeć problem?

Ten post edytował ZaqU 10.01.2015, 15:18:50
Go to the top of the page
+Quote Post
irytek
post 10.01.2015, 16:37:04
Post #10





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


Hej,
podaj jak dokładnie wygląda paypal_url, albo jeśli to nie kłopot to wklej cały kod tego pliku lub prześlij na priv
Go to the top of the page
+Quote Post
ZaqU
post 10.01.2015, 19:01:49
Post #11





Grupa: Zarejestrowani
Postów: 71
Pomógł: 1
Dołączył: 21.01.2013

Ostrzeżenie: (0%)
-----


Oto cały kod:

  1. <?php
  2. include("../config/config.inc.php");
  3. include("../config/siteinfo.php");
  4. include("../classes/Site.php");
  5.  
  6. define("LOG_FILE", "/DEBUG_LOG_FOR_DEVELOPER.log");
  7.  
  8. //paypal or sandbox?
  9. if( $testmode === true ) {
  10. $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  11. } else {
  12. $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
  13. }
  14.  
  15.  
  16. // read the post from PayPal system and add 'cmd'
  17. $req = 'cmd=_notify-validate';
  18. foreach( $_POST as $key => $value ) {
  19. $value = urlencode(stripslashes(mysqli_real_escape_string($link, $value)));
  20. $req .= "&$key=$value";
  21. }
  22. error_log(date('[Y-m-d H:i e] '). "Req:" . $req . PHP_EOL, 3, LOG_FILE);
  23.  
  24. // Post IPN data back to PayPal to validate the IPN data is genuine
  25. // Without this step anyone can fake IPN data
  26. $ch = curl_init($paypal_url);
  27. if ($ch == FALSE) {
  28. error_log(date('[Y-m-d H:i e] '). "Connection failed!" . PHP_EOL, 3, LOG_FILE);
  29. }
  30. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  31. curl_setopt($ch, CURLOPT_POST, 1);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  33. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  35. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  36. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  37. if( $testmode == true ){
  38. curl_setopt($ch, CURLOPT_HEADER, 1);
  39. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  40. }
  41. // CONFIG: Optional proxy configuration
  42. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
  43. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  44. // Set TCP timeout to 30 seconds
  45. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  47.  
  48. $res = curl_exec($ch);
  49.  
  50. error_log(date('[Y-m-d H:i e] '). "Res:" . $res . PHP_EOL, 3, LOG_FILE);
  51.  
  52. if (curl_errno($ch) != 0){ // cURL error
  53. if($testmode == true) {
  54. error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
  55. }
  56. curl_close($ch);
  57. } else {
  58. // Log the entire HTTP response if debug is switched on.
  59. // Log the entire HTTP response if debug is switched on.
  60. if($testmode == true) {
  61. error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
  62. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  63. }
  64. curl_close($ch);
  65. }
  66.  
  67. $tokens = explode("\r\n\r\n", trim($res));
  68. $res = trim(end($tokens));
  69.  
  70. error_log(date('[Y-m-d H:i e] '). "Res (end) (trim): " . $res . PHP_EOL, 3, LOG_FILE);
  71.  
  72. if(strcmp ($res, "VERIFIED") == 0) {
  73. // check whether the payment_status is Completed
  74. // check that txn_id has not been previously processed
  75. // check that receiver_email is your PayPal email
  76. // check that payment_amount/payment_currency are correct
  77. // process payment and mark item as paid.
  78. // assign posted variables to local variables
  79. //$item_name = $_POST['item_name'];
  80. //$item_number = $_POST['item_number'];
  81. //$payment_status = $_POST['payment_status'];
  82. //$payment_amount = $_POST['mc_gross'];
  83. //$payment_currency = $_POST['mc_currency'];
  84. //$txn_id = $_POST['txn_id'];
  85. //$receiver_email = $_POST['receiver_email'];
  86. //$payer_email = $_POST['payer_email'];
  87.  
  88. if($testmode == true) {
  89. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  90. }
  91. } else if (strcmp ($res, "INVALID") == 0) {
  92. // log for manual investigation
  93. // Add business logic here which deals with invalid IPN messages
  94. if($testmode == true) {
  95. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  96. }
  97. }
Go to the top of the page
+Quote Post
irytek
post 10.01.2015, 22:49:51
Post #12





Grupa: Zarejestrowani
Postów: 16
Pomógł: 5
Dołączył: 31.12.2014

Ostrzeżenie: (0%)
-----


Proponowałbym jednak aż tak nie modyfikować kodu w szczególności początku gdzie szczytuje się dane z posta które są bardzo podatne na serializacje. Nie dodawaj do $req żadnych dodatkowych znaków gdyż dane wysłane do PayPal muszą być identyczne do tych które otrzymasz poddane urlencode.

Proponuje zostawić kod w niezmienionej formie i przetestować go najpierw z symulatorem:
https://developer.paypal.com/webapps/develo...s/ipn_simulator

  1. <?php
  2. include("../config/config.inc.php");
  3. include("../config/siteinfo.php");
  4. include("../classes/Site.php");
  5.  
  6. define("LOG_FILE", "/DEBUG_LOG_FOR_DEVELOPER.log");
  7.  
  8.  
  9.  
  10. // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
  11. // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
  12. // Set this to 0 once you go live or don't require logging.
  13. define("DEBUG", 1);
  14. // Set to 0 once you're ready to go live
  15. define("USE_SANDBOX", 1);
  16. define("LOG_FILE", "./ipn.log");
  17. // Read POST data
  18. // reading posted data directly from $_POST causes serialization
  19. // issues with array data in POST. Reading raw POST data from input stream instead.
  20. $raw_post_data = file_get_contents('php://input');
  21. $raw_post_array = explode('&', $raw_post_data);
  22. $myPost = array();
  23. foreach ($raw_post_array as $keyval) {
  24. $keyval = explode ('=', $keyval);
  25. if (count($keyval) == 2)
  26. $myPost[$keyval[0]] = urldecode($keyval[1]);
  27. }
  28. // read the post from PayPal system and add 'cmd'
  29. $req = 'cmd=_notify-validate';
  30. if(function_exists('get_magic_quotes_gpc')) {
  31. $get_magic_quotes_exists = true;
  32. }
  33. foreach ($myPost as $key => $value) {
  34. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  35. $value = urlencode(stripslashes($value));
  36. } else {
  37. $value = urlencode($value);
  38. }
  39. $req .= "&$key=$value";
  40. }
  41. // Post IPN data back to PayPal to validate the IPN data is genuine
  42. // Without this step anyone can fake IPN data
  43. if(USE_SANDBOX == true) {
  44. $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  45. } else {
  46. $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
  47. }
  48. $ch = curl_init($paypal_url);
  49. if ($ch == FALSE) {
  50. return FALSE;
  51. }
  52. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  58. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  59. if(DEBUG == true) {
  60. curl_setopt($ch, CURLOPT_HEADER, 1);
  61. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  62. }
  63. // CONFIG: Optional proxy configuration
  64. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
  65. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  66. // Set TCP timeout to 30 seconds
  67. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  68. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  69. // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
  70. // of the certificate as shown below. Ensure the file is readable by the webserver.
  71. // This is mandatory for some environments.
  72. //$cert = __DIR__ . "./cacert.pem";
  73. //curl_setopt($ch, CURLOPT_CAINFO, $cert);
  74. $res = curl_exec($ch);
  75. if (curl_errno($ch) != 0) // cURL error
  76. {
  77. if(DEBUG == true) {
  78. error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
  79. }
  80. curl_close($ch);
  81. } else {
  82. // Log the entire HTTP response if debug is switched on.
  83. if(DEBUG == true) {
  84. error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
  85. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  86. }
  87. curl_close($ch);
  88. }
  89. // Inspect IPN validation result and act accordingly
  90. // Split response headers and payload, a better way for strcmp
  91. $tokens = explode("\r\n\r\n", trim($res));
  92. $res = trim(end($tokens));
  93. if (strcmp ($res, "VERIFIED") == 0) {
  94.  
  95. echo "transakcja zweryfikowana i mozna cos zrobic z danymi
  96. <br>
  97. jeśli payment_status = completed to znaczy iz platnosc przebiegla prawidlowo
  98. ";
  99.  
  100. // check whether the payment_status is Completed
  101. // check that txn_id has not been previously processed
  102. // check that receiver_email is your PayPal email
  103. // check that payment_amount/payment_currency are correct
  104. // process payment and mark item as paid.
  105. // assign posted variables to local variables
  106. //$item_name = $_POST['item_name'];
  107. //$item_number = $_POST['item_number'];
  108. //$payment_status = $_POST['payment_status'];
  109. //$payment_amount = $_POST['mc_gross'];
  110. //$payment_currency = $_POST['mc_currency'];
  111. //$txn_id = $_POST['txn_id'];
  112. //$receiver_email = $_POST['receiver_email'];
  113. //$payer_email = $_POST['payer_email'];
  114.  
  115. if(DEBUG == true) {
  116. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  117. }
  118. } else if (strcmp ($res, "INVALID") == 0) {
  119.  
  120. echo "transakcja niezweryfikowana zapewne ktos probuje ciebie oszukac";
  121. // log for manual investigation
  122. // Add business logic here which deals with invalid IPN messages
  123. if(DEBUG == true) {
  124. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  125. }
  126. }
  127. ?>


Ten post edytował irytek 10.01.2015, 22:50:21
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.04.2025 - 07:44