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

Add Masterpass to STPSourceParams, allowing Sources to be created #928

Merged
merged 6 commits into from
Apr 17, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Adding Masterpass support to `STPSourceParams` [#928](https://github.com/stripe/stripe-ios/pull/928)
* Silence STPAddress deprecation warnings we ignore to stay compatible with older iOS versions
* Fix "Card IO" link in full SDK reference https://github.com/stripe/stripe-ios/pull/913

Expand Down
16 changes: 16 additions & 0 deletions Stripe/PublicHeaders/STPSourceParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ NS_ASSUME_NONNULL_BEGIN

/**
Creates params for a card source created from Visa Checkout.
@see https://stripe.com/docs/visa-checkout

@note Creating an STPSource with these params will give you a
source with type == STPSourceTypeCard
Expand All @@ -298,6 +299,21 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (STPSourceParams *)visaCheckoutParamsWithCallId:(NSString *)callId;


/**
Creates params for a card source created from Masterpass.
@see https://stripe.com/docs/masterpass

@note Creating an STPSource with these params will give you a
source with type == STPSourceTypeCard

@param cartId The cartId from a `MCCCheckoutResponse` object.
@param transactionId The transactionid from a `MCCCheckoutResponse` object.
@return An STPSourceParams object populated with the provided values.
*/
+ (STPSourceParams *)masterpassParamsWithCartId:(NSString *)cartId
transactionId:(NSString *)transactionId;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions Stripe/STPSourceParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ + (STPSourceParams *)visaCheckoutParamsWithCallId:(NSString *)callId {
return params;
}

+ (STPSourceParams *)masterpassParamsWithCartId:(NSString *)cartId
transactionId:(NSString *)transactionId {
STPSourceParams *params = [self new];
params.type = STPSourceTypeCard;
params.additionalAPIParameters = @{ @"card": @{
@"masterpass": @{
@"cart_id": cartId,
@"transaction_id": transactionId,
}
}

};
return params;
}

#pragma mark - Redirect Dictionary

/**
Expand Down
56 changes: 56 additions & 0 deletions Tests/Tests/STPSourceFunctionalTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ @interface STPSourceFunctionalTest : XCTestCase

@end

@interface STPAPIClient (WritableURL)
@property (nonatomic, readwrite) NSURL *apiURL;
@end

@implementation STPSourceFunctionalTest

- (void)testCreateSource_bancontact {
Expand Down Expand Up @@ -295,6 +299,58 @@ - (void)testCreateSource_threeDSecure {
[self waitForExpectationsWithTimeout:5.0f handler:nil];
}

- (void)skip_testCreateSourceVisaCheckout {
// The SDK does not have a means of generating Visa Checkout params for testing. Supply your own
// callId, and the correct publishable key, and you can run this test case
// manually after removing the `skip_` prefix. It'll log the source's stripeID, and that
// can be verified in dashboard.
STPSourceParams *params = [STPSourceParams visaCheckoutParamsWithCallId:@""];
STPAPIClient *client = [[STPAPIClient alloc] initWithPublishableKey:@"pk_"];
client.apiURL = [NSURL URLWithString:@"https://api.stripe.com/v1"];

XCTestExpectation *sourceExp = [self expectationWithDescription:@"VCO source created"];
[client createSourceWithParams:params completion:^(STPSource * _Nullable source, NSError * _Nullable error) {
[sourceExp fulfill];

XCTAssertNil(error);
XCTAssertNotNil(source);
XCTAssertEqual(source.type, STPSourceTypeCard);
XCTAssertEqual(source.flow, STPSourceFlowNone);
XCTAssertEqual(source.status, STPSourceStatusChargeable);
XCTAssertEqual(source.usage, STPSourceUsageReusable);
XCTAssertTrue([source.stripeID hasPrefix:@"src_"]);
NSLog(@"Created a VCO source %@", source.stripeID);
}];

[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)skip_testCreateSourceMasterpass {
// The SDK does not have a means of generating Masterpass params for testing. Supply your own
// cartId & transactionId, and the correct publishable key, and you can run this test case
// manually after removing the `skip_` prefix. It'll log the source's stripeID, and that
// can be verified in dashboard.
STPSourceParams *params = [STPSourceParams masterpassParamsWithCartId:@"" transactionId:@""];
STPAPIClient *client = [[STPAPIClient alloc] initWithPublishableKey:@"pk_"];
client.apiURL = [NSURL URLWithString:@"https://api.stripe.com/v1"];

XCTestExpectation *sourceExp = [self expectationWithDescription:@"Masterpass source created"];
[client createSourceWithParams:params completion:^(STPSource * _Nullable source, NSError * _Nullable error) {
[sourceExp fulfill];

XCTAssertNil(error);
XCTAssertNotNil(source);
XCTAssertEqual(source.type, STPSourceTypeCard);
XCTAssertEqual(source.flow, STPSourceFlowNone);
XCTAssertEqual(source.status, STPSourceStatusChargeable);
XCTAssertEqual(source.usage, STPSourceUsageSingleUse);
XCTAssertTrue([source.stripeID hasPrefix:@"src_"]);
NSLog(@"Created a Masterpass source %@", source.stripeID);
}];

[self waitForExpectationsWithTimeout:10.0 handler:nil];
}

- (void)testCreateSource_alipay {
STPSourceParams *params = [STPSourceParams alipayParamsWithAmount:1099
currency:@"usd"
Expand Down
24 changes: 24 additions & 0 deletions Tests/Tests/STPSourceParamsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ - (void)testCardParamsWithCard {
XCTAssertEqualObjects(sourceAddress[@"country"], card.address.country);
}

- (void)testParamsWithVisaCheckout {
STPSourceParams *params = [STPSourceParams visaCheckoutParamsWithCallId:@"12345678"];

XCTAssertEqual(params.type, STPSourceTypeCard);
NSDictionary *sourceCard = params.additionalAPIParameters[@"card"];
XCTAssertNotNil(sourceCard);
NSDictionary *sourceVisaCheckout = sourceCard[@"visa_checkout"];
XCTAssertNotNil(sourceVisaCheckout);
XCTAssertEqualObjects(sourceVisaCheckout[@"callid"], @"12345678");
}

- (void)testParamsWithMasterPass {
STPSourceParams *params = [STPSourceParams masterpassParamsWithCartId:@"12345678"
transactionId:@"87654321"];

XCTAssertEqual(params.type, STPSourceTypeCard);
NSDictionary *sourceCard = params.additionalAPIParameters[@"card"];
XCTAssertNotNil(sourceCard);
NSDictionary *sourceMasterpass = sourceCard[@"masterpass"];
XCTAssertNotNil(sourceMasterpass);
XCTAssertEqualObjects(sourceMasterpass[@"cart_id"], @"12345678");
XCTAssertEqualObjects(sourceMasterpass[@"transaction_id"], @"87654321");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can be even more strict here and compare the dictionary as a whole:

XCTAssertEqualObjects(params.additionalAPIParameters, @{@"card": {
    @"masterpass": { ... }
  }
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we'll get better assertion failures by flattening it out like this. I was also cribbing off testCardParamsWithCard above

}


#pragma mark - Redirect Dictionary Tests

Expand Down