#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <StoreKit/StoreKit.h>
#import <WebKit/WKWebView.h>
@interface TXObjectLoginPay : NSObject<SKPaymentTransactionObserver,SKProductsRequestDelegate>{
TXObjectLoginPay* objectLoginPay;
NSString* mReceiptData;
NSString* mPayAddress;
int mRechargeCfgId;
NSString* mAmount;
int mServerId;
NSString* mUserId;
int mRoleId;
int mUid;
WKWebView *wkwebView1;
}
-
(void)initWebview:(WKWebView *)webView;
-
(void)onObjC_IOS_InitPay:(void *) iosurl;
-(void)onObjC_IOSPay:
(NSString *)rechargeCfgId
secondKid: (NSString *) amount
secondKid1: (NSNumber *) serverId
secondKid2: (NSString *) userId
secondKid3: (NSNumber *) roleId
secondKid4: (NSNumber *) uid;
@end
#import "TXObjectLoginPay.h"
#include "YLWwiseEngine.h"
#include "PostObj.h"
//#import <AuthenticationServices/AuthenticationServices.h>
#import <StoreKit/StoreKit.h>
//登录借口
@interface TXObjectLoginPay ()
@property (nonatomic, copy) NSString *userId2; // 声明为成员变量
@end
@implementation TXObjectLoginPay
- (void)initWebview:(WKWebView *)webView {
wkwebView1=webView;
const char *str = "Hello";
char *strPtr = const_cast<char *>(str);
self onObjC_IOS_InitPay:strPtr;
}
// 1.支付初始化,进入游戏就开始初始化
- (void)onObjC_IOS_InitPay:(void *) iosurl{
char* pChar = (char*)iosurl;
NSString *str2 = NSString stringWithUTF8String:pChar;
\[SKPaymentQueue defaultQueue addTransactionObserver:self];
}
-(void)onObjC_IOSPay:(NSString *)rechargeCfgId
secondKid: (NSString *) amount
secondKid1: (NSNumber *) serverId
secondKid2: (NSString *) userId
secondKid3: (NSNumber *) roleId
secondKid4: (NSNumber *) uid
{
NSString *product = rechargeCfgId;
NSString* log1 =@"--1.pull recharge---";
self PrintIOSLog:log1;
self PrintIOSLog:product;
self.userId2 = userId;
NSLog(@"--------userId2:%@", self.userId2);
//NSString *product = NSString stringWithFormat:@"%d",rechargeCfgId;
NSLog(@"--------product:%@", product);
NSArray *product1 = \[NSArray alloc initWithObjects:product, nil];
NSLog(@"--------product1:%@", product1);
NSSet *nsset = NSSet setWithArray:product1;
SKProductsRequest *request = \[SKProductsRequest alloc initWithProductIdentifiers:nsset];
request.delegate = self;
request start;
}
//请求对应的产品信息
- (void)requestProductData:(NSString *)type
{
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *product = response.products;
if(product count == 0)
{
NSLog(@"--------------没有商品------------------");
NSString* log1 =@"--2.no product---";
self PrintIOSLog:log1;
return;
}
NSLog(@"productID:%@", response.invalidProductIdentifiers);
NSLog(@"产品付费数量:%d",(int)product count);
NSString* log1 =@"--3.product---";
self PrintIOSLog:log1;
for (SKProduct *pro in product)
{
NSLog(@"desc:%@", pro description);
NSLog(@"ltitle:%@", pro localizedTitle);
NSLog(@"ldesc:%@", pro localizedDescription);
NSLog(@"price:%@", pro price);
NSLog(@"identifier:%@", pro productIdentifier);
NSString* payName =@"--4.收到产品反馈消息---";
NSString *log = NSString stringWithFormat:@"%@%@%@%@%@%@",payName,\[pro description,pro localizedTitle,pro localizedDescription,pro price,pro productIdentifier];
NSString *string1 = @"--4. ";
NSString *string = pro.price stringValue;
NSString *result = string1 stringByAppendingString:string;
self PrintIOSLog:result;
}
// SKPayment *payment = SKPayment paymentWithProduct:product\[0];
// NSString* payName =@"--5.发送购买请求---";
// NSString* payName1 =@"--5.seng buy request---";
// self PrintIOSLog:payName1;
//
// \[SKPaymentQueue defaultQueue addPayment:payment];
NSLog(@"--------productsRequest 中 userId2: %@", self.userId2);
SKMutablePayment *payment = SKMutablePayment paymentWithProduct:product\[0];
// 设置用户关联信息
if (@available(iOS 13.0, *)) {
// 使用 NSInvocation 动态调用方法(更安全)
SEL selector = NSSelectorFromString(@"setAppAccountToken:");
if (payment respondsToSelector:selector) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
payment methodSignatureForSelector:selector];
invocation setTarget:payment;
invocation setSelector:selector;
NSData *tokenData = self.userId2 dataUsingEncoding:NSUTF8StringEncoding;
invocation setArgument:\&tokenData atIndex:2;
invocation invoke;
NSLog(@"成功设置 appAccountToken");
} else {
NSLog(@"---- appAccountToken");
// 回退到使用 applicationUsername
payment.applicationUsername = self.userId2;
}
} else {
// 旧版本使用 applicationUsername
payment.applicationUsername = self.userId2;
}
// 发送支付请求
\[SKPaymentQueue defaultQueue addPayment:payment];
}
//请求失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"------------------错误-----------------:%@", error);
self PrintIOSLog:@"--didFailWithError---";
}
//反馈信息结束
- (void)requestDidFinish:(SKRequest *)request
{
NSString* payName =@"--6.反馈信息结束---";
NSString* payName1 =@"--6.requestDidFinish---";
self PrintIOSLog:payName1;
}
//监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for(SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
{
//交易结束
\[SKPaymentQueue defaultQueue finishTransaction:transaction];
NSURL *receiptUrl=\[NSBundle mainBundle appStoreReceiptURL];
NSData *receiptData=NSData dataWithContentsOfURL:receiptUrl;
NSLog(@"------ios----receipt data: %@", receiptData);
NSString *receiptString=receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed;//转化为base64字符串
NSLog(@"------ios-66666-7--receiptString: %@", receiptString);
// self PrintIOSLog:@"--9.交易结束---";
self PrintIOSLog:@"--9.finish over---";
mReceiptData = receiptString;
NSString *jsFunction = NSString stringWithFormat:@"CccToJS_ApplePay('%s')", mReceiptData.UTF8String;
wkwebView1 evaluateJavaScript:jsFunction completionHandler:nil;
}
break;
case SKPaymentTransactionStatePurchasing:
// self PrintIOSLog:@"--7.商品添加进列表---";
self PrintIOSLog:@"--7.product add list---";
break;
case SKPaymentTransactionStateRestored:
NSLog(@"已经购买过商品");
// self PrintIOSLog:@"------------已经购买过商品------------";
self PrintIOSLog:@"-----------10-SKPaymentTransactionStateRestored------------";
self restoreTransaction:transaction;
break;
case SKPaymentTransactionStateFailed:
NSLog(@"交易失败");
// UnitySendMessage("SdkMessager", "IOSPayCanceled", "");
//self PrintIOSLog:@"------------交易失败------------";
self PrintIOSLog:@"-----------11-SKPaymentTransactionStateFailed------------";
self failedTransaction:transaction;
break;
default:
break;
}
}
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
// ccjsbridge::excuteJS("CCToJsUtils", "CccToJS_ApplePay", "0");
NSString *str2=@"0";
NSString *jsFunction = NSString stringWithFormat:@"CccToJS_ApplePay('%s')", str2.UTF8String;
wkwebView1 evaluateJavaScript:jsFunction completionHandler:nil;
\[SKPaymentQueue defaultQueue finishTransaction: transaction];
}
//交易结束
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
\[SKPaymentQueue defaultQueue finishTransaction:transaction];
}
//购买失败
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
if(transaction.error.code != SKErrorPaymentCancelled)
{
}
else
{
//UnitySendMessage("SdkMessager", "IOSPayCanceled", "");
}
NSString *str2=@"0";
NSString *jsFunction = NSString stringWithFormat:@"CccToJS_ApplePay('%s')", str2.UTF8String;
wkwebView1 evaluateJavaScript:jsFunction completionHandler:nil;
\[SKPaymentQueue defaultQueue finishTransaction: transaction];
}
//验证receipt失败,App启动后再次验证
- (void)sendFailedIapFiles
{
}
-(void)PrintIOSLog:(NSString*)logValue
{
//NSLog(@"------------------PrintIOSLog---------------logValue:%@", logValue);
//UnitySendMessage("SdkMessager", "IOSLog", logValue.UTF8String);
dispatch_async(dispatch_get_main_queue(), ^{
NSString *jsFunction = NSString stringWithFormat:@"CccToJS_LOG('%s')", logValue.UTF8String;
wkwebView1 evaluateJavaScript:jsFunction completionHandler:nil;
});
}
@end