From 935c69baf2788fda5cf0023752e2d671045b003c Mon Sep 17 00:00:00 2001 From: Yuki Tokuhiro Date: Wed, 30 Oct 2019 17:03:34 -0700 Subject: [PATCH] Add STPAPIClient method to convert ssn_last_4 to a Token --- Stripe/PublicHeaders/STPAPIClient.h | 8 ++++++ Stripe/STPAPIClient.m | 8 +++++- Tests/Tests/STPPIIFunctionalTest.m | 16 +++++++++++ .../post_v1_tokens_0.tail | 14 +++++----- .../testSSNLast4Token/post_v1_tokens_0.tail | 28 +++++++++++++++++++ 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 Tests/recorded_network_traffic/STPPIIFunctionalTest/testSSNLast4Token/post_v1_tokens_0.tail diff --git a/Stripe/PublicHeaders/STPAPIClient.h b/Stripe/PublicHeaders/STPAPIClient.h index ac699ab90d7..bc3180cf072 100644 --- a/Stripe/PublicHeaders/STPAPIClient.h +++ b/Stripe/PublicHeaders/STPAPIClient.h @@ -134,6 +134,14 @@ static NSString *const STPSDKVersion = @"18.1.0"; */ - (void)createTokenWithPersonalIDNumber:(NSString *)pii completion:(__nullable STPTokenCompletionBlock)completion; +/** +Converts the last 4 SSN digits into a Stripe token using the Stripe API. + +@param ssnLast4 The last 4 digits of the user's SSN. Cannot be nil. +@param completion The callback to run with the returned Stripe token (and any errors that may have occurred). +*/ +- (void)createTokenWithSSNLast4:(NSString *)ssnLast4 completion:(STPTokenCompletionBlock)completion; + @end #pragma mark Connect Accounts diff --git a/Stripe/STPAPIClient.m b/Stripe/STPAPIClient.m index c78888d5a53..72b616a0959 100644 --- a/Stripe/STPAPIClient.m +++ b/Stripe/STPAPIClient.m @@ -277,9 +277,15 @@ - (void)createTokenWithPersonalIDNumber:(NSString *)pii completion:(__nullable S NSMutableDictionary *params = [@{@"pii": @{ @"personal_id_number": pii }} mutableCopy]; [[STPTelemetryClient sharedInstance] addTelemetryFieldsToParams:params]; [self createTokenWithParameters:params completion:completion]; + [[STPTelemetryClient sharedInstance] sendTelemetryData];} + +- (void)createTokenWithSSNLast4:(NSString *)ssnLast4 completion:(STPTokenCompletionBlock)completion { + NSMutableDictionary *params = [@{@"pii": @{ @"ssn_last_4": ssnLast4 }} mutableCopy]; + [[STPTelemetryClient sharedInstance] addTelemetryFieldsToParams:params]; + [self createTokenWithParameters:params completion:completion]; [[STPTelemetryClient sharedInstance] sendTelemetryData]; } - + @end #pragma mark - Connect Accounts diff --git a/Tests/Tests/STPPIIFunctionalTest.m b/Tests/Tests/STPPIIFunctionalTest.m index be7913088bc..7d7def95b03 100644 --- a/Tests/Tests/STPPIIFunctionalTest.m +++ b/Tests/Tests/STPPIIFunctionalTest.m @@ -38,4 +38,20 @@ - (void)testCreatePersonallyIdentifiableInformationToken { [self waitForExpectationsWithTimeout:5.0f handler:nil]; } +- (void)testSSNLast4Token { + STPAPIClient *client = [[STPAPIClient alloc] initWithPublishableKey:@"pk_test_vOo1umqsYxSrP5UXfOeL3ecm"]; + + XCTestExpectation *expectation = [self expectationWithDescription:@"PII creation"]; + + [client createTokenWithSSNLast4:@"1234" completion:^(STPToken * _Nullable token, NSError * _Nullable error) { + [expectation fulfill]; + XCTAssertNil(error, @"error should be nil %@", error.localizedDescription); + XCTAssertNotNil(token, @"token should not be nil"); + XCTAssertNotNil(token.tokenId); + XCTAssertEqual(token.type, STPTokenTypePII); + }]; + + [self waitForExpectationsWithTimeout:5.0f handler:nil]; +} + @end diff --git a/Tests/recorded_network_traffic/STPPIIFunctionalTest/testCreatePersonallyIdentifiableInformationToken/post_v1_tokens_0.tail b/Tests/recorded_network_traffic/STPPIIFunctionalTest/testCreatePersonallyIdentifiableInformationToken/post_v1_tokens_0.tail index 0d3bb224591..7e13ee800e3 100644 --- a/Tests/recorded_network_traffic/STPPIIFunctionalTest/testCreatePersonallyIdentifiableInformationToken/post_v1_tokens_0.tail +++ b/Tests/recorded_network_traffic/STPPIIFunctionalTest/testCreatePersonallyIdentifiableInformationToken/post_v1_tokens_0.tail @@ -4,25 +4,25 @@ POST application/json Content-Type: application/json Access-Control-Allow-Origin: * -access-control-allow-methods: GET, POST, HEAD, OPTIONS, DELETE Server: nginx +access-control-allow-methods: GET, POST, HEAD, OPTIONS, DELETE access-control-expose-headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required access-control-max-age: 300 Cache-Control: no-cache, no-store -Date: Wed, 24 Jul 2019 23:23:07 GMT +Date: Wed, 30 Oct 2019 23:56:38 GMT stripe-version: 2019-05-16 access-control-allow-credentials: true Content-Length: 175 -Strict-Transport-Security: max-age=31556926; includeSubDomains; preload Connection: keep-alive -request-id: req_HTETf1BQbwtL3v +Strict-Transport-Security: max-age=31556926; includeSubDomains; preload +request-id: req_G0mrDBE9JwOmuv { "object" : "token", - "id" : "pii_1EztnbBbvEcIpqUbJEDM98Z2", + "id" : "pii_1FZR1mBbvEcIpqUbR2ADMXtG", "livemode" : false, - "client_ip" : "8.21.168.117", - "created" : 1564010587, + "client_ip" : "8.21.168.115", + "created" : 1572479798, "used" : false, "type" : "pii" } \ No newline at end of file diff --git a/Tests/recorded_network_traffic/STPPIIFunctionalTest/testSSNLast4Token/post_v1_tokens_0.tail b/Tests/recorded_network_traffic/STPPIIFunctionalTest/testSSNLast4Token/post_v1_tokens_0.tail new file mode 100644 index 00000000000..c4754bf0ed3 --- /dev/null +++ b/Tests/recorded_network_traffic/STPPIIFunctionalTest/testSSNLast4Token/post_v1_tokens_0.tail @@ -0,0 +1,28 @@ +POST +/v1/tokens$ +200 +application/json +Content-Type: application/json +Access-Control-Allow-Origin: * +Server: nginx +access-control-allow-methods: GET, POST, HEAD, OPTIONS, DELETE +access-control-expose-headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required +access-control-max-age: 300 +Cache-Control: no-cache, no-store +Date: Wed, 30 Oct 2019 23:56:38 GMT +stripe-version: 2019-05-16 +access-control-allow-credentials: true +Content-Length: 175 +Connection: keep-alive +Strict-Transport-Security: max-age=31556926; includeSubDomains; preload +request-id: req_TqZBbf0VIoU6HU + +{ + "object" : "token", + "id" : "pii_1FZR1mBbvEcIpqUbmYvOI5c1", + "livemode" : false, + "client_ip" : "8.21.168.115", + "created" : 1572479798, + "used" : false, + "type" : "pii" +} \ No newline at end of file