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

来源:互联网 发布:经营淘宝店成功案例 编辑:程序博客网 时间:2024/05/29 06:31

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 = messagetemp_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://okbreak;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