ios支付

#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];

}

//2.开始拉起支付

-(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

{

}

//3.收到产品返回信息

  • (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

相关推荐
yuanyxh1 天前
macOS 应用 - 纯对话生成
前端·macos·ai编程
AI创界者3 天前
PilotTTS 一键整合包(Win/Mac):8G 显存畅跑,实测解锁情绪与副语言的精准控制
人工智能·macos·aigc·音视频
AirDroid_cn3 天前
系统终端与iTerm2字体看起来不一样?macOS Sequoia统一渲染指南
macos
初级代码游戏3 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼3 天前
【iOS】RunLoop学习
学习·ios
黑科技iOS上架3 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
JiaWen技术圈3 天前
2026 年的 macOS 磁盘清理方法
macos
lichong9513 天前
让AI自己用电脑!Cua:后台操作鼠标键盘,Mac/Windows/Linux全支持
人工智能·macos·ai·计算机外设·agent·提示词
A尘埃3 天前
批处理命令(Linux/Mac、Windows项目启动脚本)
linux·windows·macos