little fixes

This commit is contained in:
Nora Trapp 2021-05-13 09:32:21 -07:00
parent db940e8e5a
commit 5bf94de2a6
7 changed files with 53 additions and 22 deletions

View File

@ -9,23 +9,26 @@ protocol CurrencyPickerDataSource {
var preferredCurrencyInfos: [Currency.Info] { get }
var supportedCurrencyInfos: [Currency.Info] { get }
init(updateTableContentsBlock: @escaping () -> Void)
var updateTableContents: (() -> Void)? { get set }
}
class CurrencyPickerViewController<DataSourceType: CurrencyPickerDataSource>: OWSTableViewController2, UISearchBarDelegate {
private let searchBar = OWSSearchBar()
private lazy var dataSource = DataSourceType { [weak self] in self?.updateTableContents() }
private var dataSource: DataSourceType
private let completion: (Currency.Code) -> Void
fileprivate var searchText: String? {
searchBar.text?.ows_stripped()
}
public required init(completion: @escaping (Currency.Code) -> Void) {
public required init(dataSource: DataSourceType, completion: @escaping (Currency.Code) -> Void) {
self.dataSource = dataSource
self.completion = completion
super.init()
self.dataSource.updateTableContents = { [weak self] in self?.updateTableContents() }
topHeader = OWSTableViewController2.buildTopHeader(forView: searchBar)
}
@ -210,24 +213,28 @@ class CurrencyPickerViewController<DataSourceType: CurrencyPickerDataSource>: OW
}
}
struct CurrencyPickerStripeDataSource: CurrencyPickerDataSource {
let currentCurrencyCode = Stripe.defaultCurrencyCode
struct StripeCurrencyPickerDataSource: CurrencyPickerDataSource {
let currentCurrencyCode: Currency.Code
let preferredCurrencyInfos = Stripe.preferredCurrencyInfos
let supportedCurrencyInfos = Stripe.supportedCurrencyInfos
init(updateTableContentsBlock: @escaping () -> Void) {}
var updateTableContents: (() -> Void)?
init(currentCurrencyCode: Currency.Code = Stripe.defaultCurrencyCode) {
self.currentCurrencyCode = currentCurrencyCode
}
}
class CurrencyPickerPaymentsDataSource: NSObject, CurrencyPickerDataSource {
class PaymentsCurrencyPickerDataSource: NSObject, CurrencyPickerDataSource {
let currentCurrencyCode = paymentsCurrenciesSwift.currentCurrencyCode
let preferredCurrencyInfos = paymentsCurrenciesSwift.preferredCurrencyInfos
private(set) var supportedCurrencyInfos = paymentsCurrenciesSwift.supportedCurrencyInfosWithCurrencyConversions {
didSet { updateTableContents() }
didSet { updateTableContents?() }
}
let updateTableContents: () -> Void
required init(updateTableContentsBlock: @escaping () -> Void) {
self.updateTableContents = updateTableContentsBlock
var updateTableContents: (() -> Void)?
override init() {
super.init()
NotificationCenter.default.addObserver(

View File

@ -360,7 +360,9 @@ extension DonationViewController: PKPaymentAuthorizationControllerDelegate {
let picker = OWSButton { [weak self] in
guard let self = self else { return }
let vc = CurrencyPickerViewController<CurrencyPickerStripeDataSource> { [weak self] currencyCode in
let vc = CurrencyPickerViewController(
dataSource: StripeCurrencyPickerDataSource(currentCurrencyCode: self.currencyCode)
) { [weak self] currencyCode in
self?.currencyCode = currencyCode
}
self.navigationController?.pushViewController(vc, animated: true)

View File

@ -949,7 +949,9 @@ public class PaymentsSettingsViewController: OWSTableViewController2 {
}
private func didTapSetCurrencyButton() {
let view = CurrencyPickerViewController<CurrencyPickerPaymentsDataSource> { currencyCode in
let view = CurrencyPickerViewController(
dataSource: PaymentsCurrencyPickerDataSource()
) { currencyCode in
Self.databaseStorage.write { transaction in
Self.paymentsCurrencies.setCurrentCurrencyCode(currencyCode, transaction: transaction)
}

View File

@ -30,11 +30,16 @@ struct Stripe: Dependencies {
}
static func integralAmount(_ amount: NSDecimalNumber, in currencyCode: Currency.Code) -> UInt {
let roundedAndScaledAmount: Double
if zeroDecimalCurrencyCodes.contains(currencyCode.uppercased()) {
return UInt(amount.doubleValue.rounded(.toNearestOrEven))
roundedAndScaledAmount = amount.doubleValue.rounded(.toNearestOrEven)
} else {
return UInt((amount.doubleValue * 100).rounded(.toNearestOrEven))
roundedAndScaledAmount = (amount.doubleValue * 100).rounded(.toNearestOrEven)
}
guard roundedAndScaledAmount <= Double(UInt.max) else { return UInt.max }
guard roundedAndScaledAmount >= 0 else { return 0 }
return UInt(roundedAndScaledAmount)
}
}
@ -72,9 +77,12 @@ fileprivate extension Stripe {
throw OWSAssertionError("Unexpected currency code")
}
// The description is never translated as it's populated into an
// english only receipt by Stripe.
let request = OWSRequestFactory.createPaymentIntent(
withAmount: integralAmount(amount, in: currencyCode),
inCurrencyCode: currencyCode
inCurrencyCode: currencyCode,
withDescription: LocalizationNotNeeded("Thank you for your donation. Your contribution helps fuel the mission of developing open source privacy technology that protects free expression and enables secure global communication for millions around the world. Signal Technology Foundation is a tax-exempt nonprofit organization in the United States under section 501c3 of the Internal Revenue Code. Our Federal Tax ID is 82-4506840. No goods or services were provided in exchange for this donation. Please retain this receipt for your tax records.")
)
return networkManager.makePromise(request: request)
@ -149,6 +157,7 @@ fileprivate extension Stripe {
parameters["pk_token_payment_network"] = payment.token.paymentMethod.network.map { $0.rawValue }
if payment.token.transactionIdentifier == "Simulated Identifier" {
owsAssertDebug(!FeatureFlags.isUsingProductionService, "Simulated ApplePay only works in staging")
// Generate a fake transaction identifier
parameters["pk_token_transaction_id"] = "ApplePayStubs~4242424242424242~0~USD~\(UUID().uuidString)"
} else {

View File

@ -197,9 +197,11 @@ typedef NS_ENUM(NSUInteger, TSVerificationTransport) { TSVerificationTransportVo
+ (TSRequest *)pushChallengeResponseWithToken:(NSString *)challengeToken;
+ (TSRequest *)recaptchChallengeResponseWithToken:(NSString *)serverToken captchaToken:(NSString *)captchaToken;
#pragma mark - Stripe
#pragma mark - Donations
+ (TSRequest *)createPaymentIntentWithAmount:(NSUInteger)amount inCurrencyCode:(NSString *)currencyCode;
+ (TSRequest *)createPaymentIntentWithAmount:(NSUInteger)amount
inCurrencyCode:(NSString *)currencyCode
withDescription:(nullable NSString *)description;
@end

View File

@ -878,13 +878,21 @@ NSString *const OWSRequestKey_AuthKey = @"AuthKey";
parameters:@{ @"type" : @"recaptcha", @"token" : serverToken, @"captcha" : captchaToken }];
}
#pragma mark - Stripe
#pragma mark - Donations
+ (TSRequest *)createPaymentIntentWithAmount:(NSUInteger)amount inCurrencyCode:(NSString *)currencyCode
+ (TSRequest *)createPaymentIntentWithAmount:(NSUInteger)amount
inCurrencyCode:(NSString *)currencyCode
withDescription:(nullable NSString *)description
{
NSMutableDictionary *parameters =
[@{ @"currency" : currencyCode.lowercaseString, @"amount" : @(amount) } mutableCopy];
if (description) {
parameters[@"description"] = description;
}
return [TSRequest requestWithUrl:[NSURL URLWithString:@"/v1/donation/authorize-apple-pay"]
method:@"POST"
parameters:@{ @"currency" : currencyCode.lowercaseString, @"amount" : @(amount) }];
parameters:parameters];
}
@end

View File

@ -449,6 +449,7 @@ dispatch_queue_t NetworkManagerQueue()
NSString *_Nullable contentType = task.originalRequest.allHTTPHeaderFields[@"Content-Type"];
BOOL isJson = [contentType isEqualToString:OWSMimeTypeJson];
BOOL isProtobuf = [contentType isEqualToString:@"application/x-protobuf"];
BOOL isFormData = [contentType isEqualToString:@"application/x-www-form-urlencoded"];
if (isJson) {
NSString *jsonBody = [[NSString alloc] initWithData:task.originalRequest.HTTPBody
encoding:NSUTF8StringEncoding];
@ -457,7 +458,7 @@ dispatch_queue_t NetworkManagerQueue()
OWSAssertDebug([jsonBody rangeOfString:@"'"].location == NSNotFound);
[curlComponents addObject:@"--data-ascii"];
[curlComponents addObject:[NSString stringWithFormat:@"'%@'", jsonBody]];
} else if (isProtobuf) {
} else if (isProtobuf || isFormData) {
NSData *bodyData = task.originalRequest.HTTPBody;
NSString *filename = [NSString stringWithFormat:@"%@.tmp", NSUUID.UUID.UUIDString];