-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Sunbit and add its API bindings
- Loading branch information
1 parent
3654e37
commit 4667a69
Showing
25 changed files
with
512 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...ple/Non-Card Payment Examples/Non-Card Payment Examples/SunbitExampleViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// SunbitExampleViewController.swift | ||
// Non-Card Payment Examples | ||
// | ||
// Created by Eric Geniesse on 6/27/24. | ||
// | ||
|
||
import Foundation | ||
import Stripe | ||
import UIKit | ||
|
||
class SunbitExampleViewController: UIViewController { | ||
@objc weak var delegate: ExampleViewControllerDelegate? | ||
var inProgress: Bool = false { | ||
didSet { | ||
navigationController?.navigationBar.isUserInteractionEnabled = !inProgress | ||
payButton.isEnabled = !inProgress | ||
inProgress | ||
? activityIndicatorView.startAnimating() : activityIndicatorView.stopAnimating() | ||
} | ||
} | ||
|
||
// UI | ||
lazy var activityIndicatorView = { | ||
return UIActivityIndicatorView(style: .gray) | ||
}() | ||
lazy var payButton: UIButton = { | ||
let button = UIButton(type: .roundedRect) | ||
button.setTitle("Pay with Sunbit", for: .normal) | ||
button.addTarget(self, action: #selector(didTapPayButton), for: .touchUpInside) | ||
return button | ||
}() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
view.backgroundColor = .white | ||
title = "Sunbit" | ||
[payButton, activityIndicatorView].forEach { subview in | ||
view.addSubview(subview) | ||
subview.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
|
||
let constraints = [ | ||
payButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
payButton.centerYAnchor.constraint(equalTo: view.centerYAnchor), | ||
|
||
activityIndicatorView.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
activityIndicatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor), | ||
] | ||
NSLayoutConstraint.activate(constraints) | ||
} | ||
|
||
@objc func didTapPayButton() { | ||
guard STPAPIClient.shared.publishableKey != nil else { | ||
delegate?.exampleViewController( | ||
self, | ||
didFinishWithMessage: "Please set a Stripe Publishable Key in Constants.m" | ||
) | ||
return | ||
} | ||
inProgress = true | ||
pay() | ||
} | ||
} | ||
|
||
// MARK: - Sunbit | ||
extension SunbitExampleViewController { | ||
@objc func pay() { | ||
// 1. Create an Sunbit PaymentIntent | ||
MyAPIClient.shared().createPaymentIntent( | ||
completion: { (_, clientSecret, error) in | ||
guard let clientSecret = clientSecret else { | ||
self.delegate?.exampleViewController(self, didFinishWithError: error) | ||
return | ||
} | ||
|
||
// 2. Confirm the payment and redirect the user to Sunbit | ||
let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret) | ||
paymentIntentParams.paymentMethodParams = STPPaymentMethodParams( | ||
sunbit: STPPaymentMethodSunbitParams(), | ||
billingDetails: nil, | ||
metadata: nil | ||
) | ||
paymentIntentParams.returnURL = "payments-example://safepay/" | ||
|
||
STPPaymentHandler.shared().confirmPayment( | ||
paymentIntentParams, | ||
with: self | ||
) { (status, _, error) in | ||
switch status { | ||
case .canceled: | ||
self.delegate?.exampleViewController( | ||
self, | ||
didFinishWithMessage: "Cancelled" | ||
) | ||
case .failed: | ||
self.delegate?.exampleViewController(self, didFinishWithError: error) | ||
case .succeeded: | ||
self.delegate?.exampleViewController( | ||
self, | ||
didFinishWithMessage: "Payment successfully created." | ||
) | ||
@unknown default: | ||
fatalError() | ||
} | ||
} | ||
}, | ||
additionalParameters: "supported_payment_methods=sunbit" | ||
) | ||
} | ||
} | ||
|
||
extension SunbitExampleViewController: STPAuthenticationContext { | ||
func authenticationPresentingViewController() -> UIViewController { | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.