因此,當用戶購買會員資格時,我得到的代碼完美運行。但是,我不確定如何編碼,以便當用戶取消。當用戶通過他們取消時,教皇會向您發送什麼字段?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);
}
}
?>
你爲什麼不使用[IPN模擬器(https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNSimulator/)? –
由於IPN模擬器沒有考慮重複成員資格。我已經用IPN模擬器測試了幾天! @Alon它也沒有考慮取消等。 –
我必須考慮subscr_id嗎?我真的希望有更多關於這個定期付款的文檔@AndrewAngell –