Skip to content

Commit

Permalink
feat(prefill_address): prefill shipping address in StripePaymentSessi…
Browse files Browse the repository at this point in the history
…on (#71)

* Support prefilled address in StripePaymentSession.

* Support prefilled Address in StripePaymentSession,
on Android. #70

* chore(): bump to v5.4
  • Loading branch information
RobertGardner authored Jun 12, 2019
1 parent 2254800 commit 528ad38
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
==============================

## 5.4.0 (2019, June 12)
### Fixes
- [(# 70)](https://github.com/triniwiz/nativescript-stripe/issues/70) Support prefilled shipping address in StripePaymentSession

## 5.3.3 (2019, May 13)
### Fixes
- [(# 63)](https://github.com/triniwiz/nativescript-stripe/issues/63) Properly handle no shipping method or address
Expand Down
16 changes: 8 additions & 8 deletions demo/app/App_Resources/Android/app.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// compile 'com.android.support:recyclerview-v7:+'
//}

android {
defaultConfig {
android {
defaultConfig {
generatedDensities = []
applicationId = "org.nativescript.demo"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
applicationId = "org.nativescript.stripe.demo"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-stripe",
"version": "5.3.3",
"version": "5.4.0",
"description": "NativeScript Stripe sdk",
"main": "stripe",
"typings": "index.d.ts",
Expand Down
21 changes: 11 additions & 10 deletions src/standard/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export declare class StripePaymentSession {
customerSession: StripeCustomerSession,
amount: number,
currency: string,
listener: StripePaymentListener);
listener: StripePaymentListener,
prefilledAddress?: StripeAddress);
/** Is the native component loading? */
readonly loading: boolean;
/** Has user entered enough info that a charge can be made? */
Expand Down Expand Up @@ -100,15 +101,15 @@ export declare interface StripePaymentData {
shippingInfo: StripeShippingMethod;
}
export declare interface StripeAddress {
name: string;
line1: string;
line2: string;
city: string;
state: string;
postalCode: string;
country: string;
phone: string;
email: string;
name?: string;
line1?: string;
line2?: string;
city?: string;
state?: string;
postalCode?: string;
country?: string;
phone?: string;
email?: string;
}
export declare interface StripeShippingMethods {
/** Is shipping to the address valid? */
Expand Down
38 changes: 32 additions & 6 deletions src/standard/standard.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,39 @@ export class StripePaymentSession {
paymentInProgress: boolean;
private receiver: android.content.BroadcastReceiver;

constructor(private page: Page,
constructor(_page: Page,
public customerSession: StripeCustomerSession,
amount: number,
public currency: string,
listener: StripePaymentListener) {
listener: StripePaymentListener,
prefilledAddress?: StripeAddress) {
let config = StripeConfig.shared().native;
if (prefilledAddress) {
const info = new com.stripe.android.model.ShippingInformation(
new com.stripe.android.model.Address.Builder()
.setLine1(prefilledAddress.line1)
.setLine2(prefilledAddress.line2)
.setCity(prefilledAddress.city)
.setState(prefilledAddress.state)
.setCountry(prefilledAddress.country)
.setPostalCode(prefilledAddress.postalCode)
.build(),
prefilledAddress.name,
prefilledAddress.phone
);
config = new com.stripe.android.PaymentSessionConfig.Builder()
.setOptionalShippingInfoFields(
config.getOptionalShippingInfoFields() ?
config.getOptionalShippingInfoFields().toArray() : [])
.setShippingInfoRequired(config.isShippingInfoRequired())
.setShippingMethodsRequired(config.isShippingMethodRequired())
.setHiddenShippingInfoFields(config.getHiddenShippingInfoFields() ?
config.getHiddenShippingInfoFields().toArray() : [])
.setPrepopulatedShippingInfo(info)
.build();
}
this.native = new com.stripe.android.PaymentSession(this.patchActivity());
if (!this.native.init(createPaymentListener(this, listener), StripeConfig.shared().native)) {
if (!this.native.init(createPaymentListener(this, listener), config)) {
throw new Error("CustomerSession not initialized");
}
this.native.setCartTotal(amount);
Expand Down Expand Up @@ -132,7 +158,7 @@ export class StripePaymentSession {

function createPaymentListener(parent: StripePaymentSession, listener: StripePaymentListener): com.stripe.android.PaymentSession.PaymentSessionListener {
return new com.stripe.android.PaymentSession.PaymentSessionListener({
onPaymentSessionDataChanged: function(sessionData: com.stripe.android.PaymentSessionData): void {
onPaymentSessionDataChanged: function (sessionData: com.stripe.android.PaymentSessionData): void {
if (parent.paymentInProgress) {
if (sessionData.getPaymentResult() === com.stripe.android.PaymentResultListener.SUCCESS) {
if (listener.onPaymentSuccess) listener.onPaymentSuccess();
Expand Down Expand Up @@ -160,11 +186,11 @@ function createPaymentListener(parent: StripePaymentSession, listener: StripePay
}
}));
},
onCommunicatingStateChanged: function(isCommunicating: boolean): void {
onCommunicatingStateChanged: function (isCommunicating: boolean): void {
parent.loading = isCommunicating;
listener.onCommunicatingStateChanged(isCommunicating);
},
onError: function(code: number, message: string): void {
onError: function (code: number, message: string): void {
listener.onError(code, message);
}
});
Expand Down
18 changes: 9 additions & 9 deletions src/standard/standard.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ export interface StripeShippingMethods {
}

export interface StripeAddress {
name: string;
line1: string;
line2: string;
city: string;
state: string;
postalCode: string;
country: string;
phone: string;
email: string;
name?: string;
line1?: string;
line2?: string;
city?: string;
state?: string;
postalCode?: string;
country?: string;
phone?: string;
email?: string;
}

export const enum StripeBillingAddressFields {
Expand Down
16 changes: 15 additions & 1 deletion src/standard/standard.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,27 @@ export class StripePaymentSession {
customerSession: StripeCustomerSession,
amount: number,
currency: string,
listener?: StripePaymentListener) {
listener?: StripePaymentListener,
prefilledAddress?: StripeAddress) {
this.native = STPPaymentContext.alloc()
.initWithCustomerContextConfigurationTheme(
customerSession.native,
StripeConfig.shared().native,
STPTheme.defaultTheme());
this.native.prefilledInformation = STPUserInformation.alloc().init();
if (prefilledAddress) {
const addr = STPAddress.alloc().init();
addr.name = prefilledAddress.name;
addr.line1 = prefilledAddress.line1;
addr.line2 = prefilledAddress.line2;
addr.city = prefilledAddress.city;
addr.state = prefilledAddress.state;
addr.country = prefilledAddress.country;
addr.postalCode = prefilledAddress.postalCode;
addr.phone = prefilledAddress.phone;
addr.email = prefilledAddress.email;
this.native.prefilledInformation.shippingAddress = addr;
}
this.native.paymentAmount = amount;
this.native.paymentCurrency = currency;
if (listener) {
Expand Down

0 comments on commit 528ad38

Please sign in to comment.