接入UWP SDK支付

简介

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

快速支付

如果应用有自己的商店UI,或者有特殊商品不宜出现在MiracleGames提供的商店页面中,可以使用快速支付功能。开发者可根据自身应用情况灵活使用

 MiracleGames.StoreNavigationCommand command = new MiracleGames.StoreNavigationCommand(); 
 command.Type = MiracleGames.NavigationType.InApp; 
 command.Comment = "test comment"; 
 // 可选,商品Key,再创建商品时由MiracleGames生成,用于唯一标识一个商品 
 command.DigitalGoodsKey = "9ECG76AED"; 
 // 可选, 商品数量,默认值为1 
 command.DigitalGoodsCount = 2; 
 // 可选,服务器回调地址Id,默认值为null,在设置回调地址时由MiracleGames生成,开发 者可以设置多个回调地址,该笔交易将会给指定Id的回调地址发起支付成功的回调 
 // 如果设置了回调地址,但未指定Id(CallbackId=null),将使用默认回调地址
 // 如果指定了Id,但该Id不存在,将不能支付 
 // 如果不使用服务器回调,请忽略该属性。 
 command.CallbackId = "JDDF83KDC"; 
 MiracleGames.Payment.PaymentManager.OpenStore(command);

打开MG商店

在应用需打开商店的方法中添加如下代码:

MiracleGames.NavigationCommand command = new MiracleGames.NavigationCommand(); 
// 在当前应用中打开。 IE:跳转到系统浏览器中打开,InApp:在当前应用中打开 
command.Type = MiracleGames.NavigationType.InApp; 
// 备注信息 (与资产中的Comment对应),可以在备注中包含用户Id等信息,来确定接收资产的用户。 
// 如果使用服务器回调,该值对应服务器回调中的user_comment参数。 
command.Comment = "test comment"; 
MiracleGames.Payment.PaymentManager.OpenStore(command); // 打开商店

OpenStore有一个不带参数的重载方法:

MiracleGames.Payment.PaymentManager.OpenStore(); 

该方法等同于如下代码:

MiracleGames.NavigationCommand command = new MiracleGames.NavigationCommand(); 
command.Type = MiracleGames.NavigationType.InApp; 
MiracleGames.Payment.PaymentManager.OpenStore(command);

商店的显示方式

需要注意的是,如果应用使用Socket进行通信,使用InIE方式会导致应用网络中断。同样的使用InApp方式也有可能导致网络中断(具体取决于应用本身),如果应用在InApp方式下网络中断,可以在打开商店之前添加如下代码:

command.ParentContainer = this.grid;

this.grid即当前页面中的一个Grid控件,当然你也可设置ParentContainer为其他类型的控件。 INavigationCommand中的ParentContainer属性可以指定要打开的界面的父容器。如果不指定 ParentContainer,SDK将会自动选择以合适的行为打开。

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

支付窗口关闭事件

当玩家关闭支付窗口时,会回调此事件

UWPCPP

MiracleGames.Payment.PaymentManager.StoreClosed += PaymentManager_StoreClosed;
private async void PaymentManager_StoreClosed(object sender, object e)
{
    new MessageDialog("支付窗口关闭").ShowAsync();
}

//在.h头文件中添加如下代码
void onPaymentManager_StoreClosed(Platform::Object ^sender, Platform::Object ^e);

//在app.xaml.cpp文件中的加载方法中添加如下代码,注册事件
MiracleGames::Payment::PaymentManager::StoreClosed += ref new  Windows::Foundation::EventHandler<Platform::Object ^>(this, &App::onPaymentManager_StoreClosed);

//在app.xaml.cpp文件中添加如下代码
void App::onPaymentManager_StoreClosed(Platform::Object ^,Platform::Object ^e)
{
	Windows::UI::Popups::MessageDialog^ dialog = ref new Windows::UI::Popups::MessageDialog("支付窗口关闭");
	dialog->ShowAsync();
}