Skip to content

Commit

Permalink
feat: add back payment form
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Aug 15, 2024
1 parent d7905c9 commit 7cdde85
Show file tree
Hide file tree
Showing 17 changed files with 5,718 additions and 493 deletions.
2 changes: 2 additions & 0 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ dependencies {
implementation 'com.google.android.gms:play-services-vision:20.1.3'
implementation 'com.google.android.gms:play-services-maps:18.2.0'
implementation 'com.google.android.gms:play-services-location:21.2.0'
implementation 'com.google.android.gms:play-services-wallet:19.1.0'

implementation "com.google.firebase:firebase-messaging:$fcmVersion"
implementation "com.google.firebase:firebase-crashlytics:$crashlyticsVersion"
// implementation "com.google.android.play:core:$playCoreVersion"

implementation 'com.stripe:stripe-android:2.0.2'
implementation 'com.android.billingclient:billing:6.0.1'

testImplementation "junit:junit:4.13.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@SuppressWarnings("ConstantConditions")
public class BuildVars {

public static final boolean IS_BILLING_UNAVAILABLE = true;
public static final boolean IS_BILLING_UNAVAILABLE = false;
public static boolean DEBUG_VERSION = BuildConfig.BUILD_TYPE.equals("debug");
public static boolean DEBUG_PRIVATE_VERSION = DEBUG_VERSION;
public static boolean LOGS_ENABLED = DEBUG_PRIVATE_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;

//import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.ProductDetails;

import androidx.collection.LongSparseArray;

Expand Down Expand Up @@ -441,8 +441,64 @@ public Integer getPremiumHintAnnualDiscount(boolean checkTransaction) {
if (checkTransaction && (!BillingController.getInstance().isReady() || BillingController.getInstance().getLastPremiumTransaction() == null) || premiumPromo == null) {
return null;
}
// NekoX: Remove BillingClient
return null;

boolean found = false;
int discount = 0;
double currentPrice = 0;
for (TLRPC.TL_premiumSubscriptionOption option : premiumPromo.period_options) {
if (checkTransaction ? option.current && Objects.equals(option.transaction.replaceAll(PremiumPreviewFragment.TRANSACTION_PATTERN, "$1"), BillingController.getInstance().getLastPremiumTransaction()) : option.months == 1) {
found = true;

if (!BuildVars.useInvoiceBilling() && BillingController.PREMIUM_PRODUCT_DETAILS != null) {
ProductDetails.SubscriptionOfferDetails offerDetails = null;
for (ProductDetails.SubscriptionOfferDetails details : BillingController.PREMIUM_PRODUCT_DETAILS.getSubscriptionOfferDetails()) {
String period = details.getPricingPhases().getPricingPhaseList().get(0).getBillingPeriod();
if (option.months == 12 ? period.equals("P1Y") : period.equals(String.format(Locale.ROOT, "P%dM", option.months))) {
offerDetails = details;
break;
}
}

if (offerDetails == null) {
currentPrice = (double) option.amount / option.months;
} else {
currentPrice = (double) offerDetails.getPricingPhases().getPricingPhaseList().get(0).getPriceAmountMicros() / option.months;
}
} else {
currentPrice = (double) option.amount / option.months;
}
}
}
for (TLRPC.TL_premiumSubscriptionOption option : premiumPromo.period_options) {
if (found && option.months == 12) {
double amount;
if (!BuildVars.useInvoiceBilling() && BillingController.PREMIUM_PRODUCT_DETAILS != null) {
ProductDetails.SubscriptionOfferDetails offerDetails = null;
for (ProductDetails.SubscriptionOfferDetails details : BillingController.PREMIUM_PRODUCT_DETAILS.getSubscriptionOfferDetails()) {
String period = details.getPricingPhases().getPricingPhaseList().get(0).getBillingPeriod();
if (option.months == 12 ? period.equals("P1Y") : period.equals(String.format(Locale.ROOT, "P%dM", option.months))) {
offerDetails = details;
break;
}
}

if (offerDetails == null) {
amount = (double) option.amount / option.months;
} else {
amount = (double) offerDetails.getPricingPhases().getPricingPhaseList().get(0).getPriceAmountMicros() / option.months;
}
} else {
amount = (double) option.amount / option.months;
}

discount = (int) ((1.0 - amount / currentPrice) * 100);
}
}
if (!found || discount <= 0) {
return null;
}

return discount;
}

public TLRPC.TL_attachMenuBots getAttachMenuBots() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.telegram.ui.Components.AnimatedEmojiSpan;
import org.telegram.ui.Components.AnimatedFileDrawable;
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.PaymentFormActivity;
import org.telegram.ui.Stars.StarsController;
import org.telegram.ui.Stars.StarsIntroActivity;
import org.telegram.ui.bots.BotWebViewSheet;
Expand Down Expand Up @@ -3257,8 +3258,21 @@ public void sendCallback(final boolean cache, final MessageObject messageObject,
AlertsCreator.showOpenUrlAlert(parentFragment, button.url, false, true);
}
} else if (button instanceof TLRPC.TL_keyboardButtonBuy) {
Toast.makeText(ApplicationLoader.applicationContext, R.string.nekoXPaymentRemovedToast, Toast.LENGTH_SHORT).show();
// NekoX: The payment function has been removed.
if (response instanceof TLRPC.TL_payments_paymentFormStars) {
TLRPC.InputInvoice inputInvoice = ((TLRPC.TL_payments_getPaymentForm) request[0]).invoice;
StarsController.getInstance(currentAccount).openPaymentForm(messageObject, inputInvoice, (TLRPC.TL_payments_paymentFormStars) response, () -> {
waitingForCallback.remove(key);
finalKeys.remove(key);
}, status -> {});
} else if (response instanceof TLRPC.PaymentForm) {
final TLRPC.PaymentForm form = (TLRPC.PaymentForm) response;
getMessagesController().putUsers(form.users, false);
parentFragment.presentFragment(new PaymentFormActivity(form, messageObject, parentFragment));
} else if (response instanceof TLRPC.TL_payments_paymentReceiptStars) {
StarsIntroActivity.showTransactionSheet(LaunchActivity.instance != null ? LaunchActivity.instance : ApplicationLoader.applicationContext, false, currentAccount, (TLRPC.TL_payments_paymentReceiptStars) response, null);
} else if (response instanceof TLRPC.PaymentReceipt) {
parentFragment.presentFragment(new PaymentFormActivity((TLRPC.PaymentReceipt) response));
}
} else {
TLRPC.TL_messages_botCallbackAnswer res = (TLRPC.TL_messages_botCallbackAnswer) response;
if (!cacheFinal && res.cache_time != 0 && !button.requires_password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28940,8 +28940,7 @@ private boolean createMenu(View v, boolean single, boolean listView, float x, fl
if (response instanceof TLRPC.TL_payments_paymentReceiptStars) {
StarsIntroActivity.showTransactionSheet(getContext(), false, currentAccount, (TLRPC.TL_payments_paymentReceiptStars) response, resourceProvider);
} else if (response instanceof TLRPC.PaymentReceipt) {
// NekoX: payment deleted
// presentFragment(new PaymentFormActivity((TLRPC.PaymentReceipt) response));
presentFragment(new PaymentFormActivity((TLRPC.PaymentReceipt) response));
}
}), ConnectionsManager.RequestFlagFailOnServerErrors);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.MessageSendPreview;
import org.telegram.ui.PassportActivity;
import org.telegram.ui.PaymentFormActivity;
import org.telegram.ui.PhotoPickerActivity;
import org.telegram.ui.PhotoPickerSearchActivity;
import org.telegram.ui.PhotoViewer;
Expand Down Expand Up @@ -270,8 +271,42 @@ public void onWebAppSetBackgroundColor(int color) {

@Override
public void onWebAppOpenInvoice(TLRPC.InputInvoice inputInvoice, String slug, TLObject response) {
Toast.makeText(getContext(), LocaleController.getString("nekoXPaymentRemovedToast", R.string.nekoXPaymentRemovedToast), Toast.LENGTH_LONG).show();
// NekoX: The payment function has been removed.
BaseFragment parentFragment = baseFragment;
PaymentFormActivity paymentFormActivity = null;
if (response instanceof TLRPC.TL_payments_paymentFormStars) {
final AlertDialog progressDialog = new AlertDialog(getContext(), AlertDialog.ALERT_TYPE_SPINNER);
progressDialog.showDelayed(150);
StarsController.getInstance(currentAccount).openPaymentForm(null, inputInvoice, (TLRPC.TL_payments_paymentFormStars) response, () -> {
progressDialog.dismiss();
}, status -> {
webViewLayout.getWebViewContainer().onInvoiceStatusUpdate(slug, status);
});
AndroidUtilities.hideKeyboard(webViewLayout);
return;
} else if (response instanceof TLRPC.PaymentForm) {
TLRPC.PaymentForm form = (TLRPC.PaymentForm) response;
MessagesController.getInstance(currentAccount).putUsers(form.users, false);
paymentFormActivity = new PaymentFormActivity(form, slug, parentFragment);
} else if (response instanceof TLRPC.PaymentReceipt) {
paymentFormActivity = new PaymentFormActivity((TLRPC.PaymentReceipt) response);
}

if (paymentFormActivity != null) {
webViewLayout.scrollToTop();

AndroidUtilities.hideKeyboard(webViewLayout);
OverlayActionBarLayoutDialog overlayActionBarLayoutDialog = new OverlayActionBarLayoutDialog(parentFragment.getParentActivity(), resourcesProvider);
overlayActionBarLayoutDialog.show();
paymentFormActivity.setPaymentFormCallback(status -> {
if (status != PaymentFormActivity.InvoiceStatus.PENDING) {
overlayActionBarLayoutDialog.dismiss();
}

webViewLayout.getWebViewContainer().onInvoiceStatusUpdate(slug, status.name().toLowerCase(Locale.ROOT));
});
paymentFormActivity.setResourcesProvider(resourcesProvider);
overlayActionBarLayoutDialog.addFragment(paymentFormActivity);
}
}

@Override
Expand Down
Loading

0 comments on commit 7cdde85

Please sign in to comment.