Skip to content

Commit

Permalink
Use the same way to get the configuration through the bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadalkady committed Mar 13, 2024
1 parent e49708f commit 02b5469
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.payment.paymentsdk.sharedclasses.model.response.TransactionResponseBody;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
Expand Down Expand Up @@ -356,13 +357,33 @@ private PaymentSdkConfigurationDetails getPaymentSdkConfigurationDetails(JSONObj
if (shippingDetails != null) {
shippingData = new PaymentSdkShippingDetails(shippingDetails.optString("pt_city_shipping"), shippingDetails.optString("pt_country_shipping"), shippingDetails.optString("pt_email_shipping"), shippingDetails.optString("pt_name_shipping"), shippingDetails.optString("pt_phone_shipping"), shippingDetails.optString("pt_state_shipping"), shippingDetails.optString("pt_address_shipping"), shippingDetails.optString("pt_zip_shipping"));
}
final String ptCardDiscounts = paymentDetails.optString("pt_card_discounts", "[]");
final List<PaymentSdkCardDiscount> cardDiscounts = new Gson().fromJson(ptCardDiscounts, new TypeToken<List<PaymentSdkCardDiscount>>() {
}.getType());
final List<PaymentSdkCardDiscount> paymentSdkCardDiscounts = getPaymentSdkCardDiscounts(paymentDetails);
return new PaymentSdkConfigBuilder(profileId, serverKey, clientKey, amount, currency).setCartDescription(cartDesc).setLanguageCode(locale).setBillingData(billingData).setMerchantCountryCode(paymentDetails.optString("pt_merchant_country_code")).setShippingData(shippingData).setCartId(orderId).setTransactionClass(createPaymentSdkTransactionClass(paymentDetails.optString("pt_transaction_class"))).setTransactionType(transaction_type).setTokenise(tokeniseType, tokenFormat).setTokenisationData(token, transRef).setAlternativePaymentMethods(aPmsList).showBillingInfo(paymentDetails.optBoolean("pt_show_billing_info")).showShippingInfo(paymentDetails.optBoolean("pt_show_shipping_info")).forceShippingInfo(paymentDetails.optBoolean("pt_force_validate_shipping")).setMerchantIcon(iconUri).setScreenTitle(screenTitle).linkBillingNameWithCard(paymentDetails.optBoolean("pt_link_billing_name")).hideCardScanner(paymentDetails.optBoolean("pt_hide_card_scanner"))
//New stuff
.enableZeroContacts(paymentDetails.optBoolean("pt_enable_zero_contacts")).isDigitalProduct(paymentDetails.optBoolean("pt_is_digital_product")).setPaymentExpiry(paymentScreenExpiry)
.setCardDiscount(cardDiscounts).build();
.setCardDiscount(paymentSdkCardDiscounts).build();
}

@NonNull
private static List<PaymentSdkCardDiscount> getPaymentSdkCardDiscounts(JSONObject paymentDetails) {
final JSONArray ptCardDiscounts = paymentDetails.optJSONArray("pt_card_discounts");
final List<PaymentSdkCardDiscount> paymentSdkCardDiscounts = new ArrayList<>();
if (ptCardDiscounts != null) {
for (int i = 0; i < ptCardDiscounts.length(); i++) {
final JSONObject discount = ptCardDiscounts.optJSONObject(i);
final JSONArray cardsPrefixes = discount.optJSONArray("pt_discount_cards");
final List<String> cards = new ArrayList<>();
if (cardsPrefixes != null) {
for (int j = 0; j < cardsPrefixes.length(); j++) {
cards.add(cardsPrefixes.optString(j));
}
}
final PaymentSdkCardDiscount cardDiscount = new PaymentSdkCardDiscount(cards, discount.optDouble("pt_discount_value"),
discount.optString("pt_discount_title"), discount.optBoolean("pt_is_percentage"));
paymentSdkCardDiscounts.add(cardDiscount);
}
}
return paymentSdkCardDiscounts;
}

