2015-10-23 164 views
2

因此,當用戶購買會員資格時,我得到的代碼完美運行。但是,我不確定如何編碼,以便當用戶取消。當用戶通過他們取消時,教皇會向您發送什麼字段?Paypal IPN取消會員PHP

我知道我應該使用subscr_cancelled但我不確定取消後的正確僞碼和邏輯。

例如:

  • 什麼是付費狀態當用戶取消?
  • 我只是忽略付款狀態?

這裏是我的代碼迄今,它有點麻煩,因爲我一直在測試了很多:

if (strcmp ($res, "VERIFIED") == 0) { 
    // check whether the payment_status is Completed 
    // check that txn_id has not been previously processed 
    // check that receiver_email is your PayPal email 
    // check that payment_amount/payment_currency are correct 
    // process payment and mark item as paid. 
    // assign posted variables to local variables 

    $item_name = $_POST['item_name']; 
    $item_number = $_POST['item_number']; 
    $txn_type = $_POST['txn_type']; 
    $payment_status = $_POST['payment_status']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
    $id = $_POST['custom']; 
    $make_premium =1; 
    $make_loser=0; 


     if($payment_status=="Completed"){ 
     $user_id_check = $mysqli->query("SELECT `username` FROM `users` WHERE `ID` ='$id'"); 
     if($user_id_check->num_rows > 0){ 
      $txn_id_check = $mysqli->query("SELECT `transaction_id` FROM `payment` WHERE `transaction_id`='$txn_id'"); 
      if($txn_id_check->num_rows != 1) { 
         $query = "INSERT INTO `payment` (`transaction_id`, `payment_status`, `users_id`, `txn_type`) VALUES(?, ?, ?, ?)"; 
         $statement = $mysqli->prepare($query); 
         $statement->bind_param('ssis',$txn_id, $payment_status, $id, $txn_type); 
         if($statement->execute()){ 
          if($txn_type =="subscr_signup"){ 
           $update_user_to_premium =("UPDATE `users` SET `is_member` =? WHERE `ID` =?"); 
           $stmt =$mysqli->prepare($update_user_to_premium); 
           $stmt->bind_param('ii',$make_premium,$id); 
           $stmt->execute(); 
          } 
         }else{ 
         die('Error : ('. $mysqli->errno .') '. $mysqli->error); 
         } 
         $statement->close(); 
       } 
      } 
     } 
    } 
    if($txn_id_check->num_rows == 1){ 
     if($txn_type=="subscr_cancel"){ 
      $update_user_to_loser =("UPDATE `users` SET `is_member` =? WHERE `ID` =?"); 
      $stmt =$mysqli->prepare($update_user_to_loser); 
      $stmt->bind_param('ii',$make_loser,$id); 
      $stmt->execute(); 
     } 
    { 
    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE); 
    } 
} else if (strcmp ($res, "INVALID") == 0) { 
    // log for manual investigation 
    // Add business logic here which deals with invalid IPN messages 
    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE); 
    } 
} 

我應該使用這樣的事情

