Unity 接微软支付(C++/CX)

本文介绍了使用Unity通过IL2CPP&XAML导出的C++/CX项目中的UWP应用内购流程,包括旧版API的支付发起、订单确认及未完成订单检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.介绍

项目是Unity通过IL2CPP & XAML导出的C++/CX的项目,如果是C#导出可以不用看这个,直接去看官网文档,查看例子即可。

 

2.相关代码(旧版本)

注意:SendMessageToU3D这个API,是向Unity发送消息。插件地址

微软文档:https://docs.microsoft.com/zh-cn/windows/uwp/monetize/enable-consumable-in-app-product-purchases

 

1.这是旧版API,流程是前端发起支付,支付成功后,拿交易的收据发给后端,需要后端去向微软验证订单合法性。

后端验证成功后,告诉前端订单确认。然后前端再调用函数MSConfirmOrder,通知微软此订单确认成功。

2.检查未确认的订单,是因为有可能出现网络问题,导致支付成功,却没有向微软确认订单可以发货。如果不检查,可能会导致用户,不能进行第二次购买。(建议每次游戏启动的时候,调用一次订单检查。旧版流程发起检查,拿到收据重新向后端发起验证....)

 

1.发起支付

void MainPage::MSPurchase(String^ productid)
{
	Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, productid]
	{
		//develop: CurrentAppSimulator
		//release: CurrentApp 

		auto authTask = Concurrency::create_task(Windows::ApplicationModel::Store::CurrentApp::RequestProductPurchaseAsync(productid));
		authTask.then([=](Windows::ApplicationModel::Store::PurchaseResults^ result)
		{
			String^ message;
			switch (result->Status)
			{
			case Windows::ApplicationModel::Store::ProductPurchaseStatus::Succeeded:
				message = "0" + "#%#" + productid + "#%#" + result->TransactionId.ToString()+ "#%#" + result->ReceiptXml;
				break;
			default:
				message = ((int)result->Status).ToString() + "#%#" + productid;
				break;
			}
			SendMessageToU3D(3, message);
		});

	}));
}

2.订单确认

void MainPage::MSConfirmOrder(String^ message)
{
	Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, message]
	{
		//develop: CurrentAppSimulator
		//release: CurrentApp 
		String^ temp_transactionId = message
		temp_transactionId = "{" + temp_transactionId + "}";

		GUID transactionId;
		HRESULT hr = IIDFromString(temp_transactionId->Data(), &transactionId);
		if (SUCCEEDED(hr)) {
			Platform::Guid UID(transactionId);
			auto authTask = Concurrency::create_task(Windows::ApplicationModel::Store::CurrentApp::ReportConsumableFulfillmentAsync(productid, UID));
			authTask.then([=](Windows::ApplicationModel::Store::FulfillmentResult result)
			{
				switch (result)
				{
				case Windows::ApplicationModel::Store::FulfillmentResult::Succeeded:
					//ok
					break;
				case Windows::ApplicationModel::Store::FulfillmentResult::NothingToFulfill:
					break;
				case Windows::ApplicationModel::Store::FulfillmentResult::PurchasePending:
					break;
				case Windows::ApplicationModel::Store::FulfillmentResult::PurchaseReverted:
					break;
				case Windows::ApplicationModel::Store::FulfillmentResult::ServerError:
					break;
				default:
					break;
				}
			});
		}
	}));
}

 

3.检查未完成订单

void MainPage::MSResumeOrder() 
{
	Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]
	{
		//develop: CurrentAppSimulator
		//release: CurrentApp 
		try
		{
			auto products = Concurrency::create_task(Windows::ApplicationModel::Store::CurrentApp::GetUnfulfilledConsumablesAsync());
			products.then([=](Windows::Foundation::Collections::IVectorView<Windows::ApplicationModel::Store::UnfulfilledConsumable^>^ Result) {
				for (int i = 0; i < Result->Size; ++i) {

					auto ProductID = Result->GetAt(i)->ProductId;
					auto transactionID = Result->GetAt(i)->TransactionId;
					auto requsetReceipt = Concurrency::create_task(Windows::ApplicationModel::Store::CurrentApp::GetProductReceiptAsync(ProductID));
					requsetReceipt.then([=](String^ Receipt) {
						SendMessageToU3D(5, ProductID + "#%#" + transactionID + "#%#" + Receipt);
					});
				}
			});
		}
		catch (Exception^ e)
		{
		
		}

	}));
}

3.新版API

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值