Skip to content

Commit

Permalink
chore: add more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Sep 25, 2020
1 parent da2de26 commit aa3181f
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 1 deletion.
16 changes: 16 additions & 0 deletions android/src/ti/stripe/TiEphemeralKeyProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ti.stripe;

import com.stripe.android.EphemeralKeyProvider;
import com.stripe.android.EphemeralKeyUpdateListener;

import org.jetbrains.annotations.NotNull;

public class TiEphemeralKeyProvider implements EphemeralKeyProvider {

@Override
public void createEphemeralKey(@NotNull String apiVersion, @NotNull EphemeralKeyUpdateListener ephemeralKeyUpdateListener) {
// TODO: Add a HTTP request here and use the backend API info (url)
// via a module property
ephemeralKeyUpdateListener.onKeyUpdate("<TEST>");
}
}
65 changes: 65 additions & 0 deletions android/src/ti/stripe/TiHostActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ti.stripe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.stripe.android.PaymentSession;
import com.stripe.android.PaymentSessionConfig;
import com.stripe.android.PaymentSessionData;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.ShippingInformation;

public class TiHostActivity extends Activity {
private PaymentSession paymentSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This currently does not work?!
/*paymentSession = new PaymentSession(
this,
new PaymentSessionConfig.Builder()
.setShouldPrefetchCustomer(true)
.setShippingInfoRequired(false)
.setShippingMethodsRequired(false)
.build()
);*/
setupPaymentSession();
}

private void setupPaymentSession() {
paymentSession.init(
new PaymentSession.PaymentSessionListener() {
@Override
public void onCommunicatingStateChanged(boolean isCommunicating) {
// update UI, such as hiding or showing a progress bar
}

@Override
public void onError(int errorCode, String errorMessage) {
// handle error
}

@Override
public void onPaymentSessionDataChanged(PaymentSessionData data) {
final PaymentMethod paymentMethod = data.getPaymentMethod();
// use paymentMethod
}
}
);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
paymentSession.handlePaymentData(requestCode, resultCode, data);
}
}

private ShippingInformation getDefaultShippingInfo() {
// optionally specify default shipping address
return new ShippingInformation();
}
}
109 changes: 108 additions & 1 deletion android/src/ti/stripe/TitaniumStripeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,56 @@
*/
package ti.stripe;

import androidx.activity.ComponentActivity;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.jetbrains.annotations.NotNull;

import com.stripe.android.CustomerSession;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.EphemeralKeyProvider;
import com.stripe.android.PaymentSession;
import com.stripe.android.PaymentSessionConfig;
import com.stripe.android.PaymentSessionData;
import com.stripe.android.model.Address;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.ShippingInformation;
import com.stripe.android.view.ShippingInfoWidget;

import java.util.Arrays;

@Kroll.module(name="TitaniumStripe", id="ti.stripe")
public class TitaniumStripeModule extends KrollModule {/* implements EphemeralKeyProvider {*/
public class TitaniumStripeModule extends KrollModule {

// Standard Debugging variables
private static final String LCAT = "TitaniumStripeModule";
private static final boolean DBG = TiConfig.LOGD;

private String ephemeralKeyAPIURL = "";
private PaymentSession paymentSession;

@Kroll.method
public void initialize(KrollDict params) {
String publishableKey = params.getString("publishableKey");
ephemeralKeyAPIURL = params.getString("ephemeralKeyAPIURL");
PaymentConfiguration.init(TiApplication.getInstance().getApplicationContext(), publishableKey);

// Create the PaymentSession
paymentSession = new PaymentSession((ComponentActivity) TiApplication.getAppCurrentActivity(), createPaymentSessionConfig());

// Attach your listener
paymentSession.init(createPaymentSessionListener());

CustomerSession.initCustomerSession(
TiApplication.getAppRootOrCurrentActivity().getApplicationContext(),
new TiEphemeralKeyProvider()
);
}

@Kroll.method
Expand All @@ -44,5 +69,87 @@ public void updatePaymentDetails(KrollDict params) {
public void requestPayment(KrollDict params) {
// TODO
}

// Helper

private PaymentSessionConfig createPaymentSessionConfig() {
return new PaymentSessionConfig.Builder()

// hide the phone field on the shipping information form
.setHiddenShippingInfoFields(
ShippingInfoWidget.CustomizableShippingField.Phone
)

// make the address line 2 field optional
.setOptionalShippingInfoFields(
ShippingInfoWidget.CustomizableShippingField.Line2
)

// specify an address to pre-populate the shipping information form
.setPrepopulatedShippingInfo(new ShippingInformation(
new Address.Builder()
.setLine1("123 Market St")
.setCity("San Francisco")
.setState("CA")
.setPostalCode("94107")
.setCountry("US")
.build(),
"Jenny Rosen",
"4158675309")
)

// collect shipping information
.setShippingInfoRequired(true)

// collect shipping method
.setShippingMethodsRequired(true)

// specify the payment method types that the customer can use;
// defaults to PaymentMethod.Type.Card
.setPaymentMethodTypes(
Arrays.asList(PaymentMethod.Type.Card)
)

// if `true`, will show "Google Pay" as an option on the
// Payment Methods selection screen
.setShouldShowGooglePay(true)

.build();
}

private PaymentSession.PaymentSessionListener createPaymentSessionListener() {
return new PaymentSession.PaymentSessionListener() {
@Override
public void onPaymentSessionDataChanged(PaymentSessionData data) {
if (data.getUseGooglePay()) {
// customer intends to pay with Google Pay
} else {
final PaymentMethod paymentMethod = data.getPaymentMethod();
if (paymentMethod != null) {
// Display information about the selected payment method
}
}

// Update your UI here with other data
if (data.isPaymentReadyToCharge()) {
// Use the data to complete your charge - see below.
}
}

@Override
public void onCommunicatingStateChanged(boolean isCommunicating) {
if (isCommunicating) {
// update UI to indicate that network communication is in progress
} else {
// update UI to indicate that network communication has completed
}
}

@Override
public void onError(int errorCode, String errorMessage) {
// handle error
}
};
}
}

0 comments on commit aa3181f

Please sign in to comment.