 	var buyNumber = 1
 	var request:SKProductsRequest?
 	var buyAction:(PayResult)->Void = { _ in }
+	var customUserId:String?
 	
-	public func register() {
+	var lastTransId:String = ""
+	var compLock = NSLock()
+	var compQueue = OperationQueue()
+	
+	public static var shareInstance:CLPay = { CLPay() }()
+	
+	public func register() -> Bool {
+		compQueue.maxConcurrentOperationCount = 1
+		
+		if !SKPaymentQueue.canMakePayments() {
+			return false
+		}
+		
 		SKPaymentQueue.default().add(self)
+		return true
 	}
 	
 	deinit{

	public func addPayment(product:SKProduct) {
		let payment = SKMutablePayment(product: product)
		payment.quantity = buyNumber
		SKPaymentQueue.default().add(payment)
	}
	

			case .purchasing :
				print("show no deferred progress", tran.payment.productIdentifier)
			case .deferred:
				print("---show deferred progress", tran.payment.productIdentifier)
			case .failed:
				print("transaction failed ---- \(tran.error)")
				buyAction(PayResult.faild("Pay error"))
				SKPaymentQueue.default().finishTransaction(tran)
			case .purchased:
				compQueue.addOperation { [weak self, tran] in
					self?.doPuchase(tran: tran)
				}
			case .restored:
				print("restored ----", tran.payment.productIdentifier)
				SKPaymentQueue.default().finishTransaction(tran)
			}
		}
	}
	
	func doPuchase(tran:SKPaymentTransaction) {
		compLock.lock()
		print("transId", tran.transactionIdentifier!, self.lastTransId)
		if let transId = tran.transactionIdentifier, transId != self.lastTransId {
			print("set up ", self.lastTransId, transId)
			self.lastTransId = transId
			
			print("purchased ----", transId, tran.payment.productIdentifier)
			let productId = tran.payment.productIdentifier
			buyAction(PayResult.success(productId: productId, transaction: tran))
			SKPaymentQueue.default().finishTransaction(tran)
		}
		compLock.unlock()
	}
}

