项目需要接入微软支付,折腾了两天终于搞好了,记录下接入流程:
1.发起支付;
2.支付成功通知后台验证发货;
3.发货成功,客户端上报微软该订单已完成;
4.启动时,检测是否有未完成订单,若存在则进行第二步;
发起支付:
void TKWinrtSdk::buyProduct(Platform::String^ productId, Platform::String^ orderId)
{
auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
if (dispatcher && panel)
{
// run on main UI thread
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, panel, productId, orderId]()
{
try
{
auto licenseInformation = CurrentApp::LicenseInformation;
if (!licenseInformation->ProductLicenses->Lookup(productId)->IsActive)
{
ProductPurchaseDisplayProperties^ prop;
create_task(CurrentApp::RequestProductPurchaseAsync(productId, orderId, prop)).then([this, productId, orderId](PurchaseResults^ result)
{
try
{
switch (result->Status)
{
case ProductPurchaseStatus::AlreadyPurchased:
break;
case ProductPurchaseStatus::NotPurchased:
break;
case ProductPurchaseStatus::Succeeded:
case ProductPurchaseStatus::NotFulfilled:
//CurrentApp::ReportProductFulfillment(productId);
TKSdkUwp::getInstance()->tellServerForUwp(PlatformStrToStdStr(orderId), PlatformStrToStdStr(result->ReceiptXml),"");
break;
}
}
catch (Platform::Exception^ exception)
{
}
});
}
else {
this->checkNotCompleteOrders();
}
}
catch (Platform::Exception^ exception)
{
}
}));
}
}
上报微软订单完成
void TKWinrtSdk::respondBuyProductSuc(Platform::String^ productId)
{
auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
if (dispatcher && panel)
{
// run on main UI thread
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, panel, productId]()
{
try {
CurrentApp::ReportProductFulfillment(productId);
}
catch (Platform::Exception^ exception)
{
}
}));
}
}
检测未完成订单
void TKWinrtSdk::checkNotCompleteOrders()
{
auto panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();
auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
if (dispatcher && panel)
{
// run on main UI thread
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, panel]()
{
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 offerId = Result->GetAt(i)->OfferId;
auto requsetReceipt = Concurrency::create_task(Windows::ApplicationModel::Store::CurrentApp::GetProductReceiptAsync(productID));
requsetReceipt.then([=](Platform::String^ ReceiptXml) {
TKSdkUwp::getInstance()->tellServerForUwp(PlatformStrToStdStr(offerId), PlatformStrToStdStr(ReceiptXml), PlatformStrToStdStr(productID));
});
}
});
}
catch (Exception^ e)
{
}
}));
}
}
注意调用checkNotCompleteOrders时需要保证微软商店登录了账户,否则会崩溃!!
字符串转换:
std::string PlatformStrToStdStr(Platform::String^ ms)
{
std::wstring w_str(ms->Begin());
return std::string(w_str.begin(), w_str.end());
}
std::wstring stows(std::string s)
{
std::wstring ws;
ws.assign(s.begin(), s.end());
return ws;
}
Platform::String^ stops(std::string s)
{
return ref new Platform::String(stows(s).c_str());
}