Skip to content
Snippets Groups Projects
Commit cb6bdf97 authored by drastik's avatar drastik
Browse files

webhooks

parent 1d3d4494
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ require_once 'CRM/Core/Config.php';
$config = & CRM_Core_Config::singleton();
//Get the data from stripe
//Get the data from stripe
$data_raw = file_get_contents("php://input");
$data = json_decode($data_raw);
if(! $data) {
......@@ -23,12 +23,12 @@ $stripe_key = CRM_Core_DAO::singleValueQuery("SELECT user_name FROM civicrm_paym
require_once ("packages/stripe-php/lib/Stripe.php");
Stripe::setApiKey($stripe_key);
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
$stripe_event_data = Stripe_Event::retrieve($data->id);
$customer_id = $stripe_event_data->data->object->customer;
switch($stripe_event_data->type) {
//Successful recurring payment
//Successful recurring payment
case 'invoice.payment_succeeded' :
//Get the Stripe charge object
try {
......@@ -38,7 +38,7 @@ switch($stripe_event_data->type) {
break;
}
//Find the recurring contribution in CiviCRM by mapping it from Stripe
//Find the recurring contribution in CiviCRM by mapping it from Stripe
$rel_info_query = CRM_Core_DAO::executeQuery("SELECT invoice_id, end_time FROM civicrm_stripe_subscriptions WHERE customer_id = '$customer_id'");
if(! empty($rel_info_query)) {
$rel_info_query->fetch();
......@@ -65,6 +65,16 @@ switch($stripe_event_data->type) {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
$new_invoice_id = $stripe_event_data->data->object->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
$first_contrib_check = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contribution WHERE invoice_id = '$invoice_id' AND contribution_status_id = '2'");
if(! empty($first_contrib_check)) {
CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET contribution_status_id = '1' WHERE id = '$first_contrib_check'");
return;
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -74,8 +84,8 @@ switch($stripe_event_data->type) {
contribution_recur_id, is_test, contribution_status_id, campaign_id
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', '$recur_contrib_query->campaign_id'
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$new_invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', $recur_contrib_query->campaign_id
)");
if($time_compare > $end_time) {
......@@ -95,7 +105,7 @@ switch($stripe_event_data->type) {
break;
//Failed recurring payment
//Failed recurring payment
case 'invoice.payment_failed' :
//Get the Stripe charge object
try {
......@@ -124,6 +134,9 @@ switch($stripe_event_data->type) {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -134,7 +147,7 @@ switch($stripe_event_data->type) {
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', '$recur_contrib_query->campaign_id'
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', $recur_contrib_query->campaign_id
)");
//Failed charge. Set to status to: Failed
......@@ -146,11 +159,9 @@ switch($stripe_event_data->type) {
break;
//One-time donation and per invoice payment
//One-time donation and per invoice payment
case 'charge.succeeded' :
//Not implemented
break;
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ require_once 'CRM/Core/Page.php';
class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
function run() {
//Get the data from stripe
//Get the data from stripe
$data_raw = file_get_contents("php://input");
$data = json_decode($data_raw);
if(!$data) {
......@@ -19,13 +19,13 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$stripe_key = CRM_Core_DAO::singleValueQuery("SELECT user_name FROM civicrm_payment_processor WHERE payment_processor_type = 'Stripe' AND is_test = '$test_mode'");
require_once ("packages/stripe-php/lib/Stripe.php");
Stripe::setApiKey($stripe_key);
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
$stripe_event_data = Stripe_Event::retrieve($data->id);
$customer_id = $stripe_event_data->data->object->customer;
switch($stripe_event_data->type) {
//Successful recurring payment
//Successful recurring payment
case 'invoice.payment_succeeded':
//Get the Stripe charge object
try {
......@@ -35,7 +35,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
break;
}
//Find the recurring contribution in CiviCRM by mapping it from Stripe
//Find the recurring contribution in CiviCRM by mapping it from Stripe
$rel_info_query = CRM_Core_DAO::executeQuery("SELECT invoice_id, end_time FROM civicrm_stripe_subscriptions WHERE customer_id = '$customer_id'");
if(!empty($rel_info_query)) {
$rel_info_query->fetch();
......@@ -62,6 +62,16 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
$new_invoice_id = $stripe_event_data->data->object->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
$first_contrib_check = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contribution WHERE invoice_id = '$invoice_id' AND contribution_status_id = '2'");
if(!empty($first_contrib_check)) {
CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET contribution_status_id = '1' WHERE id = '$first_contrib_check'");
return;
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -71,8 +81,8 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
contribution_recur_id, is_test, contribution_status_id, campaign_id
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', '$recur_contrib_query->campaign_id'
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$new_invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', $recur_contrib_query->campaign_id
)");
if($time_compare > $end_time) {
......@@ -92,7 +102,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
break;
//Failed recurring payment
//Failed recurring payment
case 'invoice.payment_failed':
//Get the Stripe charge object
try {
......@@ -121,6 +131,9 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -131,7 +144,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', '$recur_contrib_query->campaign_id'
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', $recur_contrib_query->campaign_id
)");
......@@ -145,7 +158,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
break;
//One-time donation and per invoice payment
//One-time donation and per invoice payment
case 'charge.succeeded':
//Not implemented
break;
......@@ -155,4 +168,4 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
parent::run();
}
}
}
......@@ -10,7 +10,7 @@ require_once 'CRM/Core/Config.php';
$config = & CRM_Core_Config::singleton();
//Get the data from stripe
//Get the data from stripe
$data_raw = file_get_contents("php://input");
$data = json_decode($data_raw);
if(! $data) {
......@@ -23,12 +23,12 @@ $stripe_key = CRM_Core_DAO::singleValueQuery("SELECT user_name FROM civicrm_paym
require_once ("packages/stripe-php/lib/Stripe.php");
Stripe::setApiKey($stripe_key);
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
//Retrieve Event from Stripe using ID even though we already have the values now.
//This is for extra security precautions mentioned here: https://stripe.com/docs/webhooks
$stripe_event_data = Stripe_Event::retrieve($data->id);
$customer_id = $stripe_event_data->data->object->customer;
switch($stripe_event_data->type) {
//Successful recurring payment
//Successful recurring payment
case 'invoice.payment_succeeded' :
//Get the Stripe charge object
try {
......@@ -38,7 +38,7 @@ switch($stripe_event_data->type) {
break;
}
//Find the recurring contribution in CiviCRM by mapping it from Stripe
//Find the recurring contribution in CiviCRM by mapping it from Stripe
$rel_info_query = CRM_Core_DAO::executeQuery("SELECT invoice_id, end_time FROM civicrm_stripe_subscriptions WHERE customer_id = '$customer_id'");
if(! empty($rel_info_query)) {
$rel_info_query->fetch();
......@@ -65,6 +65,16 @@ switch($stripe_event_data->type) {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
$new_invoice_id = $stripe_event_data->data->object->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
$first_contrib_check = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contribution WHERE invoice_id = '$invoice_id' AND contribution_status_id = '2'");
if(! empty($first_contrib_check)) {
CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET contribution_status_id = '1' WHERE id = '$first_contrib_check'");
return;
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -74,8 +84,8 @@ switch($stripe_event_data->type) {
contribution_recur_id, is_test, contribution_status_id, campaign_id
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', '$recur_contrib_query->campaign_id'
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$new_invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '1', $recur_contrib_query->campaign_id
)");
if($time_compare > $end_time) {
......@@ -95,7 +105,7 @@ switch($stripe_event_data->type) {
break;
//Failed recurring payment
//Failed recurring payment
case 'invoice.payment_failed' :
//Get the Stripe charge object
try {
......@@ -124,6 +134,9 @@ switch($stripe_event_data->type) {
$fee_amount = $charge->fee / 100;
$net_amount = $total_amount - $fee_amount;
$transaction_id = $charge->id;
if(empty($recur_contrib_query->campaign_id)) {
$recur_contrib_query->campaign_id = 'NULL';
}
//Create this instance of the contribution for accounting in CiviCRM
CRM_Core_DAO::executeQuery("
......@@ -134,7 +147,7 @@ switch($stripe_event_data->type) {
) VALUES (
'$recur_contrib_query->contact_id', '$recur_contrib_query->contribution_type_id', '$recur_contrib_query->payment_instrument_id', '$recieve_date',
'$total_amount', '$fee_amount', '$net_amount', '$transaction_id', '$invoice_id', '$recur_contrib_query->currency',
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', '$recur_contrib_query->campaign_id'
'$recur_contrib_query->id', '$recur_contrib_query->is_test', '4', $recur_contrib_query->campaign_id
)");
//Failed charge. Set to status to: Failed
......@@ -146,11 +159,9 @@ switch($stripe_event_data->type) {
break;
//One-time donation and per invoice payment
//One-time donation and per invoice payment
case 'charge.succeeded' :
//Not implemented
break;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment