Created
November 24, 2016 07:07
-
-
Save Manny7311/ea87d842f1c308dc7fc6504298e6589a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use latest'; | |
import express from 'express'; | |
import { fromExpress } from 'webtask-tools'; | |
import bodyParser from 'body-parser'; | |
import stripe from 'stripe'; | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.post('/payment', (req,res) => { | |
var ctx = req.webtaskContext; | |
var STRIPE_SECRET_KEY = ctx.secrets.STRIPE_SECRET_KEY; | |
stripe(STRIPE_SECRET_KEY).charges.create({ | |
amount: ctx.data.amount, | |
currency: ctx.data.currency, | |
source: ctx.body.stripeToken, | |
description: ctx.data.description | |
}, (err, charge) => { | |
const status = err ? 400 : 200; | |
const message = err ? err.message : 'Payment done!'; | |
res.JSON({message:"Success"}); | |
return res.end(message); | |
}); | |
}); | |
module.exports = fromExpress(app); |
This file contains hidden or 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 | |
// Stripe-SwiftSample | |
// | |
// Created by Karthik Murugesan on 3/15/15. | |
// Copyright (c) 2015 Karthik. All rights reserved. | |
// | |
import UIKit | |
import Stripe | |
import PassKit | |
import Alamofire | |
class PaymentViewController: UIViewController, STPCheckoutViewControllerDelegate,PKPaymentAuthorizationViewControllerDelegate { | |
// Find this at https://dashboard.stripe.com/account/apikeys | |
let stripePublishableKey = "pk_test_Dpny9s7K2DPlv1qqoEhuRuaH" | |
// To set this up, see https://github.com/stripe/example-ios-backend | |
let backendChargeURLString = "https://wt-lightfalldev-gmail-com-0.run.webtask.io/stripe-payment?webtask_no_cache=1" | |
// To set this up, see https://stripe.com/docs/mobile/apple-pay | |
let appleMerchantId = "" | |
let heads = ["Success": "application/json"] | |
let shirtPrice : UInt = 1000 // this is in cents | |
//@IBAction func beginPayment(_ sender: AnyObject) { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if (stripePublishableKey == "") { | |
let alert = UIAlertController( | |
title: "You need to set your Stripe publishable key.", | |
message: "You can find your publishable key at https://dashboard.stripe.com/account/apikeys .", | |
preferredStyle: UIAlertControllerStyle.alert | |
) | |
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) | |
alert.addAction(action) | |
present(alert, animated: true, completion: nil) | |
return | |
} | |
if (appleMerchantId != "") { | |
let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier: appleMerchantId) | |
if Stripe.canSubmitPaymentRequest(paymentRequest) { | |
paymentRequest?.paymentSummaryItems = [PKPaymentSummaryItem(label: "Cool shirt", amount: NSDecimalNumber(string: "10.00")), PKPaymentSummaryItem(label: "Stripe shirt shop", amount: NSDecimalNumber(string: "10.00"))] | |
let paymentAuthVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest!) | |
paymentAuthVC.delegate = self | |
present(paymentAuthVC, animated: true, completion: nil) | |
return | |
} | |
} | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
let options = STPCheckoutOptions() | |
options.publishableKey = stripePublishableKey | |
options.companyName = "Revamp Barbershop" | |
options.purchaseDescription = "Adult Haircut" | |
options.purchaseAmount = shirtPrice | |
options.logoColor = UIColor.black | |
let checkoutViewController = STPCheckoutViewController(options: options) | |
checkoutViewController?.checkoutDelegate = self | |
present(checkoutViewController!, animated: true, completion: nil) | |
} | |
func checkoutController(_ controller: STPCheckoutViewController!, didCreateToken token: STPToken!, completion: STPTokenSubmissionHandler!) { | |
createBackendChargeWithToken(token, completion: completion) | |
} | |
func checkoutController(_ controller: STPCheckoutViewController!, didFinishWith status: STPPaymentStatus, error: Error!) { | |
dismiss(animated: true, completion: { | |
switch(status) { | |
case .userCancelled: | |
return // just do nothing in this case | |
case .success: | |
print("great success!") | |
case .error: | |
print("oh no, an error: \(error.localizedDescription)") | |
} | |
}) | |
} | |
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping ((PKPaymentAuthorizationStatus) -> Void)) { | |
let apiClient = STPAPIClient(publishableKey: stripePublishableKey) | |
apiClient?.createToken(with: payment, completion: { (token, error) -> Void in | |
self.createBackendChargeWithToken(token!, completion: { (result, error) -> Void in | |
if result == STPBackendChargeResult.success { | |
completion(PKPaymentAuthorizationStatus.success) | |
} else { | |
completion(PKPaymentAuthorizationStatus.failure) | |
} | |
}) | |
}) | |
} | |
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) { | |
dismiss(animated: true, completion: nil) | |
} | |
func createBackendChargeWithToken(_ token: STPToken, completion: @escaping STPTokenSubmissionHandler) { | |
if backendChargeURLString != "" { | |
if let url = URL(string: "https://wt-lightfalldev-gmail-com-0.run.webtask.io/stripe-payment/payment") { | |
let chargeParams : [String: AnyObject] = ["stripeToken": token.tokenId as AnyObject, "amount": shirtPrice as AnyObject] | |
Alamofire.request(url, method: .post, parameters: chargeParams, encoding: JSONEncoding.default, headers: heads).responseJSON { (response:DataResponse<Any>) in | |
switch(response.result) { | |
case .success(_): | |
if response.result.value != nil{ | |
print(response.result.value!) | |
} | |
break | |
case .failure(_): | |
print(response.result.error!) | |
break | |
} | |
} | |
} | |
} | |
completion(STPBackendChargeResult.failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "You created a token! Its value is \(token.tokenId). Now configure your backend to accept this token and complete a charge."])) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment