Skip to content

Commit

Permalink
Stripe payments completed
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficpest committed Jan 12, 2024
1 parent d3dc16e commit 0c96304
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
22 changes: 22 additions & 0 deletions methods/stripe/inc/stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ function delete_stripe_webhook($webhook_id){

return $response;
}

function get_stripe_transaction($trans_id){
global $stripe;

$url = 'https://api.stripe.com/v1/balance_transactions/'
.$trans_id;

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$stripe['PRI_KEY'],
));

$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response , true);

return $response;
}

/*
function show_pp_webhook($webhook_id){
Expand Down
4 changes: 1 addition & 3 deletions methods/stripe/pages/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

$stripe = load_stripe_config($path_to_stripe.'/config/config.php');

$response = list_stripe_webhooks();
$response = get_stripe_transaction('txn_3OXe3FIHV3zDeNmw1FbCgcpG');

print_r($response);

echo '<br><br>';
print_r($response['data'][0]['enabled_events'][0]);
?>
76 changes: 40 additions & 36 deletions methods/stripe/webhooks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,47 @@

date_default_timezone_set( $strikeout['timezone'] );

$webhook_raw = file_get_contents("php://input");
$webhook_data = json_decode($webhook_raw, true);

if ($webhook_data['type'] == 'charge.succeeded'){
error_log($webhook_raw);
// code ran when invoice is updates
$active_plugins = load_config($path_to_stripe.'/config/plugins.php');
$method = 'stripe';
if ($webhook_data['data']['object']['paid'] == 'true')
{$pay_state = 'PAID';} else {$pay_state = 'UNPAID';}

$plugin_payload = array(
'Date' => date('Y-m-d'),
'Reference' => $webhook_data['data']['object']['metadata']['custId'],
'Correlation ID' => $webhook_data['id'],
'Amount' => $webhook_data['data']['object']['metadata']['amount'],
'Fee' => 0,
'Net' => 0,
'Currency' => strtoupper($webhook_data['data']['object']['currency']),
'State' => $pay_state,
'Invoice ID' => 'Need to add this',
'Description' => 'Payment from '
.$webhook_data['data']['object']['billing_details']['email'],
);

foreach($active_plugins as $plugin => $status){
if ($status == 'on'){
include $path_to_root.'/webhooks/'.$plugin.'.php';
}
ini_set('error_log', $path_to_root.'/logs/stripe.log');
//return log to stripe
$webhook_data = json_decode(file_get_contents("php://input"), true);

// Check if payment is made
if ($webhook_data['type'] == 'charge.succeeded'){

// check yourself if transaction is valid with stripe
$tx_data = get_stripe_transaction(
$webhook_data['data']['object']['balance_transaction']);
if ($tx_data['source'] == $webhook_data['data']['object']['id']){

// Looks good proceed to record payment
$active_plugins = load_config($path_to_stripe.'/config/plugins.php');
$method = 'stripe';
if ($webhook_data['data']['object']['paid'] == 'true')
{$pay_state = 'PAID';} else {$pay_state = 'UNPAID';}

$plugin_payload = array(
'Date' => date('Y-m-d'),
'Reference' => $webhook_data['data']['object']['metadata']['custId'],
'Correlation ID' => $webhook_data['data']['object']['id'],
'Amount' => $tx_data['amount']/100,
'Fee' => $tx_data['fee']/100,
'Net' => $tx_data['net']/100,
'Currency' => strtoupper($webhook_data['data']['object']['currency']),
'State' => $pay_state,
'Invoice ID' => $webhook_data['data']['object']['balance_transaction'],
'Description' => 'Payment from '
.$webhook_data['data']['object']['billing_details']['email'],
);

foreach($active_plugins as $plugin => $status){
if ($status == 'on'){
include $path_to_root.'/webhooks/'.$plugin.'.php';
}
ini_set('error_log', $path_to_root.'/logs/stripe.log');
//return log to stripe
}
}else{
error_log('The webhook verification didnt pass. You could be receiving'
.' a false message');
}
/*
}else{
error_log('The webhook signature didnt pass. If your webhook id is set'
.' correctly then you received a false message');
}
*/

?>

0 comments on commit 0c96304

Please sign in to comment.