733 lines
14 KiB
PHP
733 lines
14 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
include 'header.php';
|
||
|
|
|
||
|
|
include_once "./include/class_semods_paypal.php";
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$gwdata = semods::db_query_count( "SELECT paymentgateway_metadata FROM se_semods_paymentgateways WHERE paymentgateway_typename = 'paypal'" );
|
||
|
|
|
||
|
|
$gwdata = unserialize( $gwdata );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$paypal = new Paypal( array( 'business' => $gwdata['business'],
|
||
|
|
|
||
|
|
'cert_id' => $gwdata['cert_id'],
|
||
|
|
|
||
|
|
'identity_token' => $gwdata['identity_token'],
|
||
|
|
|
||
|
|
'currency_code' => semods::get_setting('payment_currency', 'USD' ),
|
||
|
|
|
||
|
|
'ssl_myprivkey_file' => $gwdata['ssl_myprivkey_file'],
|
||
|
|
|
||
|
|
'ssl_mypubcert_file' => $gwdata['ssl_mypubcert_file'],
|
||
|
|
|
||
|
|
'ssl_paypalcert_file' => $gwdata['ssl_paypalcert_file'],
|
||
|
|
|
||
|
|
'sandbox' => $gwdata['sandbox']
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
)
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// map paypal_states <-> our states
|
||
|
|
|
||
|
|
$paypal_tx_state_map = array( 'completed' => 0,
|
||
|
|
|
||
|
|
'canceled-reversal' => 3,
|
||
|
|
|
||
|
|
'denied' => 4,
|
||
|
|
|
||
|
|
'expired' => 4,
|
||
|
|
|
||
|
|
'failed' => 4,
|
||
|
|
|
||
|
|
'in-progress' => 1,
|
||
|
|
|
||
|
|
'partially-refunded' => 2,
|
||
|
|
|
||
|
|
'pending' => 1,
|
||
|
|
|
||
|
|
'processed' => 0,
|
||
|
|
|
||
|
|
'refunded' => 2,
|
||
|
|
|
||
|
|
'reversed' => 3,
|
||
|
|
|
||
|
|
'voided' => 4
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$action = semods::getpost('action');
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
switch( $action ) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
case 'ipn':
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// TBD: log requests
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
|
||
|
|
* txn_type
|
||
|
|
|
||
|
|
* subscr_signup: subscription sign-up.
|
||
|
|
|
||
|
|
* subscr_cancel: subscription cancellation.
|
||
|
|
|
||
|
|
* subscr_failed: subscription payment failure.
|
||
|
|
|
||
|
|
* subscr_payment: subscription payment.
|
||
|
|
|
||
|
|
* subscr_eot: subscription's end-of-term.
|
||
|
|
|
||
|
|
* subscr_modify: subscription modification.
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
* Pending => Complete -> same transaction
|
||
|
|
|
||
|
|
* Reversed -> new transaction, previous transaction -> "Canceled_Revers"
|
||
|
|
|
||
|
|
* Refund -> new transaction
|
||
|
|
|
||
|
|
* Denied -> same transaction
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// TBD: refunds for recurring_payment arrive without txn_type !!!
|
||
|
|
|
||
|
|
if( $paypal->process_ipn() ) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
switch($paypal->tx_data['txn_type']) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// just payment
|
||
|
|
|
||
|
|
case "payment":
|
||
|
|
|
||
|
|
case "cart":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// express checkout cart
|
||
|
|
|
||
|
|
case "express_checkout":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$cart_id = $paypal->tx_data['custom'];
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$cart = new semods_cart( $cart_id );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// WTF? - somebody made manual payment? etc
|
||
|
|
|
||
|
|
if($cart->cart_exists != false) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if($paypal->tx_status() == "Completed") {
|
||
|
|
|
||
|
|
$cart->on_cart_paid();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$payment_state = semods::g($paypal_tx_state_map, strtolower($paypal->tx_status()), 5);
|
||
|
|
|
||
|
|
$payment_type = 0; // Sale / Cart
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// TBD: paypal date is in PDT -> convert to GMT?
|
||
|
|
|
||
|
|
$payment_date = strtotime( $paypal->tx_data['payment_date'] );
|
||
|
|
|
||
|
|
$payment_text = semods::get_language_text( 100015012 );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// $payment_amount = sprintf( "%.2F", $paypal->tx_data['payment_gross'] );
|
||
|
|
|
||
|
|
$payment_amount = sprintf( "%.2F", $paypal->tx_data['mc_gross'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
semods_payment_invoice( $cart->cart_info['cart_user_id'],
|
||
|
|
|
||
|
|
$payment_date,
|
||
|
|
|
||
|
|
$payment_type,
|
||
|
|
|
||
|
|
$payment_state,
|
||
|
|
|
||
|
|
$payment_text,
|
||
|
|
|
||
|
|
$payment_amount,
|
||
|
|
|
||
|
|
$paypal->tx_data['txn_id'],
|
||
|
|
|
||
|
|
1, // handler - paypal
|
||
|
|
|
||
|
|
$paypal->tx_data['first_name'],
|
||
|
|
|
||
|
|
$paypal->tx_data['last_name'],
|
||
|
|
|
||
|
|
$paypal->tx_data['payer_email'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_street'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_city'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_state'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_zip'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_country']
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
// error
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Transaction -> cart_id (as invoice / details)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// IPN after creating recurring_payment_profile via API (not subscription)
|
||
|
|
|
||
|
|
case "recurring_payment_profile_created":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// only to invoice if there were initial amount.
|
||
|
|
|
||
|
|
if($paypal->tx_data['initial_payment_status'] == "Completed") {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// find out user_id for this profile
|
||
|
|
|
||
|
|
$profile_id = $paypal->tx_data['recurring_payment_id'];
|
||
|
|
|
||
|
|
$profile_reference = $paypal->tx_data['rp_invoice_id'];
|
||
|
|
|
||
|
|
$userplan = semods_userplan::create_from_profile_reference( $profile_reference );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// try from profile id
|
||
|
|
|
||
|
|
if(!$userplan->userplan_exists) {
|
||
|
|
|
||
|
|
$userplan = new semods_userplan( null, null, null, null, $profile_id );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if($userplan->userplan_exists) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// TBD: paypal date is in PDT -> convert to GMT?
|
||
|
|
|
||
|
|
$payment_type = 2; // initial subscription / recurring payment
|
||
|
|
|
||
|
|
$payment_state = 0; // completed
|
||
|
|
|
||
|
|
$payment_date = strtotime( $paypal->tx_data['time_created'] ); // use profile creation as time for initial payment
|
||
|
|
|
||
|
|
$payment_text = $functions_subscriber[6] . ' ' . $userplan->userplan_info['plan_name'];
|
||
|
|
|
||
|
|
$payment_amount = sprintf( "%.2f", $paypal->tx_data['initial_payment_amount'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
semods_payment_invoice( $userplan->userplan_info['userplan_user_id'],
|
||
|
|
|
||
|
|
$payment_date,
|
||
|
|
|
||
|
|
$payment_type,
|
||
|
|
|
||
|
|
$payment_state,
|
||
|
|
|
||
|
|
$payment_text,
|
||
|
|
|
||
|
|
$payment_amount,
|
||
|
|
|
||
|
|
$paypal->tx_data['initial_payment_txn_id'],
|
||
|
|
|
||
|
|
1, // handler - paypal
|
||
|
|
|
||
|
|
$paypal->tx_data['first_name'],
|
||
|
|
|
||
|
|
$paypal->tx_data['last_name'],
|
||
|
|
|
||
|
|
$paypal->tx_data['payer_email'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_street'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_city'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_state'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_zip'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_country']
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
subscriber_log_error( "[Paypal] Received recurring_payment_profile_created with profile id '$profile_id' and reference id '$profile_reference' but no associated userplan" );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// IPN after for recurring payment
|
||
|
|
|
||
|
|
case "recurring_payment":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
|
||
|
|
* Actions:
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
* need userid
|
||
|
|
|
||
|
|
* need planid
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
* create plan if none
|
||
|
|
|
||
|
|
* extend plan if already active
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
* when upgrading -
|
||
|
|
|
||
|
|
* cancel current plan (if active)
|
||
|
|
|
||
|
|
* pay via EC -
|
||
|
|
|
||
|
|
* pro-rate
|
||
|
|
|
||
|
|
* recurring payment - create new profile
|
||
|
|
|
||
|
|
*
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if($paypal->tx_status() == "Completed") {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$profile_id = $paypal->tx_data['recurring_payment_id'];
|
||
|
|
|
||
|
|
$profile_reference = $paypal->tx_data['rp_invoice_id'];
|
||
|
|
|
||
|
|
$userplan = semods_userplan::create_from_profile_reference( $profile_reference );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// try from profile id
|
||
|
|
|
||
|
|
if(!$userplan->userplan_exists) {
|
||
|
|
|
||
|
|
$userplan = new semods_userplan( null, null, null, null, $profile_id );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if($userplan->userplan_exists) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if(trim($paypal->tx_data['period_type']) == 'Trial' ) {
|
||
|
|
|
||
|
|
$userplan->extend_plan( 1 );
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
$userplan->extend_plan( 0 );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// TBD: paypal date is in PDT -> convert to GMT?
|
||
|
|
|
||
|
|
$payment_type = 1; // recurring payment
|
||
|
|
|
||
|
|
$payment_state = 0; // completed
|
||
|
|
|
||
|
|
$payment_date = strtotime( $paypal->tx_data['payment_date'] );
|
||
|
|
|
||
|
|
$payment_text = $functions_subscriber[5] . ' ' . $userplan->userplan_info['plan_name'];
|
||
|
|
|
||
|
|
// $payment_amount = sprintf( "%.2f", $paypal->tx_data['payment_gross'] );
|
||
|
|
|
||
|
|
$payment_amount = sprintf( "%.2f", $paypal->tx_data['mc_gross'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
semods_payment_invoice( $userplan->userplan_info['userplan_user_id'],
|
||
|
|
|
||
|
|
$payment_date,
|
||
|
|
|
||
|
|
$payment_type,
|
||
|
|
|
||
|
|
$payment_state,
|
||
|
|
|
||
|
|
$payment_text,
|
||
|
|
|
||
|
|
$payment_amount,
|
||
|
|
|
||
|
|
$paypal->tx_data['txn_id'],
|
||
|
|
|
||
|
|
1, // handler - paypal
|
||
|
|
|
||
|
|
$paypal->tx_data['first_name'], //$client_firstname,
|
||
|
|
|
||
|
|
$paypal->tx_data['last_name'],
|
||
|
|
|
||
|
|
$paypal->tx_data['payer_email'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_street'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_city'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_state'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_zip'],
|
||
|
|
|
||
|
|
$paypal->tx_data['address_country']
|
||
|
|
|
||
|
|
);
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
subscriber_log_error( "[Paypal] Received recurring_payment with profile id '$profile_id' and reference id '$profile_reference' but no associated userplan" );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
case 'recurring_payment_profile_cancel':
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$profile_id = $paypal->tx_data['recurring_payment_id'];
|
||
|
|
|
||
|
|
$profile_reference = $paypal->tx_data['rp_invoice_id'];
|
||
|
|
|
||
|
|
$userplan = semods_userplan::create_from_profile_reference( $profile_reference );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// try from profile id
|
||
|
|
|
||
|
|
if(!$userplan->userplan_exists) {
|
||
|
|
|
||
|
|
$userplan = new semods_userplan( null, null, null, null, $profile_id );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if($userplan->userplan_exists) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$userplan->cancel_plan( 0, 0, 1 );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
subscriber_log_error( "[Paypal] Received recurring_payment_profile_cancel with profile id '$profile_id' and reference id '$profile_reference' but no associated userplan" );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Payment for subscription
|
||
|
|
|
||
|
|
case "subscr_payment":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Start of subscription period
|
||
|
|
|
||
|
|
case "subscr_signup":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$plan->plan_subscribe( $user_id, $paypal->tx_data['subscr_id'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Cancel subscription
|
||
|
|
|
||
|
|
case "subscr_cancel":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$plan->plan_cancel( $user_id, $paypal->tx_data['subscr_id'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Failure of payment / subscription
|
||
|
|
|
||
|
|
case "subscr_failed":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$plan->plan_suspend( $user_id, $paypal->tx_data['subscr_id'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// End of subscription - expiration
|
||
|
|
|
||
|
|
case "subscr_eot":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$plan->plan_end( $user_id, $paypal->tx_data['subscr_id'] );
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Subscription details modified
|
||
|
|
|
||
|
|
case "subscr_modify":
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
} else {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if(function_exists('subscriber_log_error')) {
|
||
|
|
|
||
|
|
subscriber_log_error( "[Paypal] IPN Verification FAILED - " . $paypal->error_message );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// log failed process
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
case 'success':
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
header( "Location: home.php" );
|
||
|
|
|
||
|
|
exit();
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
case 'cancel':
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
header( "Location: home.php" );
|
||
|
|
|
||
|
|
exit();
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
default;
|
||
|
|
|
||
|
|
header("Location: home.php");
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
?>
|