UWP SDK支付

注意:

Win10版本的产品才可接入UWP SDK,如需接入请查阅【Untiy移植Windows 10版本】,【Cocos移植Windows 10版本】

根据Microsoft Store策略条款,开发者仅限使用Microsoft Store应用内购买API接口

简介

本文介绍了如何对接Miracle Games SDK的支付接口,打开支付窗口。

在调用支付接口前,确保已经完成了SDK的初始化【参考】。

目前一共有两种支付方式,分为微软支付及MG支付。在接入SDK结束后,微软的支付方式必须要等游戏上架后才能进行真正的微软支付。

该接口由MiracleGames统一管理,若需要开启支付功能,需要提前联系MG工作人员协助开通

用户支付流程图

用户支付

C#CPP

public async void ShowPayChannel()
{
	try
	{
		if (!MiracleGames.ApplicationManager.SetupCompletedSuccessfully)//检测是否初始化完成
			return;
		// goodsKey:商品Key,创建商品时由MiracleGames生成 
		// message:自定义参数,传递前进行urlencode。传入此参数,完成订单后会得到此参数
		// callbackid:回调地址唯一标识,在MG后台创建获得
		// 最后一个bool参数,ture:微软支付  false:第三方支付
		var result = await MiracleGames.PaymentManager.OpenPayChannelAsync(GoodsKey, message, callbackid, false);
		if (result.ReturnValue)//检测是否完成支付操作
		{
		   
		}
		else
		{
			var msg = new MessageDialog("Cancel payment.");
			await msg.ShowAsync();
		}
	}
	catch (Exception){}//添加异常处理机制,预防游戏崩溃.
}

Platform::String ^ MainPage::PurchaseGoodsMicrosoft(Platform::String^ goodsKey, Platform::String^ message)
{	
	try
	{
		if (!MiracleGames::ApplicationManager::SetupCompletedSuccessfully)//检测是否初始化完成
			return;
				
		// goodsKey:商品Key,创建商品时由MiracleGames生成
		// message:自定义参数,传递前进行urlencode。传入此参数,完成订单后会得到此参数
		// callbackid:回调地址唯一标识,在MG后台创建获得
		// 最后一个bool参数,ture:微软支付  false:第三方支付
		concurrency::create_task(MiracleGames::PaymentManager::OpenPayChannelAsync(goodsKey, message, callbackid, true))
			.then([](MiracleGames::Services::Core::Common::AsyncProcessResult^ result)
		{
			if (result->ReturnValue)//检测是否完成支付操作
			{
				
			}
			else
			{
				// Cancel payment.
			
			}	
		});
		return "1";
	}
	catch (...){}//添加异常处理机制,预防游戏崩溃.
}

Unity引擎游戏调用支付时需要设置遮罩层。请参考

支付成功后服务器端回调开发者服务器

支付成功后服务器端回调游戏客户端

如果游戏有服务器可接收回调,则无需接入此功能,直接接入服务器端的支付回调。

C#CPP

try
{
	MiracleGames.PaymentManager.AssetsChanged += async (o, args) =>//客户端支付回调
	{
	Debug.WriteLine("Assets changed fired, assets count is {0}", args.Assets?.Count());
	int cnt = args.Assets.Count();
	if (cnt > 0) //检测是否有未完成订单
	{
		for (int i = 0; i < cnt; i++)//遍历订单信息
		{
			var item = args.GetAssetElementAt(i);

			if (item != null)
			{
				//var  userid = MiracleGames.AuthenticationManager.UserInfo.Id;// 如果commen中储存有用户ID信息,可以通过此接口获取用户ID。
				//if ((userid != null&&userid!="") && userid.Equals(item.Comment))//并以此判断当前用户和commen信息中储存的用户信息是否一致
				{
					var nm = item.DigitalGoods.Name.ToString();
					var id = item.Id;
					var comment = item.Comment;
					var type = args.EventType;

					switch (type)
					{
						case 0:
							var res = await item.ReportFulfillmentAsync();//核销订单
							if (res.ReturnValue)
							{
								//发送道具
							}
							break;
						case 1:
							var res1 = await item.ReportFulfillmentAsync();
							if (res1.ReturnValue)
							{
								//补发道具
							}
							break;
						default:
							break;
					}

				}
			}
		}
	}
	};
}
catch (Exception){}//添加异常处理机制,预防游戏崩溃.

try
{
	Loaded += ref new RoutedEventHandler([this](Object^, RoutedEventArgs^)//客户端支付回调
	{
	MiracleGames::PaymentManager::AssetsChanged += ref new EventHandler(
	[&](Object^ o, MiracleGames::AssetsChangedEventArgs^ args)//检测是否有未完成订单
	{

		int cnt = args->GetAssetsCount();
		for (int i = 0; i < cnt; i++)//遍历订单信息
		{
			auto item = args->GetAssetElementAt(i);
			//auto uid = MiracleGames::AuthenticationManager::UserInfo->Id;// 如果commen中储存有用户ID信息,可以通过此接口获取用户ID。
			//if ((uid != nullptr&& uid !="") && uid->Equals(item->Comment))//并以此判断当前用户和commen信息中储存的用户信息是否一致
			if (item)
			{
				auto nm = item->DigitalGoods->Name->ToString();
				auto id = item->Id;
				auto comment = item->Comment;
				auto type = args->EventType;

				switch (type)
				{
				case 0:
					concurrency::create_task(item->ReportFulfillmentAsync()).then([=](MiracleGames::Services::Core::Common::AsyncProcessResult^ result) {
						if (result->ReturnValue)
						{
							//汇报订单已完成,发送道具
							auto meg = item->Comment;
							SendMessageToU3D(2, comment);
						}
						});
					break;

				case 1:
					concurrency::create_task(item->ReportFulfillmentAsync()).then([=](MiracleGames::Services::Core::Common::AsyncProcessResult^ result) {
						if (result->ReturnValue)
						{
							//汇报订单已完成,补发道具
							SendMessageToU3D(2, "补发道具");
						}
						});
					break;
				default:
					break;
				}
			}
		}
	});
	});
}
catch (...){}//添加异常处理机制,预防游戏崩溃.

视频演示--支付