2010-05-02 3 views
0

모두. PayPal IPN 통합에 문제가있어 세션 변수를 읽는 솔루션을 얻을 수없는 것으로 보입니다.PayPal IPN - 세션 데이터에 액세스하는 데 문제가 있습니까?

기본적으로 내 상점 모듈 스크립트에서 주문 테이블에 PayPal에서 제공 한 고객의 세부 정보를 저장합니다. 그러나 거래에서 주문한 제품을 주문 ID로 연결된 별도의 테이블에 저장하고자합니다.

그러나 스크립트의 두 번째 부분에서 작동하지 않습니다. 여기서 세션의 제품을 반복하여 orders_products 테이블에 저장합니다.

세션 데이터를 읽지 못하는 이유가 있습니까?

다음과 같이 shop.php 내 코드는 다음과 같습니다

if ($paypal->validate_ipn()) { 
    $name = $paypal->ipn_data['address_name']; 
    $street_1 = $paypal->ipn_data['address_street']; 
    $street_2 = ""; 
    $city = $paypal->ipn_data['address_city']; 
    $state = $paypal->ipn_data['address_state']; 
    $zip = $paypal->ipn_data['address_zip']; 
    $country = $paypal->ipn_data['address_country']; 
    $txn_id = $paypal->ipn_data['txn_id']; 
    $sql = "INSERT INTO orders (name, street_1, street_2, city, state, zip, country, txn_id) 
      VALUES (:name, :street_1, :street_2, :city, :state, :zip, :country, :txn_id)"; 
    $smt = $this->pdo->prepare($sql); 
    $smt->bindParam(':name', $name, PDO::PARAM_STR); 
    $smt->bindParam(':street_1', $street_1, PDO::PARAM_STR); 
    $smt->bindParam(':street_2', $street_2, PDO::PARAM_STR); 
    $smt->bindParam(':city', $city, PDO::PARAM_STR); 
    $smt->bindParam(':state', $state, PDO::PARAM_STR); 
    $smt->bindParam(':zip', $zip, PDO::PARAM_STR); 
    $smt->bindParam(':country', $country, PDO::PARAM_STR); 
    $smt->bindParam(':txn_id', $txn_id, PDO::PARAM_INT); 
    $smt->execute(); 
    // save products to orders relationship 
    $order_id = $this->pdo->lastInsertId(); 
    // $cart = $this->session->get('cart'); 
    $cart = $this->session->get('cart'); 
    foreach ($cart as $product_id => $item) { 
     $quantity = $item['quantity']; 
     $sql = "INSERT INTO orders_products (order_id, product_id, quantity) VALUES ('$order_id', '$product_id', '$quantity')"; 
     $res = $this->pdo->query($sql); 
    } 
    $this->session->del('cart'); 
    mail('[email protected]', 'IPN result', 'IPN was successful on wrestling-wear.com'); 
} else { 
    mail('[email protected]', 'IPN result', 'IPN failed on wrestling-wear.com'); 
} 

를 그리고 여기로 내가 PHP를위한 페이팔 IPN 클래스를 사용하고 있습니다 : http://www.micahcarrick.com/04-19-2005/php-paypal-ipn-integration-class.html을하지만, 다음과 같이 validate_ipn() 방법의 내용은 다음과 같습니다

public function validate_ipn() 
{ 
    $url_parsed = parse_url($this->paypal_url); 
    $post_string = ''; 
    foreach ($_POST as $field => $value) { 
     $this->ipn_data[$field] = $value; 
     $post_string.= $field.'='.urlencode(stripslashes($value)).'&'; 
    } 
    $post_string.= "cmd=_notify-validate"; // append IPN command 
    // open the connection to PayPal 
    $fp = fsockopen($url_parsed[host], "80", $err_num, $err_str, 30); 
    if (!$fp) { 
     // could not open the connection. If logging is on, the error message will be in the log 
     $this->last_error = "fsockopen error no. $errnum: $errstr"; 
     $this->log_ipn_results(false); 
     return false; 
    } else { 
     // post the data back to PayPal 
     fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n"); 
     fputs($fp, "Host: $url_parsed[host]\r\n"); 
     fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
     fputs($fp, "Content-length: ".strlen($post_string)."\r\n"); 
     fputs($fp, "Connection: close\r\n\r\n"); 
     fputs($fp, $post_string . "\r\n\r\n"); 
     // loop through the response from the server and append to variable 
     while (!feof($fp)) { 
      $this->ipn_response.= fgets($fp, 1024); 
     } 
     fclose($fp); // close connection 
    } 
    if (eregi("VERIFIED", $this->ipn_response)) { 
     // valid IPN transaction 
     $this->log_ipn_results(true); 
     return true; 
    } else { 
     // invalid IPN transaction; check the log for details 
     $this->last_error = 'IPN Validation Failed.'; 
     $this->log_ipn_results(false); 
     return false; 
    } 
} 

답변

1

올바르게 회신하면 PayPal IPN이 스크립트에 직접 알림을 보냅니다. 알림은 주문을 한 고객이 아닌 PayPal에서 제공되므로 세션은이 컨텍스트에 존재하지 않습니다. 따라서 모든 장바구니 데이터가 세션에 존재하지 않습니다.

과거에 IPN을 사용했을 때, 나는 그들의 주문에 관한 모든 것을 데이터베이스에 저장하고 TransactionID를 생성했습니다. 이것은 PayPal에 나머지 주문과 함께 발송할 수있는 통과 변수 중 하나이며이를 다시 전달합니다. PayPal에서 IPN을 받으면 TransactionID를 기준으로 주문을 다시 정리하고 따라야하는 업무 규칙을 진행했습니다. 전자 메일을 보내고 암호를 만드십시오.

+0

기본적으로 tmp 파일 대신 데이터베이스에 세션을 저장 하시겠습니까? 건배. –

+0

@ Martin 당신이 원하는 곳에 언제든지 세션을 떠날 수 있다고 생각하지만 PayPal의 응답과 일치시킬 수 있도록 트랜잭션 데이터를 영구적으로 복사해야합니다. session_unset()이 예기치 않게 호출되면 (아마도 쿠키 손실?) 트랜잭션이 반드시 사라진 것은 아닙니다. – cbednarski

0

나는 현재이 문제를 해결하고 있습니다. 당신은 귀하의 거래 테이블에 삽입 세션 user_id를 얻으려고 노력하고 있습니다, 그것은 주문과 함께 페이팔에 user_id를 보내는 것이 가장 좋습니다 수 있습니다. 왜냐하면 그들이 당신이 실제로 주문을 완료했다는 보장이 없다면 페이팔로 리다이렉트되는대로 DB에 쓰는 것이기 때문입니다. 또한 PayPal은 제품/주문 정보를 쉼표로 구분 된 문자열로 되돌려 보내므로 판매 한 많은 고유 항목의 수를 살펴보고 item_id1, item_id2, item_id3 ...을 동적으로 가져 오는 것이 좋습니다. 귀하의 질의는 항상 변하고 있습니다. 내가 틀렸다고 정정 해 주면, 클래스를 사용하지 않고 이것을 구현하려고합니다.