Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/ReportConsumableFulfillmentAsyncWorker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "ReportConsumableFulfillmentAsyncWorker.h"
#include <Windows.h>
#include <iostream>
#include <napi.h>
#include <string>

ReportConsumableFulfillmentAsyncWorker::ReportConsumableFulfillmentAsyncWorker(
const Napi::Function &callback, std::string addOnStoreId, int quantity, winrt::guid trackingId, WindowsStoreImpl *pImpl)
: Napi::AsyncWorker(callback), m_addonstoreid(addOnStoreId), m_quantity(quantity), m_trackingid(trackingId), m_pImpl(pImpl),
m_result(NULL, NULL) {}

void ReportConsumableFulfillmentAsyncWorker::Execute() {
m_result = m_pImpl->ReportConsumableFulfillmentAsync(m_addonstoreid, m_quantity, m_trackingid);
}

void ReportConsumableFulfillmentAsyncWorker::OnOK() {
Napi::Env env = Env();
Napi::Object obj = Napi::Object::New(env);
if (m_result.extended_error != NULL) {
obj.Set("extendedError", m_result.extended_error);
}
if (m_result.status != NULL) {
obj.Set("status", m_result.status);
}
std::string returnTrackingId;
std::array<char,38> convertGuid;
snprintf(convertGuid.data(), convertGuid.size(), "%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X", m_trackingid.Data1, m_trackingid.Data2, m_trackingid.Data3, m_trackingid.Data4[0], m_trackingid.Data4[1], m_trackingid.Data4[2], m_trackingid.Data4[3], m_trackingid.Data4[4], m_trackingid.Data4[5], m_trackingid.Data4[6], m_trackingid.Data4[7]);
returnTrackingId = convertGuid.data();
obj.Set("TrackingId", returnTrackingId);
Callback().MakeCallback(Receiver().Value(), {
env.Null(), // error first callback
obj // this is apparently the value sent back to the callback
});
}

void ReportConsumableFulfillmentAsyncWorker::OnError(const Napi::Error &e) {
Napi::Env env = Env();

Callback().MakeCallback(Receiver().Value(), {e.Value(), env.Undefined()});
}
24 changes: 24 additions & 0 deletions src/ReportConsumableFulfillmentAsyncWorker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "WindowsStoreImpl.h"
#include <napi.h>
#include <string>

class ReportConsumableFulfillmentAsyncWorker : public Napi::AsyncWorker {
public:
ReportConsumableFulfillmentAsyncWorker(const Napi::Function &callback, std::string addOnStoreId,
int quantity, winrt::guid trackingId,
WindowsStoreImpl *pImpl);

protected:
virtual void Execute() override;
virtual void OnOK() override;
virtual void OnError(const Napi::Error &e) override;

private:
WindowsStoreImpl *m_pImpl;
std::string m_addonstoreid;
int m_quantity;
winrt::guid m_trackingid;
WindowsStoreImpl::StoreConsumableStatus m_result;
};
23 changes: 21 additions & 2 deletions src/StoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include "GetCustomerPurchaseIdAsyncWorker.h"
#include "GetStoreProductsAsyncWorker.h"
#include "RequestPurchaseAsyncWorker.h"
#include "ReportConsumableFulfillmentAsyncWorker.h"
#include "GetAppLicenseAsyncWorker.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <combaseapi.h>
#include <objbase.h>

Napi::FunctionReference StoreContext::constructor;

Expand All @@ -22,6 +25,7 @@ Napi::Object StoreContext::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("getAssociatedStoreProductsAsync", &StoreContext::GetAssociatedStoreProductsAsync),
InstanceMethod("getCustomerPurchaseIdAsync", &StoreContext::GetCustomerPurchaseIdAsync),
InstanceMethod("requestPurchaseAsync", &StoreContext::RequestPurchaseAsync),
InstanceMethod("reportConsumableFulfillmentAsync", &StoreContext::ReportConsumableFulfillmentAsync),
InstanceMethod("getAppLicenseAsync", &StoreContext::GetAppLicenseAsync),
});