switch ($_POST['txn_type']) { 
case 'subscr_signup': 
     break; 

    case 'subscr_eot': 
     //subscription end of term 
     break; 

    case 'subscr_cancel': 
     //subscription canceled 
     break; 

編輯:

if (strcmp ($res, "VERIFIED") == 0) { 
    // check whether the payment_status is Completed 
    // check that txn_id has not been previously processed 
    // check that receiver_email is your PayPal email 
    // check that payment_amount/payment_currency are correct 
    // process payment and mark item as paid. 
    // assign posted variables to local variables 

    $item_name = $_POST['item_name']; 
    $item_number = $_POST['item_number']; 
    $txn_type = $_POST['txn_type']; 
    $payment_status = $_POST['payment_status']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
    $id = $_POST['custom']; 
    $subscr_id = $_POST['subscr_id']; 
    $make_premium =1; 
    $make_loser=0; 


     if($payment_status=="Completed"){ 
     $user_id_check = $mysqli->query("SELECT `username` FROM `users` WHERE `ID` ='$id'"); 
     if($user_id_check->num_rows > 0){ 
      $txn_id_check = $mysqli->query("SELECT `transaction_id` FROM `payment` WHERE `transaction_id`='$txn_id'"); 
      if($txn_id_check->num_rows != 1) { 
         $query = "INSERT INTO `payment` (`transaction_id`, `payment_status`, `users_id`, `txn_type`, `subscr_id`) VALUES(?, ?, ?, ?, ?)"; 
         $statement = $mysqli->prepare($query); 
         $statement->bind_param('ssiss',$txn_id, $payment_status, $id, $txn_type, $subscr_id); 
         if($statement->execute()){ 
          if($txn_type =="subscr_signup"){ 
           $update_user_to_premium =("UPDATE `users` SET `is_member` =? WHERE `ID` =?"); 
           $stmt =$mysqli->prepare($update_user_to_premium); 
           $stmt->bind_param('ii',$make_premium,$id); 
           $stmt->execute(); 
          } 
         }else{ 
         die('Error : ('. $mysqli->errno .') '. $mysqli->error); 
         } 
         $statement->close(); 
       } 
      } 
     } 
    } 

     if($txn_type=="subscr_cancel"){ 
      // get user id from row with subscription id 
      $get_user_id = $mysqli->("SELECT `users_id` FROM `payment` WHERE `subscr_id`='$subscr_id'"); 
      $row = $get_user_id->fetch_assoc(); 
      $users_id = $row[`users_id`]; 
      $update_user_to_loser =("UPDATE `users` SET `is_member` =? WHERE `ID` =?"); 
      $stmt =$mysqli->prepare($update_user_to_loser); 
      $stmt->bind_param('ii',$make_loser,$users_id); 
      $stmt->execute(); 
     } 
    { 
    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE); 
    } 
} else if (strcmp ($res, "INVALID") == 0) { 
    // log for manual investigation 
    // Add business logic here which deals with invalid IPN messages 
    if(DEBUG == true) { 
     error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE); 
    } 
} 
?> 
+0

你爲什麼不使用[IPN模擬器(https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNSimulator/)? –

+0

由於IPN模擬器沒有考慮重複成員資格。我已經用IPN模擬器測試了幾天! @Alon它也沒有考慮取消等。 –

+0

我必須考慮subscr_id嗎?我真的希望有更多關於這個定期付款的文檔@AndrewAngell –

回答

1

如果profil e被取消,則不會發生付款。這是一種不同類型的交易。沒有付款,所以沒有付款狀態,所以你不會在那種IPN中獲得該參數。

您可以在沙盒中輕鬆測試此功能。您只需創建一個賣家帳戶和一個買家帳戶,並在您的賣家帳戶中配置IPN來打擊您的聽衆。然後從賣家帳戶創建訂閱按鈕,從買家帳戶註冊,然後取消它。這將觸發每個步驟的IPN,以便您可以看到期望的結果並相應地調整IPN偵聽器。

以下是subscr_cancel事務的IPN示例。

Array 
(
    [amount3] => 4.99 
    [address_status] => confirmed 
    [recur_times] => 2 
    [subscr_date] => 15:51:33 Jan 19, 2015 PST 
    [payer_id] => YW66KXBKJRRES 
    [address_street] => 123 Test Ave 
    [mc_amount3] => 4.99 
    [charset] => windows-1252 
    [address_zip] => 64030 
    [first_name] => Tester 
    [reattempt] => 1 
    [address_country_code] => US 
    [address_name] => Testers, LLC 
    [notify_version] => 3.8 
    [subscr_id] => S-6YS75493RP123324L 
    [payer_status] => verified 
    [business] => [email protected] 
    [address_country] => United States 
    [address_city] => Grandview 
    [verify_sign] => AI7Wj0s667jdpY.UclGpjUpSpsZNAJzn.UcYbCVhXVG5fO05tyBo36EL 
    [payer_email] => [email protected] 
    [payer_business_name] => Testers, LLC 
    [last_name] => Testerson 
    [address_state] => MO 
    [receiver_email] => [email protected] 
    [recurring] => 1 
    [txn_type] => subscr_cancel 
    [item_name] => Test Subscription 
    [mc_currency] => USD 
    [item_number] => TEST-123 
    [residence_country] => US 
    [test_ipn] => 1 
    [period3] => 1 M 
    [ipn_track_id] => f0f2f2be88fd 
) 
+0

所以我把它擺出來的方式是正確的?通過不將subscr_cancelled放入payment_status if語句中?另外,我應該在用戶取消其會員資格時存儲信息,還是應該將其更新爲非會員?另外當然,我需要考慮subscr_id爲了更新用戶非保費? –

+0

txn_type的值不會被subscr_cancelled,它會是subscr_cancel –

+0

檢查我編輯的代碼 –