j3k0 / cordova-plugin-purchase

In-App Purchase for Cordova on iOS, Android and Windows
https://purchase.cordova.fovea.cc
1.3k stars 537 forks source link

How can I get transaction receiptData for self-validation #402

Closed isayeter closed 8 years ago

isayeter commented 8 years ago

Hello,

I need to get receipt data in base64 encoded because I want to use my own validation algorithm, so I need receipt data to send my servers,

How can I grap it after purchase is approved ? product.transaction returns these :

{"type":"ios-appstore","id":"1000000194899091"}

but I need receipt data ?

Thanks.

isayeter commented 8 years ago

sorry, I just found

store.validator = function(product, callback)

method in documentation, that is what I need.

Thanks.

piin commented 7 years ago

Hello @fattalgaz!!! excuse me, How to use the validator, can you show me pls? I want to use my own server.

isayeter commented 7 years ago

Hello @piin , you can use it like that:

        //////////////
        //validator //
        //////////////
        store.validator = function(product, callback) // jshint ignore:line
        {
            var receipt = product.transaction.appStoreReceipt;
            var product_type = product.type;
            var vaidatorURI = (product_type == "consumable") ? "user/credit/buy/" : "user/subscription/buy/";

            var requestObject = (new Request(
                vaidatorURI,
                function (responseData) // jshint ignore:line
                {
                    product.server_response = responseData;
                    callback(true, responseData);
                },
                function (errorReason, errorData) // jshint ignore:line
                {
                    product.server_response = errorData;

                    //server error ! callback false to come back again.
                    if(typeof errorData==="undefined")
                    {
                        logger.warn('=========== STORE php not answered !!');
                        callback(false, errorReason); 
                    }
                    //it is ok, inserted to db. clear from queue (jailbreak purchase)
                    else
                    {
                        logger.warn('=========== STORE php is answered !!');
                        callback(true);
                    }
                }
            ));

            requestObject.post({
                receipt: receipt
            });
        };
piin commented 7 years ago

Thanks @fattalgazi for the response, I've already use your method but the request has been never called, this is my code I am using Typescript:

        window.store.validator = (product, callback)=> {
            let receipt = product.transaction.appStoreReceipt;
            let product_type = product.type;
            let url = "wp-json/amplemind/v1/validate_iap_ios"
            var p = this.request.post(url,product).then(response=>{
                product.server_response = response
                console.log("success",response)
                callback(true,response)
            }).catch(reason=>{
                console.log("error", reason)
                product.server_response = reason;
                if(typeof reason==="undefined"){
                    callback(false,reason)
                }else{
                    callback(true)
                }
            })
        }

I don't know why the request never response, If I make the request outside of the validator the request response with an error but it works. Can you help me pls

isayeter commented 7 years ago

it is hard to catch error . did you tried to add some console.log messages between lines in window.store.validator function? it maybe called but there can be errors in other lines.