Expand Down Expand Up @@ -52,7 +56,7 @@ Napi::Value StoreContext::Initialize(const Napi::CallbackInfo &info) {
Napi::HandleScope scope(env);
Napi::Buffer<char *> bufferData = info[0].As<Napi::Buffer<char *>>();
uint32_t handle = *reinterpret_cast<uint32_t *>(bufferData.Data());
HWND hwnd = (HWND)handle;
HWND hwnd = (HWND)(UINT_PTR)handle;
bool result = this->m_impl->Initialize(hwnd);
return Napi::Boolean::New(info.Env(), result);
}
Expand Down Expand Up @@ -96,9 +100,24 @@ void StoreContext::RequestPurchaseAsync(const Napi::CallbackInfo &info) {
(new RequestPurchaseAsyncWorker(cb, storeId, storePurchaseProperties, GetInternalInstance()))->Queue();
}

void StoreContext::ReportConsumableFulfillmentAsync(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() < 3) {
Napi::TypeError::New(env, "Too few arguments.").ThrowAsJavaScriptException();
}
Napi::String addOnStoreId = info[0].As<Napi::String>();
int quantity = napi_get_value_int32(env, info[1], 0);
GUID trackingGuid;
CoCreateGuid(&trackingGuid);
winrt::guid trackingId = trackingGuid;
Napi::Function cb = info[2].As<Napi::Function>();
(new ReportConsumableFulfillmentAsyncWorker(cb, addOnStoreId, quantity, trackingId, GetInternalInstance()))->Queue();
}

void StoreContext::GetAppLicenseAsync(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Function cb = info[0].As<Napi::Function>();
(new GetAppLicenseAsyncWorker(cb, GetInternalInstance()))->Queue();
}
}
1 change: 1 addition & 0 deletions src/StoreContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StoreContext : public Napi::ObjectWrap<StoreContext> {
void GetAssociatedStoreProductsAsync(const Napi::CallbackInfo &info);
void GetCustomerPurchaseIdAsync(const Napi::CallbackInfo &info);
void RequestPurchaseAsync(const Napi::CallbackInfo &info);
void ReportConsumableFulfillmentAsync(const Napi::CallbackInfo &info);
void GetAppLicenseAsync(const Napi::CallbackInfo &info);

Napi::Value Initialize(const Napi::CallbackInfo &info);
Expand Down
19 changes: 18 additions & 1 deletion src/WindowsStoreImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,21 @@ WindowsStoreImpl::RequestPurchaseAsync(std::string storeId, StorePurchasePropert
} else {
return WindowsStoreImpl::StorePurchaseResult(result.ExtendedError(), NULL);
}
}
}

WindowsStoreImpl::StoreConsumableStatus
WindowsStoreImpl::ReportConsumableFulfillmentAsync(std::string addOnStoreId, int quantity, winrt::guid trackingId) {
StoreContext context = StoreContext::GetDefault();
auto initWindow = context.try_as<IInitializeWithWindow>();
if (initWindow != nullptr) {
HRESULT hr = initWindow->Initialize(m_hwnd);
}

auto result = context.ReportConsumableFulfillmentAsync(to_hstring(addOnStoreId), quantity, trackingId).get();

if (result.ExtendedError() == S_OK) {
return WindowsStoreImpl::StoreConsumableStatus(NULL, static_cast<int>(result.Status()));
} else {
return WindowsStoreImpl::StoreConsumableStatus(result.ExtendedError(), NULL);
}
}
7 changes: 7 additions & 0 deletions src/WindowsStoreImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class WindowsStoreImpl {
StorePurchaseResult(int extendedError, int stat) : extended_error(extendedError), status(stat) {}
};

struct StoreConsumableStatus {
int extended_error;
int status;
StoreConsumableStatus(int extendedError, int stat) : extended_error(extendedError), status(stat) {}
};

struct StoreProduct {
std::string in_app_purchase_token;
StoreProduct(std::string inAppPurchaseToken) : in_app_purchase_token(inAppPurchaseToken) {}
Expand All @@ -45,6 +51,7 @@ class WindowsStoreImpl {
StorePurchaseResult
RequestPurchaseAsync(std::string storeId,
winrt::Windows::Services::Store::StorePurchaseProperties &purchaseProperties);
StoreConsumableStatus ReportConsumableFulfillmentAsync(std::string addOnStoreId, int quantity, winrt::guid trackingId);

private:
bool GetIsMicrosoftAccrued(AttributionScope scope);
Expand Down