Created
September 13, 2015 08:33
-
-
Save agentcoops/ee75f1b6a452a4317227 to your computer and use it in GitHub Desktop.
ViewController.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
// | |
// ViewController.swift | |
// testingstripe | |
// | |
// Created by Charles Francis on 5/22/15. | |
// Copyright (c) 2015 Charles Francis. All rights reserved. | |
import UIKit | |
class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBOutlet var inputField: UITextField? | |
@IBOutlet weak var textOutput: UITextField! | |
func consoleOut(text: String) { | |
textOutput.text = text | |
} | |
@IBAction func beginPayment(sender: UIButton) { | |
let request = Stripe.paymentRequestWithMerchantIdentifier(<MERCHANT ID>) | |
let label = "A very nice product." | |
let amount: NSDecimalNumber = NSDecimalNumber(integer: Int(inputField!.text!)!) | |
request!.paymentSummaryItems = [PKPaymentSummaryItem(label: label, amount: amount)] | |
request!.requiredShippingAddressFields = PKAddressField.All | |
if (Stripe.canSubmitPaymentRequest(request)) { | |
let paymentController = STPTestPaymentAuthorizationViewController(paymentRequest: request) | |
paymentController.delegate = self | |
presentViewController(paymentController, animated: true, completion: nil) | |
} | |
} | |
func handlePaymentAuthorizationWithPayment(payment: PKPayment, completion: ((PKPaymentAuthorizationStatus) -> Void)!) { | |
STPAPIClient.sharedClient().createTokenWithPayment( | |
payment, | |
completion: { (token: STPToken?, error: NSError?) in | |
if ((error) != nil) { | |
completion(PKPaymentAuthorizationStatus.Failure) | |
} | |
self.createBackendChargeWithToken(token!, completion: completion) | |
}) | |
} | |
func createBackendChargeWithToken(token: STPToken, completion: ((PKPaymentAuthorizationStatus) -> Void)!) { | |
let url = NSURL(string: "http://localhost:9000/makeCharge") | |
let request = NSMutableURLRequest(URL: url!) | |
request.HTTPMethod = "POST" | |
let body = String(format: "stripeToken=%@&amount=", token.tokenId) | |
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding) | |
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { | |
(response: NSURLResponse?, data: NSData?, error: NSError?) in | |
if (error != nil) { | |
completion(PKPaymentAuthorizationStatus.Failure) | |
} else { | |
self.consoleOut(String(format: "Payment success with: %@", response!.hashValue)) | |
completion(PKPaymentAuthorizationStatus.Success) | |
} | |
}) | |
} | |
func paymentAuthorizationViewController( | |
controller: PKPaymentAuthorizationViewController, | |
didAuthorizePayment payment: PKPayment, | |
completion: ((PKPaymentAuthorizationStatus) -> Void)) { | |
self.handlePaymentAuthorizationWithPayment(payment, completion:completion); | |
} | |
func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) { | |
self.dismissViewControllerAnimated(true, completion: nil) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment