question

Fréderic Duguay avatar image
Fréderic Duguay asked

Facebook canvas payment

Hey there, currently working with unity 5 to create webgl builds and deploy on facebook canvas.

It seems I should be using StartPurchase, PayForPurchase and CompletePurchase in order to have playfab automatically reward items from canvas payment's since it can take days to process depending on the payment method chosen by the user. We do have webhook payments working between facebook and playfab.

 

My first specific question points to this documentation (PayForPurchase)

it seems I should be calling Facebook.Unity.FB.Canvas.Pay() after the PayForPurchase call, but to get a transaction id i would have to invoke facebook's payment dialogue before, what's the actual purchase flow for facebook?

 

My second question would relate to how items are rewarded to the user,

With CompletePurchase it states that items would be rewarded automatically. What happens when it will take a delayed time for the purchase to actually go through? Since I have no control over the RealTimeUpdate (or webhook) because that's been assigned to playfab, is playfab going to wait until the transaction is actually successful to give the items?

 

Thanks in advance for any response!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Sounds like you're very close. Here's the additional info you need:

For other payment providers, the Transaction ID is returned in the ProviderData property of the PayForPurchaseResponse. However, the Facebook purchase process works the other way around, requiring the ProviderTransactionId as part of PayForPurchase.

To do this, the flow is StartPurchase -> Kick off the Facebook purchase -> PayForPurchase -> ConfirmPurchase

Here's the code we use for the Facebook purchase in our test case:

FB.ui({
method: 'pay',
action: 'purchaseitem',
product: "https://{ {TitleId}}.playfabapi.com/OpenGraphProduct/{ {TitleId}}/{ {StoreId}}/{ {CatalogId}}/{ {ItemId}}",
request_id: orderId
}, function (response) {
console.log('Payment completed', response);
print(response);
if (response.payment_id) {
facebookPurchaseId = response.payment_id;
payForOrder(); // PayForPurchase PlayFab API
}
});

For items being added to the user inventory, that happens when Facebook calls the callback URL. You can read more about that in this post:

https://community.playfab.com/hc/en-us/community/posts/207011008-Facebook-Purchase-Webhooks

Ideally, the title would still call ConfirmPurchase, but it's not required to get the items into the player's inventory with Facebook purchasing.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fréderic Duguay avatar image
Fréderic Duguay answered

Hey there! thank's for the response!

 

I'm not looking at paypal unfortunately, and i'm not sure I quite understand how to get transaction id from PayForPurchases' response when I need it to call PayForPurchases.

As for ConfirmPurchases, thank's that part seems clearer now, since facebook payment's can sometimes take days, i'll have to keep track of orderId's that are not yet completed and keep calling ConfirmPurchases until I get a response for it, at which point items will have been rewarded.

 

If you can offer clarification for that transaction id, that'd be awesome! thanks again!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Ah, apologies for the confusion - trying to do too many things at once. I've updated the response to clear that up. Yes, this is specifically just for Facebook purchases.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fréderic Duguay avatar image
Fréderic Duguay answered

Now i'm getting "BadRequest: Payment returned that transaction was not approved. Provider Status: Init" when I call PayForPurchases properly with facebook's responses, am I suppose to keep calling PayForPurchases or ConfirmPurchases? How can I handle transactions that are not instantaneous?

Edit: actually this could come from my logic flow, i'll confirm shortly

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

In the example above, the "function (response)" portion is the callback that Facebook calls when the Pay dialog is closed. At that point, the payment should be either completed or cancelled by the user. The payForOrder call in our example is where we call PayForPurchase.

In your case then, you're making the call similar to above, but when you call PayForPurchase, you're getting that the status still "Init"? That shouldn't be happening - Facebook is the one telling us in the example that he purchase is "finished" (one way or the other). Can you send the code snippets showing how you're making the calls, and a write-up of your repro steps to devrel@playfab.com?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fréderic Duguay avatar image
Fréderic Duguay answered

You're correct, turns out I was mis-associating the debug logs I have put, not very convenient it is to work in webgl/html5! Everything should be good now as I see my items appearing in my inventory, even with delayed transaction. It's just a matter of always calling ConfirmPurchases with the OrderID's from purchases in progress. Hopefully this doesn't push the limits around making playfab calls, I call ConfirmPurchases every minute when transactions are still in progress.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Glad to hear it's working for you. And once a minute shouldn't be an issue, but I'm curious - how long are you seeing it take before ConfirmPurchase works, once PayForPuchase has been called successfully (on average)?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fréderic Duguay avatar image
Fréderic Duguay answered

For testing purchases, facebook offers to confirm the purchase 30 seconds after making the purchase to test these asynchronous payments method. 

It's possible the user can decide to use payment options that would require them to mail papers/cheque somewhere which could take days for confirmation to go through.  Facebook requires that this type of payment be supported 
https://developers.facebook.com/docs/payments/implementation-guide/order-fulfillment

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Thanks for the link. Since these purchases could take days to complete, but electronic payments are quick, I would recommend using an exponential backoff on the calls to ConfirmPurchase, with a manual option for the player to check if payments have been processed by Facebook. So the way this would work is that you'd check ConfirmPurchase when PayForPurchase completed, storing the ID if it's still incomplete, then again in a minute, then two, then 4, then 8, until you hit some cap (maybe once an hour). That'll help to minimize the bandwidth you use (not that this call is much, it's just a best practice). You'll also want to provide a "check for completed transactions" options, if you have any IDs that are awaiting completion, so that the user can manually check.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

I was just pointed to this older thread by someone researching Facebook payments, and wanted to update it to correct on thing. With Facebook, and specifically because of the way their payment fulfillment is set up, the key is the callback URL, as described in this post: https://community.playfab.com/questions/505/207011008-Facebook-Purchase-Webhooks.html. Facebook makes the call to that URL when the payment clears their service. It is actually at that point that fulfillment occurs, and items are added to the player inventory. Ideally, you would still call ConfirmPurchase after that point, but the player will have received the items.

I'll update the answer above, to make sure this is clear for everyone.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.