0

Can someone suggest a working method to purchase an add-on from within the app?

In my application, which is published in the Microsoft Store, I have a single add-on (a subscription). Since the official documentation states:

Selling subscriptions to customers directly via the Store is not supported at this time. Subscriptions are available for in-app purchases of digital products only.

I understand that I need to implement the purchase of this add-on manually within the app.

I found this sample as a starting point, but the author mentions it "generates an exception":

function TForm1.PurchaseItem(Item: string) : string;
begin
  for var i := 0 to WindowsStore1.AppProducts.Count - 1 do
    if TWindowsString.HStringToString(WindowsStore1.AppProducts[i].InAppOfferToken) = Item then begin
      BuyProduct(WindowsStore1.AppProducts[i]);
      exit;
    end;
end;

procedure TForm1.BuyProduct(Product: IStoreProduct);
begin
  try
    var status := WindowsStore1.PurchaseProduct(Product);
    if status = StorePurchaseStatus.Succeeded then 
    begin
      EnableFullVersion;
    end
    else begin
      //;
    end;
  except
    On e : Exception do
      //;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PurchaseItem('MyAddon');
end;

So, what is the correct way to let a user purchase my subscription add-on from within the app using the TWindowsStore component in Delphi (12.1)?

2
  • "the author mentions it generates an exception" - so, rather than debug his code and fix it, he tells his readers to write a DLL in C# to work around the problem. Nice. Commented Jun 17 at 15:36
  • Your question is basically asking for a tutorial, which is off-topic for StackOverflow. Are you encountering the exception that the author mentioned, or having another problem? You should edit your question to reflect that. Commented Jun 17 at 15:38

1 Answer 1

0

As @remy-lebeau rightly pointed out, the code sample I originally found wasn't the best example to follow.
The following code works nice in my case and allows me to set up an in-app subscription for the add-on:

procedure TfSubsriptions.btnSubscribe(Sender: TObject);
var
  Status: StorePurchaseStatus;
begin
  try
    WindowsStore1.RefreshInfo;
  except
    // 
  end;

  for var i := 0 to WindowsStore1.AppProducts.Count - 1 do
    begin
      if WindowsStore1.AppProducts[i].StoreId.ToString = 'XXXXXXXXXXXX' then 
        begin
          try
            Status := WindowsStore1.PurchaseProduct(WindowsStore1.AppProducts[i]);
            case Status of
              StorePurchaseStatus.Succeeded:
                begin
                  SUBSCRIPTION := True;
          UpdateUI;
                  Break;
                end;
              StorePurchaseStatus.AlreadyPurchased:
                //
              StorePurchaseStatus.NotPurchased:
                //
              StorePurchaseStatus.NetworkError:
                //
              StorePurchaseStatus.ServerError:
                //
            else
              //
            end;
          except
            On e : Exception do
              begin
                //
                Break;
              end;
          end;
        end;
    end;
end;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.