public static String optString(JSONObject json, String key) {
Expand Down
9 changes: 4 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:flutter_paytabs_bridge/BaseBillingShippingInfo.dart';
import 'package:flutter_paytabs_bridge/CardDiscount.dart';
import 'package:flutter_paytabs_bridge/IOSThemeConfiguration.dart';
import 'package:flutter_paytabs_bridge/PaymentSDKCardDiscount.dart';
import 'package:flutter_paytabs_bridge/PaymentSDKQueryConfiguration.dart';
Expand Down Expand Up @@ -62,12 +61,12 @@ class _MyAppState extends State<MyApp> {
PaymentSDKCardDiscount(
discountCards: ["4111"],
discountValue: 50,
discountTitle: "testing discounts ",
discountTitle: "50% discount on cards starting with 4111",
isPercentage: true),
PaymentSDKCardDiscount(
discountCards: ["4000"],
discountValue: 30,
discountTitle: "testing discounts 22 ",
discountCards: ["4000", "41111"],
discountValue: 2,
discountTitle: "2 discount on cards starting with 4000 and 41111",
isPercentage: false)
];
return configuration;
Expand Down
49 changes: 0 additions & 49 deletions lib/CardDiscount.dart

This file was deleted.

20 changes: 20 additions & 0 deletions lib/PaymentSDKCardDiscount.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// Importing the 'flutter_paytabs_bridge.dart' file.
import 'flutter_paytabs_bridge.dart';

/// `PaymentSDKCardDiscount` is a class that represents a discount card.
///
/// It has four properties: `discountCards`, `discountValue`, `discountTitle`, and `isPercentage`.
class PaymentSDKCardDiscount {
/// A list of strings representing the discount cards.
List<String> discountCards;

/// A double representing the value of the discount.
double discountValue;

/// A string representing the title of the discount.
String discountTitle;

/// A boolean indicating whether the discount is a percentage or not.
bool isPercentage;

/// The constructor for the `PaymentSDKCardDiscount` class.
///
/// It requires all four properties to be initialized.
PaymentSDKCardDiscount({
required this.discountCards,
required this.discountValue,
Expand All @@ -14,7 +28,13 @@ class PaymentSDKCardDiscount {
});
}

/// An extension on the `PaymentSDKCardDiscount` class.
///
/// It adds a getter `map` that converts the `PaymentSDKCardDiscount` object's data into a map.
extension PaymentSDKCardDiscountExtension on PaymentSDKCardDiscount {
/// A getter that converts the `PaymentSDKCardDiscount` object's data into a map.
///
/// It returns a map with the keys `pt_discount_cards`, `pt_discount_value`, `pt_discount_title`, and `pt_is_percentage`.
Map<String, dynamic> get map {
return {
pt_discount_cards: this.discountCards,
Expand Down
6 changes: 2 additions & 4 deletions lib/PaymentSdkConfigurationDetails.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:flutter_paytabs_bridge/CardDiscount.dart';

import 'BaseBillingShippingInfo.dart';
import 'IOSThemeConfiguration.dart';
import 'PaymentSDKCardDiscount.dart';
import 'PaymentSdkApms.dart';
import 'PaymentSdkLocale.dart';
import 'PaymentSdkTokenFormat.dart';
import 'PaymentSdkTokeniseType.dart';
import 'PaymentSdkTransactionClass.dart';
import 'PaymentSdkTransactionType.dart';
import 'PaymentSDKCardDiscount.dart';
import 'flutter_paytabs_bridge.dart';

class PaymentSdkConfigurationDetails {
Expand Down Expand Up @@ -130,7 +128,7 @@ extension PaymentSdkConfigurationDetailsExtension
pt_enable_zero_contacts: this.enableZeroContacts,
pt_is_digital_product: this.isDigitalProduct,
pt_expiry_time: this.expiryTime,
pt_card_discounts: this.cardDiscounts?.map
pt_card_discounts: this.cardDiscounts?.map((e) => e.map).toList(),
};
}
}

0 comments on commit 02b5469

Please sign in to comment.