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

相关推荐
greasyfork7 小时前
多种数据库管理混乱?Navicat Premium 17 for Mac 统一解决
数据库·mysql·macos·mac
碎_浪9 小时前
Mac 壁纸被 MDM 锁住?我做了个 2MB 的开源方案
macos·开源·swift
2501_9160088911 小时前
iOS 证书管理最佳实践 从创建到续期的完整指南
android·ios·小程序·https·uni-app·iphone·webview
逆向编程11 小时前
AI编程CLI两种配置方案(macOS专用)
macos·ai编程
沐禾安信11 小时前
开会来不及记录?Mac 录屏方法收好
macos·音视频·电脑录屏
ITKEY_21 小时前
macOS brew 安装的nginx 文件在哪里?
运维·nginx·macos
大E帝国子民11 天前
MacOS 安装Seismic Unix
服务器·macos·unix
黑科技iOS上架1 天前
ReactNative入门介绍
经验分享·ios
智塑未来1 天前
鸿蒙系统对比安卓、iOS,核心优势是什么?
android·ios·harmonyos
z***刺1 天前
VMware安装macOS安装教程(附安装包)2025超详细下载安装教程
其他·macos