Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): to be customer optional #165

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ public void createPaymentFlow(final PluginCall call) {
String customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret", null);
String customerId = call.getString("customerId", null);

if ((paymentIntentClientSecret == null && setupIntentClientSecret == null) || customerId == null) {
if (paymentIntentClientSecret == null && setupIntentClientSecret == null) {
notifyListenersFunction.accept(PaymentFlowEvents.FailedToLoad.getWebEventName(), emptyObject);
call.reject("Invalid Params. this method require paymentIntentClientSecret or setupIntentClientSecret, and customerId.");
call.reject("Invalid Params. This method require paymentIntentClientSecret or setupIntentClientSecret.");
return;
}

if (customerId != null && customerEphemeralKeySecret == null) {
notifyListenersFunction.accept(PaymentFlowEvents.FailedToLoad.getWebEventName(), emptyObject);
call.reject("Invalid Params. When you set customerId, you must set customerEphemeralKeySecret.");
return;
}

Expand All @@ -52,10 +58,12 @@ public void createPaymentFlow(final PluginCall call) {
merchantDisplayName = "";
}

PaymentSheet.Configuration paymentConfiguration = new PaymentSheet.Configuration(
merchantDisplayName,
new PaymentSheet.CustomerConfiguration(customerId, customerEphemeralKeySecret)
);
PaymentSheet.Configuration paymentConfiguration = customerId != null
? new PaymentSheet.Configuration(
merchantDisplayName,
new PaymentSheet.CustomerConfiguration(customerId, customerEphemeralKeySecret)
)
: new PaymentSheet.Configuration(merchantDisplayName);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ public void createPaymentSheet(final PluginCall call) {
String customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret", null);
String customerId = call.getString("customerId", null);

if (paymentIntentClientSecret == null || customerId == null) {
if (paymentIntentClientSecret == null) {
notifyListenersFunction.accept(PaymentSheetEvents.FailedToLoad.getWebEventName(), emptyObject);
call.reject("Invalid Params. this method require paymentIntentClientSecret and customerId.");
call.reject("Invalid Params. this method require paymentIntentClientSecret.");
return;
}

if (customerId != null && customerEphemeralKeySecret == null) {
notifyListenersFunction.accept(PaymentFlowEvents.FailedToLoad.getWebEventName(), emptyObject);
call.reject("Invalid Params. When you set customerId, you must set customerEphemeralKeySecret.");
return;
}

Expand All @@ -53,10 +59,12 @@ public void createPaymentSheet(final PluginCall call) {
}

paymentConfiguration =
new PaymentSheet.Configuration(
merchantDisplayName,
new PaymentSheet.CustomerConfiguration(customerId, customerEphemeralKeySecret)
);
customerId != null
? new PaymentSheet.Configuration(
merchantDisplayName,
new PaymentSheet.CustomerConfiguration(customerId, customerEphemeralKeySecret)
)
: new PaymentSheet.Configuration(merchantDisplayName);

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

Expand Down
4 changes: 2 additions & 2 deletions demo/angular/ios/App/App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
CODE_SIGN_ENTITLEMENTS = App/App.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = MYVF6R3R26;
DEVELOPMENT_TEAM = P7E2DJ2G49;
INFOPLIST_FILE = App/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -372,7 +372,7 @@
CODE_SIGN_ENTITLEMENTS = App/App.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = MYVF6R3R26;
DEVELOPMENT_TEAM = P7E2DJ2G49;
INFOPLIST_FILE = App/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
2 changes: 1 addition & 1 deletion demo/angular/ios/App/App/App.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<dict>
<key>com.apple.developer.in-app-payments</key>
<array>
<string>merchant.com.getcapacitor.stripe</string>
<string>merchant.com.capacitor.stripe</string>
</array>
</dict>
</plist>
11 changes: 11 additions & 0 deletions demo/angular/src/app/tab1/tab1.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@
<ion-item button="true" detail="true" (click)="createPaymentSheet()" [disabled]="processSheet !== 'willReady'"><ion-label>create</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="presentPaymentSheet()" [disabled]="processSheet !== 'Ready'"><ion-label>present</ion-label></ion-item>
</ion-list>
<ion-list>
<ion-list-header><ion-label>PaymentSheet without customer</ion-label></ion-list-header>
<ion-item button="true" detail="true" (click)="createPaymentSheet(false)" [disabled]="processSheet !== 'willReady'"><ion-label>create</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="presentPaymentSheet()" [disabled]="processSheet !== 'Ready'"><ion-label>present</ion-label></ion-item>
</ion-list>
<ion-list>
<ion-list-header><ion-label>PaymentFlow</ion-label></ion-list-header>
<ion-item button="true" detail="true" (click)="createPaymentFlow()" [disabled]="processFlow !== 'willReady'"><ion-label>create</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="presentPaymentFlow()" [disabled]="processFlow !== 'Ready'"><ion-label>present</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="confirmPaymentFlow()" [disabled]="processFlow !== 'canConfirm'"><ion-label>confirm</ion-label></ion-item>
</ion-list>
<ion-list>
<ion-list-header><ion-label>PaymentFlow without customer</ion-label></ion-list-header>
<ion-item button="true" detail="true" (click)="createPaymentFlow()" [disabled]="processFlow !== 'willReady'"><ion-label>create</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="presentPaymentFlow()" [disabled]="processFlow !== 'Ready'"><ion-label>present</ion-label></ion-item>
<ion-item button="true" detail="true" (click)="confirmPaymentFlow()" [disabled]="processFlow !== 'canConfirm'"><ion-label>confirm</ion-label></ion-item>
</ion-list>
<ion-list>
<ion-list-header><ion-label>ApplePay</ion-label></ion-list-header>
<ion-item button="true" detail="true" (click)="createApplePay()" [disabled]="processApplePay !== 'willReady' || !isApplePayAvailable"><ion-label>create</ion-label></ion-item>
Expand Down
78 changes: 50 additions & 28 deletions demo/angular/src/app/tab1/tab1.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,60 @@ export class Tab1Page implements OnInit {
Stripe.isGooglePayAvailable().then(() => this.isGooglePayAvailable = true);
}

async createPaymentSheet() {
const { paymentIntent, ephemeralKey, customer } = await this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentSheet({
paymentIntentClientSecret: paymentIntent,
customerEphemeralKeySecret: ephemeralKey,
customerId: customer,
merchantDisplayName: 'rdlabo',
});
async createPaymentSheet(withCustomer = true) {
if (withCustomer) {
const { paymentIntent, ephemeralKey, customer } = await this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentSheet({
paymentIntentClientSecret: paymentIntent,
customerEphemeralKeySecret: ephemeralKey,
customerId: customer,
merchantDisplayName: 'rdlabo',
});
} else {
const { paymentIntent } = await this.http.post<{
paymentIntent: string;
}>(environment.api + 'intent/without-customer', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentSheet({
paymentIntentClientSecret: paymentIntent,
merchantDisplayName: 'rdlabo',
});
}
}

presentPaymentSheet() {
Stripe.presentPaymentSheet();
}

async createPaymentFlow() {
const { paymentIntent, ephemeralKey, customer } = await this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentFlow({
paymentIntentClientSecret: paymentIntent,
customerEphemeralKeySecret: ephemeralKey,
customerId: customer,
merchantDisplayName: 'rdlabo',
});
async createPaymentFlow(withCustomer = true) {
if (withCustomer) {
const { paymentIntent, ephemeralKey, customer } = await this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentFlow({
paymentIntentClientSecret: paymentIntent,
customerEphemeralKeySecret: ephemeralKey,
customerId: customer,
merchantDisplayName: 'rdlabo',
});
} else {
const { paymentIntent } = await this.http.post<{
paymentIntent: string;
}>(environment.api + 'intent/without-customer', {}).pipe(first()).toPromise(Promise);

await Stripe.createPaymentFlow({
paymentIntentClientSecret: paymentIntent,
merchantDisplayName: 'rdlabo',
});
}
}

presentPaymentFlow() {
Expand All @@ -181,7 +203,7 @@ export class Tab1Page implements OnInit {
async createApplePay() {
const { paymentIntent } = await this.http.post<{
paymentIntent: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise);
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise);

await Stripe.createApplePay({
paymentIntentClientSecret: paymentIntent,
Expand All @@ -202,7 +224,7 @@ export class Tab1Page implements OnInit {
async createGooglePay() {
const { paymentIntent } = await this.http.post<{
paymentIntent: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise);
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise);

await Stripe.createGooglePay({
paymentIntentClientSecret: paymentIntent,
Expand Down
2 changes: 1 addition & 1 deletion demo/angular/src/app/tab2/tab2.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Tab2Page {
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise)
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise)
.catch(async (e) => {
await this.helper.updateItem(this.eventItems,'HttpClient', false);
throw e;
Expand Down
2 changes: 1 addition & 1 deletion demo/angular/src/app/tab3/tab3.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Tab3Page {
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'payment-sheet', {}).pipe(first()).toPromise(Promise)
}>(environment.api + 'intent', {}).pipe(first()).toPromise(Promise)
.catch(async (e) => {
await this.helper.updateItem(this.eventItems,'HttpClient', false);
throw e;
Expand Down
2 changes: 1 addition & 1 deletion demo/react/src/hooks/payment-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const usePaymentSheet = () => {
customer,
paymentIntent,
ephemeralKey,
} = await fetch(`${api}payment-sheet`, {
} = await fetch(`${api}intent`, {
method: 'POST'
}).then(res => res.json())
return {
Expand Down
1 change: 0 additions & 1 deletion demo/server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
Expand Down
22 changes: 19 additions & 3 deletions demo/server/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Post } from '@nestjs/common';
import { Controller, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { Stripe } from 'stripe';

Expand All @@ -10,8 +10,8 @@ export class AppController {
this.stripe = require('stripe')('sk_test_BpbfN6DaB1VhEDVMlLg33IIL');
}

@Post('payment-sheet')
async createPaymentSheet(): Promise<{
@Post('intent')
async createIntent(): Promise<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
Expand All @@ -35,4 +35,20 @@ export class AppController {
customer: customer.id
}
}

@Post('intent/without-customer')
async createIntentWithoutCustomer(): Promise<{
paymentIntent: string;
}> {
/**
* https://stripe.com/docs/payments/accept-a-payment?platform=ios
*/
const paymentIntent = await this.stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
});
return {
paymentIntent: paymentIntent.client_secret,
}
}
}
17 changes: 13 additions & 4 deletions ios/Plugin/PaymentFlow/PaymentFlowExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ class PaymentFlowExecutor: NSObject {
let setupIntentClientSecret = call.getString("setupIntentClientSecret") ?? nil

let customerId = call.getString("customerId") ?? nil
let customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret") ?? ""
let customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret") ?? nil

if (paymentIntentClientSecret == nil && setupIntentClientSecret == nil) || customerId == nil {
call.reject("Invalid Params. this method require paymentIntentClientSecret or setupIntentClientSecret, and customerId.")
if paymentIntentClientSecret == nil && setupIntentClientSecret == nil {
self.plugin?.notifyListeners(PaymentFlowEvents.FailedToLoad.rawValue, data: [:])
call.reject("Invalid Params. this method require paymentIntentClientSecret or setupIntentClientSecret.")
return
}

if customerId != nil && customerEphemeralKeySecret == nil {
self.plugin?.notifyListeners(PaymentFlowEvents.FailedToLoad.rawValue, data: [:])
call.reject("Invalid Params. When you set customerId, you must set customerEphemeralKeySecret.")
return
}

Expand Down Expand Up @@ -44,7 +51,9 @@ class PaymentFlowExecutor: NSObject {
)
}

configuration.customer = .init(id: customerId!, ephemeralKeySecret: customerEphemeralKeySecret)
if customerId != nil && customerEphemeralKeySecret != nil {
configuration.customer = .init(id: customerId!, ephemeralKeySecret: customerEphemeralKeySecret!)
}

if setupIntentClientSecret != nil {
PaymentSheet.FlowController.create(setupIntentClientSecret: setupIntentClientSecret!,
Expand Down
17 changes: 13 additions & 4 deletions ios/Plugin/PaymentSheet/PaymentSheetExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ class PaymentSheetExecutor: NSObject {
func createPaymentSheet(_ call: CAPPluginCall) {
let paymentIntentClientSecret = call.getString("paymentIntentClientSecret") ?? nil
let customerId = call.getString("customerId") ?? nil
let customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret") ?? ""
let customerEphemeralKeySecret = call.getString("customerEphemeralKeySecret") ?? nil

if paymentIntentClientSecret == nil || customerId == nil {
call.reject("Invalid Params. this method require paymentIntentClientSecret and customerId.")
if paymentIntentClientSecret == nil {
self.plugin?.notifyListeners(PaymentFlowEvents.FailedToLoad.rawValue, data: [:])
call.reject("Invalid Params. this method require paymentIntentClientSecret.")
return
}

if customerId != nil && customerEphemeralKeySecret == nil {
self.plugin?.notifyListeners(PaymentFlowEvents.FailedToLoad.rawValue, data: [:])
call.reject("Invalid Params. When you set customerId, you must set customerEphemeralKeySecret.")
return
}

Expand Down Expand Up @@ -42,7 +49,9 @@ class PaymentSheetExecutor: NSObject {
)
}

configuration.customer = .init(id: customerId!, ephemeralKeySecret: customerEphemeralKeySecret)
if customerId != nil && customerEphemeralKeySecret != nil {
configuration.customer = .init(id: customerId!, ephemeralKeySecret: customerEphemeralKeySecret!)
}

self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret!, configuration: configuration)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capacitor-community/stripe",
"version": "3.5.3",
"version": "3.6.0-0",
"description": "Stripe SDK bindings for Capacitor Applications",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down