Skip to content

Commit

Permalink
removed useless conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Apr 4, 2024
1 parent 5116499 commit 10a4c11
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 318 deletions.
316 changes: 157 additions & 159 deletions classes/lite/class.pmpro_register_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,187 +22,185 @@ class PMPro_Register_Hooks_Lite
*/
public function __construct()
{
if (class_exists(Loader::class)) {
Helpers::registerIntegration('pmpro');

if (is_admin()) {
new TransactionPage(
esc_html__('PMPro transactions', 'pmpro-cryptopay'),
'pmpro',
9,
[
'orderId' => function ($tx) {
if (!isset($tx->orderId)) {
return esc_html__('Not found', 'pmpro-cryptopay');
}

return '<a href="' . admin_url('admin.php?page=pmpro-orders&order=' . $tx->orderId) . '">' . $tx->orderId . '</a>';
Helpers::registerIntegration('pmpro');

if (is_admin()) {
new TransactionPage(
esc_html__('PMPro transactions', 'pmpro-cryptopay'),
'pmpro',
9,
[
'orderId' => function ($tx) {
if (!isset($tx->orderId)) {
return esc_html__('Not found', 'pmpro-cryptopay');
}
]
);
}

Hook::addFilter('init_pmpro', function (PaymentDataType $data) {
global $pmpro_levels;
return '<a href="' . admin_url('admin.php?page=pmpro-orders&order=' . $tx->orderId) . '">' . $tx->orderId . '</a>';
}
]
);
}

if (!isset($pmpro_levels[$data->getParams()->get('levelId')])) {
Response::error(esc_html__('The relevant level was not found!', 'pmpro-cryptopay'), 'LEVEL_NOT_FOUND');
}
Hook::addFilter('init_pmpro', function (PaymentDataType $data) {
global $pmpro_levels;

return $data;
});
if (!isset($pmpro_levels[$data->getParams()->get('levelId')])) {
Response::error(esc_html__('The relevant level was not found!', 'pmpro-cryptopay'), 'LEVEL_NOT_FOUND');
}

Hook::addFilter('before_payment_started_pmpro', function (PaymentDataType $data): PaymentDataType {
global $pmpro_levels;
$currentUser = wp_get_current_user();
return $data;
});

$order = new \MemberOrder();
$level = $pmpro_levels[$data->getParams()->get('levelId')];
pmpro_cryptopay_check_discount_code($level, $data->getParams()->get('discountCode'));
Hook::addFilter('before_payment_started_pmpro', function (PaymentDataType $data): PaymentDataType {
global $pmpro_levels;
$currentUser = wp_get_current_user();

if (empty($order->code)) {
$order->code = $order->getRandomCode();
}
$order = new \MemberOrder();
$level = $pmpro_levels[$data->getParams()->get('levelId')];
pmpro_cryptopay_check_discount_code($level, $data->getParams()->get('discountCode'));

// Set order values.
$order->membership_id = $level->id;
$order->membership_name = $level->name;
$order->InitialPayment = pmpro_round_price($level->initial_payment);
$order->PaymentAmount = pmpro_round_price($level->billing_amount);
$order->ProfileStartDate = date_i18n("Y-m-d\TH:i:s", current_time("timestamp"));
$order->BillingPeriod = $level->cycle_period;
$order->BillingFrequency = $level->cycle_number;
if ($level->billing_limit) {
$order->TotalBillingCycles = $level->billing_limit;
}

// Set user info.
$order->FirstName = get_user_meta($currentUser->ID, 'first_name', true);
$order->LastName = get_user_meta($currentUser->ID, 'last_name', true);
$order->Email = $currentUser->user_email;
$order->Address1 = "";
$order->Address2 = "";

// Set other values.
$order->billing = new \stdClass();
$order->billing->name = $order->FirstName . " " . $order->LastName;
$order->billing->street = trim($order->Address1 . " " . $order->Address2);
$order->billing->city = "";
$order->billing->state = "";
$order->billing->country = "";
$order->billing->zip = "";
$order->billing->phone = "";

$order->gateway = 'cryptopay_lite';
$order->payment_type = 'CryptoPay Lite';
$order->setGateway();

// Set up level var.
$order->getMembershipLevelAtCheckout();

// Set tax.
$initialTax = $order->getTaxForPrice($order->InitialPayment);
$recurringTax = $order->getTaxForPrice($order->PaymentAmount);

// Set amounts.
$order->initial_amount = pmpro_round_price((float) $order->InitialPayment + (float) $initialTax);
$order->subscription_amount = pmpro_round_price((float) $order->PaymentAmount + (float) $recurringTax);

//just save, the user will go to PayPal to pay
$order->status = "review";
$order->membership_id = $level->id;
$order->user_id = $data->getUserId();
$order->payment_transaction_id = "PMPRO_CRYPTOPAY_LITE_" . $order->code;
$order->saveOrder();

$data->getOrder()->setId(intval($order->id));
$data->getDynamicData()->set('orderId', intval($order->id));
if (empty($order->code)) {
$order->code = $order->getRandomCode();
}

return $data;
});
// Set order values.
$order->membership_id = $level->id;
$order->membership_name = $level->name;
$order->InitialPayment = pmpro_round_price($level->initial_payment);
$order->PaymentAmount = pmpro_round_price($level->billing_amount);
$order->ProfileStartDate = date_i18n("Y-m-d\TH:i:s", current_time("timestamp"));
$order->BillingPeriod = $level->cycle_period;
$order->BillingFrequency = $level->cycle_number;
if ($level->billing_limit) {
$order->TotalBillingCycles = $level->billing_limit;
}

Hook::addFilter('before_payment_finished_pmpro', function (PaymentDataType $data): PaymentDataType {
$data->getOrder()->setId(intval($data->getDynamicData()->get('orderId')));
return $data;
});
// Set user info.
$order->FirstName = get_user_meta($currentUser->ID, 'first_name', true);
$order->LastName = get_user_meta($currentUser->ID, 'last_name', true);
$order->Email = $currentUser->user_email;
$order->Address1 = "";
$order->Address2 = "";

// Set other values.
$order->billing = new \stdClass();
$order->billing->name = $order->FirstName . " " . $order->LastName;
$order->billing->street = trim($order->Address1 . " " . $order->Address2);
$order->billing->city = "";
$order->billing->state = "";
$order->billing->country = "";
$order->billing->zip = "";
$order->billing->phone = "";

$order->gateway = 'cryptopay_lite';
$order->payment_type = 'CryptoPay Lite';
$order->setGateway();

// Set up level var.
$order->getMembershipLevelAtCheckout();

// Set tax.
$initialTax = $order->getTaxForPrice($order->InitialPayment);
$recurringTax = $order->getTaxForPrice($order->PaymentAmount);

// Set amounts.
$order->initial_amount = pmpro_round_price((float) $order->InitialPayment + (float) $initialTax);
$order->subscription_amount = pmpro_round_price((float) $order->PaymentAmount + (float) $recurringTax);

//just save, the user will go to PayPal to pay
$order->status = "review";
$order->membership_id = $level->id;
$order->user_id = $data->getUserId();
$order->payment_transaction_id = "PMPRO_CRYPTOPAY_LITE_" . $order->code;
$order->saveOrder();

$data->getOrder()->setId(intval($order->id));
$data->getDynamicData()->set('orderId', intval($order->id));

return $data;
});

Hook::addFilter('before_payment_finished_pmpro', function (PaymentDataType $data): PaymentDataType {
$data->getOrder()->setId(intval($data->getDynamicData()->get('orderId')));
return $data;
});

Hook::addAction('payment_finished_pmpro', function (PaymentDataType $data): void {
global $pmpro_levels, $wpdb;

$orderId = $data->getDynamicData()->get('orderId');
$order = new \MemberOrder($orderId);
if (!$order->id) {
return;
}

Hook::addAction('payment_finished_pmpro', function (PaymentDataType $data): void {
global $pmpro_levels, $wpdb;
if (!$data->getStatus()) {
$order->status = "error";
$order->saveOrder();
return;
}

$orderId = $data->getDynamicData()->get('orderId');
$order = new \MemberOrder($orderId);
if (!$order->id) {
return;
}
$level = $pmpro_levels[$data->getParams()->get('levelId')];
pmpro_cryptopay_check_discount_code($level, $data->getParams()->get('discountCode'));

if (!$data->getStatus()) {
$order->status = "error";
$order->saveOrder();
return;
$startdate = current_time("mysql");
if (!empty($level->expiration_number)) {
if ('Hour' == $level->expiration_period) {
$enddate = date("Y-m-d H:i:s", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp")));
} else {
$enddate = date("Y-m-d 23:59:59", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp")));
}
} else {
$enddate = "NULL";
}

$level = $pmpro_levels[$data->getParams()->get('levelId')];
pmpro_cryptopay_check_discount_code($level, $data->getParams()->get('discountCode'));
$discountCodeId = "";
if ($data->getParams()->get('discountCode')) {
$discountCode = $data->getParams()->get('discountCode');
$codeCheck = pmpro_checkDiscountCode($discountCode, $level->id, true);

$startdate = current_time("mysql");
if (!empty($level->expiration_number)) {
if ('Hour' == $level->expiration_period) {
$enddate = date("Y-m-d H:i:s", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp")));
} else {
$enddate = date("Y-m-d 23:59:59", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp")));
}
if (false == $codeCheck[0]) {
$useDiscountCode = false;
} else {
$enddate = "NULL";
$useDiscountCode = true;
}

$discountCodeId = "";
if ($data->getParams()->get('discountCode')) {
$discountCode = $data->getParams()->get('discountCode');
$codeCheck = pmpro_checkDiscountCode($discountCode, $level->id, true);
// update membership_user table.
if (!empty($discountCode) && !empty($useDiscountCode)) {
$discountCodeId = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($discountCode) . "' LIMIT 1");

if (false == $codeCheck[0]) {
$useDiscountCode = false;
} else {
$useDiscountCode = true;
}

// update membership_user table.
if (!empty($discountCode) && !empty($useDiscountCode)) {
$discountCodeId = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($discountCode) . "' LIMIT 1");

$wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->getUserId() . "', '" . intval($orderId) . "', '" . current_time("mysql") . "')");
}
$wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->getUserId() . "', '" . intval($orderId) . "', '" . current_time("mysql") . "')");
}
}

$userLevel = [
'enddate' => $enddate,
'startdate' => $startdate,
'membership_id' => $level->id,
'code_id' => $discountCodeId,
'user_id' => $data->getUserId(),
'trial_limit' => $level->trial_limit,
'cycle_number' => $level->cycle_number,
'cycle_period' => $level->cycle_period,
'billing_limit' => $level->billing_limit,
'trial_amount' => pmpro_round_price($level->trial_amount),
'billing_amount' => pmpro_round_price($level->billing_amount),
'initial_payment' => pmpro_round_price($level->initial_payment),
];

pmpro_changeMembershipLevel($userLevel, $data->getUserId(), 'changed');

$order->status = "success";
$order->saveOrder();
});

Hook::addFilter('payment_redirect_urls_pmpro', function (PaymentDataType $data) {
return [
'failed' => pmpro_url("account"),
'reminderEmail' => pmpro_url("account"),
'success' => pmpro_url("confirmation", "?level=" . $data->getParams()->get('levelId'))
];
});
}
$userLevel = [
'enddate' => $enddate,
'startdate' => $startdate,
'membership_id' => $level->id,
'code_id' => $discountCodeId,
'user_id' => $data->getUserId(),
'trial_limit' => $level->trial_limit,
'cycle_number' => $level->cycle_number,
'cycle_period' => $level->cycle_period,
'billing_limit' => $level->billing_limit,
'trial_amount' => pmpro_round_price($level->trial_amount),
'billing_amount' => pmpro_round_price($level->billing_amount),
'initial_payment' => pmpro_round_price($level->initial_payment),
];

pmpro_changeMembershipLevel($userLevel, $data->getUserId(), 'changed');

$order->status = "success";
$order->saveOrder();
});

Hook::addFilter('payment_redirect_urls_pmpro', function (PaymentDataType $data) {
return [
'failed' => pmpro_url("account"),
'reminderEmail' => pmpro_url("account"),
'success' => pmpro_url("confirmation", "?level=" . $data->getParams()->get('levelId'))
];
});
}
}

Expand Down
Loading

0 comments on commit 10a4c11

Please sign in to comment.