Skip to content

Commit

Permalink
Merge pull request #84 from capacitor-community/feat/web-payment-sheet
Browse files Browse the repository at this point in the history
feat(web): support web platform
  • Loading branch information
rdlabo committed Jul 23, 2021
2 parents a075594 + 0230b0e commit a7cc524
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import android.app.Activity;
import android.util.Log;

import androidx.activity.ComponentActivity;

import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand All @@ -17,7 +15,7 @@
@NativePlugin(name = "Stripe", requestCodes = { 9972, 50000, 50001, 6000 })
public class StripePlugin extends Plugin {

// private Stripe stripeInstance;
// private Stripe stripeInstance;
private String publishableKey;
private Boolean isTest = true;
private GooglePayCallback googlePayCallback = null;
Expand All @@ -32,9 +30,13 @@ public class StripePlugin extends Plugin {

@Override
public void load() {
this.paymentSheetExecutor.paymentSheet = new PaymentSheet(getActivity(), result -> {
this.paymentSheetExecutor.onPaymentSheetResult(bridge, callbackId, result);
});
this.paymentSheetExecutor.paymentSheet =
new PaymentSheet(
getActivity(),
result -> {
this.paymentSheetExecutor.onPaymentSheetResult(bridge, callbackId, result);
}
);
}

@PluginMethod
Expand All @@ -46,7 +48,7 @@ public void initialize(final PluginCall call) {
call.reject("you must provide a valid key");
return;
}
// stripeInstance = new Stripe(getContext(), publishableKey);
// stripeInstance = new Stripe(getContext(), publishableKey);
isTest = publishableKey.contains("test");
PaymentConfiguration.init(getContext(), publishableKey);
call.resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.activity.ComponentActivity;
import androidx.core.util.Supplier;

import com.getcapacitor.Bridge;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
Expand All @@ -19,6 +17,7 @@
import com.stripe.android.paymentsheet.PaymentSheetResultCallback;

public class PaymentSheetExecutor extends Executor {

public PaymentSheet paymentSheet;
private final JSObject emptyObject = new JSObject();
private PaymentSheet.Configuration paymentConfiguration;
Expand Down Expand Up @@ -48,20 +47,17 @@ public void createPaymentSheet(final PluginCall call, String publishableKey) {
return;
}


String merchantDisplayName = call.getString("merchantDisplayName");

if (merchantDisplayName == null) {
merchantDisplayName = "";
}

paymentConfiguration = new PaymentSheet.Configuration(
paymentConfiguration =
new PaymentSheet.Configuration(
merchantDisplayName,
new PaymentSheet.CustomerConfiguration(
customerId,
customerEphemeralKeySecret
)
);
new PaymentSheet.CustomerConfiguration(customerId, customerEphemeralKeySecret)
);

Boolean useGooglePay = call.getBoolean("useGooglePay", false);

Expand All @@ -73,11 +69,10 @@ public void createPaymentSheet(final PluginCall call, String publishableKey) {
environment = PaymentSheet.GooglePayConfiguration.Environment.Test;
}

final PaymentSheet.GooglePayConfiguration googlePayConfiguration =
new PaymentSheet.GooglePayConfiguration(
environment,
call.getString("countryCode", "US")
);
final PaymentSheet.GooglePayConfiguration googlePayConfiguration = new PaymentSheet.GooglePayConfiguration(
environment,
call.getString("countryCode", "US")
);
paymentConfiguration.setGooglePay(googlePayConfiguration);
}

Expand All @@ -87,20 +82,13 @@ public void createPaymentSheet(final PluginCall call, String publishableKey) {

public void presentPaymentSheet(final PluginCall call) {
try {
paymentSheet.presentWithPaymentIntent(
paymentIntentClientSecret,
paymentConfiguration
);
paymentSheet.presentWithPaymentIntent(paymentIntentClientSecret, paymentConfiguration);
} catch (Exception ex) {
call.reject(ex.getLocalizedMessage(), ex);
}
}

public void onPaymentSheetResult(
Bridge bridge,
String callbackId,
final PaymentSheetResult paymentSheetResult
) {
public void onPaymentSheetResult(Bridge bridge, String callbackId, final PaymentSheetResult paymentSheetResult) {
PluginCall call = bridge.getSavedCall(callbackId);

if (paymentSheetResult instanceof PaymentSheetResult.Canceled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

public class MainActivity extends BridgeActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerPlugin(com.getcapacitor.community.stripe.StripePlugin.class);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerPlugin(com.getcapacitor.community.stripe.StripePlugin.class);
}
}
134 changes: 131 additions & 3 deletions demo/angular/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@capacitor/android": "^3.1.1",
"@capacitor/core": "3.0.0",
"@ionic/angular": "^5.5.2",
"@stripe-elements/stripe-elements": "0.0.1",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.11.4"
Expand Down
6 changes: 4 additions & 2 deletions demo/angular/src/app/tab1/tab1.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<ion-title size="large">Tab 1</ion-title>
</ion-toolbar>
</ion-header>

<div class="ion-text-center ion-padding"><ion-button (click)="presentPaymentSheet()">決済表示</ion-button></div>
<ion-list>
<ion-item button="true" (click)="createPaymentSheet()" [disabled]="process === 'Ready'"><ion-label>準備</ion-label></ion-item>
<ion-item button="true" (click)="presentPaymentSheet()" [disabled]="process === 'willReady'"><ion-label>実行</ion-label></ion-item>
</ion-list>
</ion-content>
Loading

0 comments on commit a7cc524

Please sign in to comment.