示例#1
0
 public static function set_payment_status($config, $entry, $status, $transaction_type, $transaction_id, $parent_transaction_id, $subscriber_id, $amount, $pending_reason, $reason)
 {
     global $current_user;
     $user_id = 0;
     $user_name = "System";
     if ($current_user && ($user_data = get_userdata($current_user->ID))) {
         $user_id = $current_user->ID;
         $user_name = $user_data->display_name;
     }
     self::log_debug("Payment status: {$status} - Transaction Type: {$transaction_type} - Transaction ID: {$transaction_id} - Parent Transaction: {$parent_transaction_id} - Subscriber ID: {$subscriber_id} - Amount: {$amount} - Pending reason: {$pending_reason} - Reason: {$reason}");
     self::log_debug("Entry: " . print_r($entry, true));
     switch (strtolower($transaction_type)) {
         case "subscr_payment":
             if ($entry["payment_status"] != "Active") {
                 self::log_debug("Starting subscription");
                 if (self::is_valid_initial_payment_amount($config, $entry)) {
                     self::start_subscription($entry, $subscriber_id, $amount, $user_id, $user_name);
                 } else {
                     self::log_debug("Payment amount does not match subscription amount. Subscription will not be activated.");
                     RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment amount (%s) does not match subscription amount. Subscription will not be activated. Transaction Id: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id));
                 }
             } else {
                 self::log_debug("Payment status is already active, so simply adding a Note");
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription payment has been made. Amount: %s. Transaction Id: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id));
             }
             self::log_debug("Inserting payment transaction");
             GFPayPalData::insert_transaction($entry["id"], "payment", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
             break;
         case "subscr_signup":
             $trial_amount = GFCommon::to_number($config["meta"]["trial_amount"]);
             //Starting subscription if there is a free trial period. Otherwise, subscription will be started when payment is received (i.e. sbscr_payment)
             if ($entry["payment_status"] != "Active" && $config["meta"]["trial_period_enabled"] && empty($trial_amount)) {
                 self::log_debug("Starting subscription");
                 self::start_subscription($entry, $subscriber_id, $amount, $user_id, $user_name);
             }
             break;
         case "subscr_cancel":
             if ($entry["payment_status"] != "Cancelled") {
                 $entry["payment_status"] = "Cancelled";
                 self::log_debug("Cancelling subscription");
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription has been cancelled. Subscriber Id: %s", "gravityforms"), $subscriber_id));
                 if ($config["meta"]["update_post_action"] == "draft" && !empty($entry["post_id"])) {
                     $post = get_post($entry["post_id"]);
                     $post->post_status = 'draft';
                     wp_update_post($post);
                     self::log_debug("Marking associated post as a Draft");
                 }
                 if ($config["meta"]["update_post_action"] == "delete" && !empty($entry["post_id"])) {
                     wp_delete_post($entry["post_id"]);
                     self::log_debug("Deleting associated post");
                 }
                 do_action("gform_subscription_canceled", $entry, $config, $transaction_id);
             }
             break;
         case "subscr_eot":
             if ($entry["payment_status"] != "Expired") {
                 $entry["payment_status"] = "Expired";
                 self::log_debug("Setting entry as expired");
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription has expired. Subscriber Id: %s", "gravityforms"), $subscriber_id));
             }
             break;
         case "subscr_failed":
             if ($entry["payment_status"] != "Failed") {
                 $entry["payment_status"] = "Failed";
                 self::log_debug("Marking entry as Failed");
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription signup has failed. Subscriber Id: %s", "gravityforms"), $subscriber_id));
             }
             break;
         default:
             //handles products and donation
             switch (strtolower($status)) {
                 case "completed":
                     self::log_debug("Processing a completed payment");
                     if ($entry["payment_status"] != "Approved") {
                         if (self::is_valid_initial_payment_amount($config, $entry)) {
                             self::log_debug("Entry is not already approved. Proceeding...");
                             $entry["payment_status"] = "Approved";
                             $entry["payment_amount"] = $amount;
                             $entry["payment_date"] = gmdate("y-m-d H:i:s");
                             $entry["transaction_id"] = $transaction_id;
                             $entry["transaction_type"] = 1;
                             //payment
                             if (!$entry["is_fulfilled"]) {
                                 self::log_debug("Payment has been made. Fulfilling order.");
                                 self::fulfill_order($entry, $transaction_id, $amount);
                                 self::log_debug("Order has been fulfilled");
                                 $entry["is_fulfilled"] = true;
                             }
                             self::log_debug("Updating entry.");
                             RGFormsModel::update_lead($entry);
                             self::log_debug("Adding note.");
                             RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been approved. Amount: %s. Transaction Id: %s", "gravityforms"), GFCommon::to_money($entry["payment_amount"], $entry["currency"]), $transaction_id));
                         } else {
                             self::log_debug("Payment amount does not match product price. Entry will not be marked as Approved.");
                             RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment amount (%s) does not match product price. Entry will not be marked as Approved. Transaction Id: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id));
                         }
                     }
                     self::log_debug("Inserting transaction.");
                     GFPayPalData::insert_transaction($entry["id"], "payment", "", $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "reversed":
                     self::log_debug("Processing reversal.");
                     if ($entry["payment_status"] != "Reversed") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Reversed";
                             self::log_debug("Setting entry as Reversed");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been reversed. Transaction Id: %s. Reason: %s", "gravityforms"), $transaction_id, self::get_reason($reason)));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "reversal", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "canceled_reversal":
                     self::log_debug("Processing a reversal cancellation");
                     if ($entry["payment_status"] != "Approved") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Approved";
                             self::log_debug("Setting entry as approved");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment reversal has been canceled and the funds have been transferred to your account. Transaction Id: %s", "gravityforms"), $entry["transaction_id"]));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "reinstated", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "denied":
                     self::log_debug("Processing a Denied request.");
                     if ($entry["payment_status"] != "Denied") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Denied";
                             self::log_debug("Setting entry as Denied.");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been denied. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "denied", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "pending":
                     self::log_debug("Processing a pending transaction.");
                     if ($entry["payment_status"] != "Pending") {
                         if ($entry["transaction_type"] != 2) {
                             $entry["payment_status"] = "Pending";
                             $entry["payment_amount"] = $amount;
                             $entry["transaction_type"] = 1;
                             //payment
                             self::log_debug("Setting entry as Pending.");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment is pending. Amount: %s. Transaction Id: %s. Reason: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id, self::get_pending_reason($pending_reason)));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "pending", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "refunded":
                     self::log_debug("Processing a Refund request.");
                     if ($entry["payment_status"] != "Refunded") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Refunded";
                             self::log_debug("Setting entry as Refunded.");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been refunded. Refunded amount: %s. Transaction Id: %s", "gravityforms"), $amount, $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "refund", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "voided":
                     self::log_debug("Processing a Voided request.");
                     if ($entry["payment_status"] != "Voided") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Voided";
                             self::log_debug("Setting entry as Voided.");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Authorization has been voided. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "void", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "processed":
                     self::log_debug("Processing a 'processed' request.");
                     if ($entry["transaction_type"] != 2) {
                         $entry["payment_status"] = "Pending";
                         self::log_debug("Setting entry as Pending.");
                         RGFormsModel::update_lead($entry);
                         $entry["transaction_type"] = 1;
                         //payment
                     }
                     RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been authorized. You can capture funds from your PayPal control panel. Transaction Id: %s", "gravityforms"), $transaction_id));
                     GFPayPalData::insert_transaction($entry["id"], "processed", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "failed":
                     self::log_debug("Processed a Failed request.");
                     if ($entry["payment_status"] != "Failed") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Failed";
                             self::log_debug("Setting entry as Failed.");
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has Failed. Failed payments occur when they are made via your customer's bank account and could not be completed. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "failed", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
             }
             break;
     }
     self::log_debug("Before gform_post_payment_status.");
     do_action("gform_post_payment_status", $config, $entry, $status, $transaction_id, $subscriber_id, $amount, $pending_reason, $reason);
 }
 public static function set_payment_status($config, $entry, $status, $transaction_type, $transaction_id, $parent_transaction_id, $subscriber_id, $amount, $pending_reason, $reason)
 {
     global $current_user;
     $user_id = 0;
     $user_name = "System";
     if ($current_user && ($user_data = get_userdata($current_user->ID))) {
         $user_id = $current_user->ID;
         $user_name = $user_data->display_name;
     }
     switch (strtolower($transaction_type)) {
         case "subscr_payment":
             if ($entry["payment_status"] != "Active") {
                 self::start_subscription($entry, $subscriber_id, $amount, $user_id, $user_name);
             } else {
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription payment has been made. Amount: %s. Transaction Id: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id));
             }
             GFPayPalData::insert_transaction($entry["id"], "payment", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
             break;
         case "subscr_signup":
             $trial_amount = GFCommon::to_number($config["meta"]["trial_amount"]);
             //Starting subscription if there is a free trial period. Otherwise, subscription will be started when payment is received (i.e. sbscr_payment)
             if ($entry["payment_status"] != "Active" && $config["meta"]["trial_period_enabled"] && empty($trial_amount)) {
                 self::start_subscription($entry, $subscriber_id, $amount, $user_id, $user_name);
             }
             break;
         case "subscr_cancel":
             if ($entry["payment_status"] != "Cancelled") {
                 $entry["payment_status"] = "Cancelled";
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription has been cancelled. Subscriber Id: %s", "gravityforms"), $subscriber_id));
                 if ($config["meta"]["update_post_action"] == "draft" && !empty($entry["post_id"])) {
                     $post = get_post($entry["post_id"]);
                     $post->post_status = 'draft';
                     wp_update_post($post);
                 }
                 if ($config["meta"]["update_post_action"] == "delete" && !empty($entry["post_id"])) {
                     wp_delete_post($entry["post_id"]);
                 }
                 do_action("gform_subscription_canceled", $entry, $config, $transaction_id);
             }
             break;
         case "subscr_eot":
             if ($entry["payment_status"] != "Expired") {
                 $entry["payment_status"] = "Expired";
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription has expired. Subscriber Id: %s", "gravityforms"), $subscriber_id));
             }
             break;
         case "subscr_failed":
             if ($entry["payment_status"] != "Failed") {
                 $entry["payment_status"] = "Failed";
                 RGFormsModel::update_lead($entry);
                 RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Subscription signup has failed. Subscriber Id: %s", "gravityforms"), $subscriber_id));
             }
             break;
         default:
             //handles products and donation
             switch (strtolower($status)) {
                 case "completed":
                     if ($entry["payment_status"] != "Approved") {
                         $entry["payment_status"] = "Approved";
                         $entry["payment_amount"] = $amount;
                         $entry["payment_date"] = gmdate("y-m-d H:i:s");
                         $entry["transaction_id"] = $transaction_id;
                         $entry["transaction_type"] = 1;
                         //payment
                         if (!$entry["is_fulfilled"]) {
                             self::fulfill_order($entry, $transaction_id, $amount);
                             $entry["is_fulfilled"] = true;
                         }
                         RGFormsModel::update_lead($entry);
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been approved. Amount: %s. Transaction Id: %s", "gravityforms"), GFCommon::to_money($entry["payment_amount"], $entry["currency"]), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "payment", "", $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "reversed":
                     if ($entry["payment_status"] != "Reversed") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Reversed";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been reversed. Transaction Id: %s. Reason: %s", "gravityforms"), $transaction_id, self::get_reason($reason)));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "reversal", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "canceled_reversal":
                     if ($entry["payment_status"] != "Approved") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Approved";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment reversal has been canceled and the funds have been transferred to your account. Transaction Id: %s", "gravityforms"), $entry["transaction_id"]));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "reinstated", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "denied":
                     if ($entry["payment_status"] != "Denied") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Denied";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been denied. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "denied", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "pending":
                     if ($entry["payment_status"] != "Pending") {
                         if ($entry["transaction_type"] != 2) {
                             $entry["payment_status"] = "Pending";
                             $entry["payment_amount"] = $amount;
                             $entry["transaction_type"] = 1;
                             //payment
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment is pending. Amount: %s. Transaction Id: %s. Reason: %s", "gravityforms"), GFCommon::to_money($amount, $entry["currency"]), $transaction_id, self::get_pending_reason($pending_reason)));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "pending", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "refunded":
                     if ($entry["payment_status"] != "Refunded") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Refunded";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been refunded. Refunded amount: %s. Transaction Id: %s", "gravityforms"), $amount, $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "refund", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "voided":
                     if ($entry["payment_status"] != "Voided") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Voided";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Authorization has been voided. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "void", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "processed":
                     if ($entry["transaction_type"] != 2) {
                         $entry["payment_status"] = "Pending";
                         RGFormsModel::update_lead($entry);
                         $entry["transaction_type"] = 1;
                         //payment
                     }
                     RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has been authorized. You can capture funds from your PayPal control panel. Transaction Id: %s", "gravityforms"), $transaction_id));
                     GFPayPalData::insert_transaction($entry["id"], "processed", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
                 case "failed":
                     if ($entry["payment_status"] != "Failed") {
                         if ($entry["transaction_type"] == 1) {
                             $entry["payment_status"] = "Failed";
                             RGFormsModel::update_lead($entry);
                         }
                         RGFormsModel::add_note($entry["id"], $user_id, $user_name, sprintf(__("Payment has Failed. Failed payments occur when they are made via your customer's bank account and could not be completed. Transaction Id: %s", "gravityforms"), $transaction_id));
                     }
                     GFPayPalData::insert_transaction($entry["id"], "failed", $subscriber_id, $transaction_id, $parent_transaction_id, $amount);
                     break;
             }
             break;
     }
     do_action("gform_post_payment_status", $config, $entry, $status, $transaction_id, $subscriber_id, $amount, $pending_reason, $reason);
 }