import UIKit
class
ViewController: UIViewController {
override
func viewDidLoad() {
super.viewDidLoad()
let
but = UIButton(type: UIButtonType.System);
but.setTitle("
支付", forState: UIControlState.Normal);
but.backgroundColor = UIColor.greenColor();
but.frame = CGRect(x: 10, y: 100, width: 100, height: 30);
but.addTarget(self, action:
"click"
, forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(but); }
func click()
{
AliplayFunc();
print(
"click"
)
}
var
TicketTotalprice:Float=0.01;
var
seller:String=
"支付宝申请的seller"
;
var
partner:String=
"支付宝申请的partner"
;
var
privateKey:String =
"替换支付申请的privet_key"
;
func AliplayFunc(){
let
Orders = Order()
Orders.partner = partner
Orders.seller = seller
Orders.productName =
"ProductName"
;
Orders.productDescription =
"this is a goods"
;
Orders.amount = NSString(format:
"%.2f"
,TicketTotalprice)
as
String ;
Orders.tradeNO =
"DJ0000000001"
;
Orders.notifyURL =
"http://selftweb.com"
;
Orders.service =
"mobile.securitypay.pay"
;
Orders.paymentType =
"1"
;
Orders.inputCharset =
"utf-8"
;
Orders.itBPay =
"30m"
;
Orders.showUrl =
"m.alipay.com"
;
let
appScheme =
"GSWAPayDemo"
;
let
orderSpec = Orders.description;
let
signer = CreateRSADataSigner(privateKey);
let
signedString = signer.signString(orderSpec);
let
orderString =
"\(orderSpec)&sign=\"\(signedString)\"&sign_type=\"RSA\""
;
AlipaySDK.defaultService().payOrder(orderString, fromScheme: appScheme, callback: { (resultDic) -> Void
in
print(
"reslut = \(resultDic)"
);
if
let
Alipayjson = resultDic
as
? NSDictionary{
let
resultStatus = Alipayjson.valueForKey(
"resultStatus"
)
as
! String
if
resultStatus ==
"9000"
{
print(
"OK"
)
}
else
if
resultStatus ==
"8000"
{
print(
"正在处理中"
)
self.navigationController?.popViewControllerAnimated(
true
)
}
else
if
resultStatus ==
"4000"
{
print(
"订单支付失败"
);
self.navigationController?.popViewControllerAnimated(
true
)
}
else
if
resultStatus ==
"6001"
{
print(
"用户中途取消"
)
self.navigationController?.popViewControllerAnimated(
true
)
}
else
if
resultStatus ==
"6002"
{
print(
"网络连接出错"
)
self.navigationController?.popViewControllerAnimated(
true
)
}
}
})
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}