Skip to content

Commit

Permalink
Uses card CVC to complete payment/setup for new or updated Link card (#…
Browse files Browse the repository at this point in the history
…747)

* Pass new card cvc to Link intent completion

* Include cvc updates
  • Loading branch information
csabol-stripe authored Feb 11, 2022
1 parent c624f3e commit ba4f0d2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
14 changes: 8 additions & 6 deletions Stripe/ConsumerSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,17 @@ extension ConsumerSession {
updateParams: updateParams,
completion: completion)
}

func completePayment(with apiClient: STPAPIClient = STPAPIClient.shared,
for paymentIntent: STPPaymentIntent,
paymentDetails: ConsumerPaymentDetails,
completion: @escaping STPPaymentIntentCompletionBlock) {
apiClient.completePayment(for: paymentIntent.stripeId,
paymentIntentClientSecret: paymentIntent.clientSecret,
consumerSessionClientSecret: clientSecret,
paymentDetailsID: paymentDetails.stripeID,
completion: completion)
paymentIntentClientSecret: paymentIntent.clientSecret,
consumerSessionClientSecret: clientSecret,
paymentDetailsID: paymentDetails.stripeID,
cvc: paymentDetails.cvc,
completion: completion)
}

func completeSetup(with apiClient: STPAPIClient = STPAPIClient.shared,
Expand All @@ -294,9 +295,10 @@ extension ConsumerSession {
setupIntentClientSecret: setupIntent.clientSecret,
consumerSessionClientSecret: clientSecret,
paymentDetailsID: paymentDetails.stripeID,
cvc: paymentDetails.cvc,
completion: completion)
}


func logout(
with apiClient: STPAPIClient = STPAPIClient.shared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ extension PayWithLinkViewController {
}
if let paymentDetails = paymentDetails {

if case .card(let card) = paymentDetails.details {
card.cvc = confirmParams.paymentMethodParams.card?.cvc
}

self.coordinator?.confirm(with: self.linkAccount,
paymentDetails: paymentDetails,
completion: { [weak self] result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ extension PayWithLinkViewController {

switch result {
case .success(let updatedPaymentDetails):
// Updates to CVC only get applied when the intent is confirmed so we manually add them here
// instead of including in the /update API call
if let cvc = paymentMethodParams.card?.cvc,
case .card(let card) = updatedPaymentDetails.details {
card.cvc = cvc
}

self?.updateButton.update(state: .succeeded, style: nil, callToAction: nil, animated: true) {
self?.delegate?.didUpdate(paymentMethod: updatedPaymentDetails)
self?.navigationController?.popViewController(animated: true)
Expand Down
12 changes: 12 additions & 0 deletions Stripe/PaymentDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ extension ConsumerPaymentDetails.Details {

let allResponseFields: [AnyHashable : Any]

/// A frontend convenience property, i.e. not part of the API Object
var cvc: String? = nil

required init(expiryYear: Int,
expiryMonth: Int,
brand: String,
Expand Down Expand Up @@ -244,6 +247,15 @@ extension ConsumerPaymentDetails {
return nil
}
}

var cvc: String? {
switch details {
case .card(let card):
return card.cvc
case .bankAccount:
return nil
}
}

var accessibilityDescription: String {
switch details {
Expand Down
14 changes: 12 additions & 2 deletions Stripe/STPAPIClient+Link.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,20 @@ extension STPAPIClient {
paymentIntentClientSecret: String,
consumerSessionClientSecret: String,
paymentDetailsID: String,
cvc: String?,
completion: @escaping STPPaymentIntentCompletionBlock) {
let endpoint: String = "consumers/payment_intents/\(paymentIntentID)/complete"

let parameters: [String: Any] = [
var parameters: [String: Any] = [
"credentials": ["consumer_session_client_secret": consumerSessionClientSecret],
"client_secret": paymentIntentClientSecret,
"payment_details_id": paymentDetailsID
]

if let cvc = cvc {
parameters["payment_method_options"] = ["card": ["cvc": cvc]]
}

APIRequest<STPPaymentIntent>.post(
with: self,
endpoint: endpoint,
Expand All @@ -343,15 +348,20 @@ extension STPAPIClient {
setupIntentClientSecret: String,
consumerSessionClientSecret: String,
paymentDetailsID: String,
cvc: String?,
completion: @escaping STPSetupIntentCompletionBlock) {
let endpoint: String = "consumers/setup_intents/\(setupIntentID)/complete"

let parameters: [String: Any] = [
var parameters: [String: Any] = [
"credentials": ["consumer_session_client_secret": consumerSessionClientSecret],
"client_secret": setupIntentClientSecret,
"payment_details_id": paymentDetailsID
]

if let cvc = cvc {
parameters["payment_method_options"] = ["card": ["cvc": cvc]]
}

APIRequest<STPSetupIntent>.post(
with: self,
endpoint: endpoint,
Expand Down

0 comments on commit ba4f0d2

Please sign in to comment.