Skip to content

Commit

Permalink
Update java example
Browse files Browse the repository at this point in the history
  • Loading branch information
mfareed-pp authored and CheZhongSdk committed Feb 22, 2023
1 parent fc1d9ca commit d0b9bf5
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Examples/Example-Java/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {
}

dependencies {
implementation 'com.izettle.payments:android-sdk-ui:1.36.1'
implementation 'com.izettle.payments:android-sdk-ui:1.36.7'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import androidx.lifecycle.MutableLiveData;

import com.google.android.material.snackbar.Snackbar;
import com.izettle.payments.android.payment.TippingStyle;
import com.izettle.payments.android.payment.TransactionReference;
import com.izettle.payments.android.payment.refunds.CardPaymentPayload;
import com.izettle.payments.android.payment.refunds.RefundsManager;
Expand All @@ -39,10 +40,11 @@ public class CardReaderActivity extends AppCompatActivity {
private EditText refundAmountEditText;
private Button settingsButton;
private EditText amountEditText;
private CheckBox tippingCheckBox;
private Button tippingStyleButton;
private CheckBox installmentsCheckBox;
private CheckBox loginCheckBox;
private MutableLiveData<String> lastPaymentTraceId;
private TippingStyle tippingStyle = TippingStyle.None;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -53,7 +55,7 @@ public void onCreate(Bundle savedInstanceState) {
settingsButton = findViewById(R.id.settings_btn);
amountEditText = findViewById(R.id.amount_input);
refundAmountEditText = findViewById(R.id.refund_amount_input);
tippingCheckBox = findViewById(R.id.tipping_check_box);
tippingStyleButton = findViewById(R.id.tipping_style_btn);
loginCheckBox = findViewById(R.id.login_check_box);
installmentsCheckBox = findViewById(R.id.installments_check_box);
lastPaymentTraceId = new MutableLiveData<>();
Expand All @@ -63,6 +65,15 @@ public void onCreate(Bundle savedInstanceState) {
chargeButton.setOnClickListener(v -> onChargeClicked());
refundButton.setOnClickListener(v -> onRefundClicked());
settingsButton.setOnClickListener(v -> onSettingsClicked());
tippingStyleButton.setOnClickListener(v -> onTippingStyleClicked());

setTippingStyleTitle();

getSupportFragmentManager().setFragmentResultListener(TippingStyleBottomSheet.REQUEST_KEY, this, (requestKey, result) -> {
TippingStyle newTippingStyle = (TippingStyle) result.getSerializable(TippingStyleBottomSheet.TIPPING_STYLE_KEY);
tippingStyle = newTippingStyle != null ? newTippingStyle : TippingStyle.None;
setTippingStyleTitle();
});
}

private final ActivityResultLauncher<Intent> paymentLauncher = registerForActivityResult(new StartActivityForResult(), result -> {
Expand Down Expand Up @@ -107,7 +118,7 @@ private void onChargeClicked() {
}
String internalTraceId = UUID.randomUUID().toString();
long amount = parseLong(amountEditText.getText());
boolean enableTipping = tippingCheckBox.isChecked();
TippingStyle tippingStyle = this.tippingStyle;
boolean enableInstallments = installmentsCheckBox.isChecked();
boolean enableLogin = loginCheckBox.isChecked();
TransactionReference reference = new TransactionReference.Builder(internalTraceId)
Expand All @@ -118,7 +129,7 @@ private void onChargeClicked() {
.amount(amount)
.reference(reference)
.enableInstalments(enableInstallments)
.enableTipping(enableTipping)
.enableTipping(tippingStyle)
.enableLogin(enableLogin)
.build();

Expand All @@ -145,6 +156,15 @@ private void onRefundClicked() {
);
}

private void onTippingStyleClicked() {
TippingStyleBottomSheet.newInstance().show(getSupportFragmentManager(), TippingStyleBottomSheet.class.getSimpleName());
}

private void setTippingStyleTitle() {
String tippingStyleTitle = "Tipping Style - " + tippingStyle.name();
tippingStyleButton.setText(tippingStyleTitle);
}

private class RefundCallback implements RefundsManager.Callback<CardPaymentPayload, RetrieveCardPaymentFailureReason> {

private final Long amount;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.izettle.payments.android.java_example;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.izettle.payments.android.payment.TippingStyle;

public class TippingStyleBottomSheet extends BottomSheetDialogFragment {

private View styleNoneView;
private View styleDefaultView;
private View styleAmountView;
private View stylePercentView;

public static final String REQUEST_KEY = "TippingStyleBottomSheetRC";
public static final String TIPPING_STYLE_KEY = "TippingStyleKey";

public static TippingStyleBottomSheet newInstance() {
return new TippingStyleBottomSheet();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tipping_style, container, false);
styleNoneView = view.findViewById(R.id.tipping_style_none);
styleDefaultView = view.findViewById(R.id.tipping_style_default);
styleAmountView = view.findViewById(R.id.tipping_style_amount);
stylePercentView = view.findViewById(R.id.tipping_style_percent);

return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

styleNoneView.setOnClickListener(v -> setResultAndExit(TippingStyle.None));
styleDefaultView.setOnClickListener(v -> setResultAndExit(TippingStyle.Default));
styleAmountView.setOnClickListener(v -> setResultAndExit(TippingStyle.Amount));
stylePercentView.setOnClickListener(v -> setResultAndExit(TippingStyle.Percentage));
}

private void setResultAndExit(TippingStyle tippingStyle) {
Bundle bundle = new Bundle();
bundle.putSerializable(TIPPING_STYLE_KEY, tippingStyle);

getParentFragmentManager().setFragmentResult(REQUEST_KEY, bundle);
dismiss();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@
android:layout_height="wrap_content"
android:orientation="horizontal">

<CheckBox
android:id="@+id/tipping_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="2"
android:text="Enable\nTipping" />

<CheckBox
android:id="@+id/installments_check_box"
android:layout_width="wrap_content"
Expand All @@ -88,6 +80,12 @@

</LinearLayout>

<Button
android:id="@+id/tipping_style_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tipping style - None" />

<Button
android:id="@+id/charge_btn"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<TextView
android:id="@+id/tipping_style_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textStyle="bold"
android:text="None"
android:background="?selectableItemBackground"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/tipping_style_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textStyle="bold"
android:text="Market Default"
android:background="?selectableItemBackground"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/tipping_style_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textStyle="bold"
android:text="Amount"
android:background="?selectableItemBackground"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/tipping_style_percent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textStyle="bold"
android:text="Percentage"
android:background="?selectableItemBackground"
tools:ignore="HardcodedText" />

</androidx.appcompat.widget.LinearLayoutCompat>

0 comments on commit d0b9bf5

Please sign in to comment.