How to differentiate Unity Purchase from Restore Purchase? - unity3d

I'm trying to implement unity iap and I need to send PaymentSucceed event to my analytics in order to track purchases!
I'm Calling PaymentSucceed event in my
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
function, but the issue is that when user re-installs application the ProcessPurchase event is called again on android to restore purchases, and sents event for my analytics, but that's not a revenue event, so that's not correct to count it as Payment, can you share please some thoughts how I can understand is it restore or actual payment in the correct way?
here is my ProcessPurchase script
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
// A consumable product has been purchased by this user.
if (String.Equals(args.purchasedProduct.definition.id, kProductIDNoAds, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
PlayerPrefs.SetInt("noads",1);
BannerAdManager.Instance.HideBanner();
CheckNoAds();
var prodID = args.purchasedProduct.definition.id;
var price = ReplaceCommas(args.purchasedProduct.metadata.localizedPrice.ToString());
var currency = args.purchasedProduct.metadata.isoCurrencyCode;
var receipt = args.purchasedProduct.receipt;
var param = new Dictionary<string, string>();
param["af_quantity"] = "1";
param["af_content_type"] = "general";
var recptToJSON = (Dictionary<string, object>)AFMiniJSON.Json.Deserialize(receipt);
var receiptPayload = (Dictionary<string, object>)AFMiniJSON.Json.Deserialize((string)recptToJSON["Payload"]);
var purchaseData = (string)receiptPayload["json"];
var signature = (string)receiptPayload["signature"];
Debug.LogError("Purchase Event Sent Start");
AppsFlyerAndroid appsFlyerAndroid = new AppsFlyerAndroid();
appsFlyerAndroid.validateAndSendInAppPurchase(
"111",
signature,
purchaseData,
price,
currency,
param,
this);
Debug.LogError("Purchase Event Sent Finish");
PaymentSucceed(args.purchasedProduct.definition.id,args.purchasedProduct.metadata.isoCurrencyCode,(float)args.purchasedProduct.metadata.localizedPrice,"noads");
}
// Or ... an unknown product has been purchased by this user. Fill in additional products here....
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
// Return a flag indicating whether this product has completely been received, or if the application needs
// to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
// saving purchased products to the cloud, and when that save is delayed.
return PurchaseProcessingResult.Complete;
}

Related

Paypal SDK giving error when trying to create Payment

I created an app on PayPal with sandbox account. Then i picked up its API Client id and Secret.
I created a new console application and added paypal nuget package version 1.9.1.
internal class Program { static void Main(string[] args) { // Get a reference to the config var config = ConfigManager.Instance.GetProperties();
// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config["clientId"], config["clientSecret"]);
var apiContext = new APIContext(accessToken.GetAccessToken());
try
{
var payment = new Payment()
{
intent = "authorize",
// A resource representing a Payer that funds a payment. Use the List of `FundingInstrument` and the Payment Method as 'credit_card'
payer = new Payer()
{
// The Payment creation API requires a list of
// FundingInstrument; add the created `FundingInstrument`
// to a List
funding_instruments = new List<FundingInstrument>()
{
// A resource representing a Payeer's funding instrument.
// Use a Payer ID (A unique identifier of the payer generated
// and provided by the facilitator. This is required when
// creating or using a tokenized funding instrument)
// and the `CreditCardDetails`
new FundingInstrument()
{
// A resource representing a credit card that can be used to fund a payment.
credit_card = new CreditCard()
{
billing_address = new Address()
{
city = "Johnstown",
country_code = "US",
line1 = "52 N Main ST",
postal_code = "43210",
state = "OH"
},
cvv2 = "874",
expire_month = 11,
expire_year = 2023,
first_name = "Joe",
last_name = "Shopper",
number = "5105105105105100",
type = "visa"
}
}
},
payment_method = "credit_card"
},
// The Payment creation API requires a list of transactions; add the created `Transaction` to a List
transactions = new List<Transaction>()
{
// A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. Transaction is created with a `Payee` and `Amount` types
new Transaction()
{
// Let's you specify a payment amount.
amount = new Amount()
{
currency = "USD",
// Total must be equal to sum of shipping, tax and subtotal.
total = "107.47",
details = new Details()
{
shipping = "0.03",
subtotal = "107.41",
tax = "0.03"
}
},
description = "This is the payment transaction description."
}
},
payee = new Payee { email = "rassmasood#hotmail.com" }
};
var createdPayment = payment.Create(apiContext);
}
catch (Exception ex)
{
throw;
}
}
}
Using the code above i get the error mentioned below.
{"name":"VALIDATION_ERROR","message":"Invalid request - see details","debug_id":"f3c178cb2a17b","details":[{"field":"/payee","location":"body","issue":"MALFORMED_REQUEST_JSON"}],"links":[]}
is there anything i am doing wrong ?
i tried adding more information to Payee for example its account merchand_id and stuff but didnt worked.
i am expecting the exception to be resolved.
The package you are using is 5 years old and deprecated.
Use the current PayPalCheckoutSDK 1.0.4

how to calculate tax using paypal api

i installed the .net sdk of paypal and created an app in sandbox environment
next i picked up the clientId and secret and used the following sample code to make a payment.
static void Main(string[] args)
{
// Get a reference to the config
var config = ConfigManager.Instance.GetProperties();
// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config["clientId"], config["clientSecret"]);
var apiContext = new APIContext(accessToken.GetAccessToken());
var payment = Payment.Create(apiContext, new Payment
{
intent = "sale",
payer = new Payer
{
payment_method = "paypal"
},
transactions = new List<Transaction>
{
new Transaction
{
description = "Test",
invoice_number = "009",
amount = new Amount
{
currency = "EUR",
total = "41.00",
details = new Details
{
tax = "0",
shipping = "0",
subtotal = "40",
handling_fee = "1"
}
},
item_list = new ItemList
{
items = new List<Item>
{
new Item
{
name = "Room 12",
currency = "EUR",
price = "10",
quantity = "4",
}
}
}
}
},
redirect_urls = new RedirectUrls
{
return_url = "https://google.de/",
cancel_url = "https://google.de/"
}
});
}
in the transaction i have to pass Tax information.
Is there was that i let paypal calculate the tax and i just pass amount information along with address and some other information if required ?
No, you must calculate the tax yourself.
By default the user will be able to select their shipping address at PayPal, which is recommended as this saves them from having to type it manually. Given that their address can change during the PayPal checkout, you may wish to calculate a new tax amount and/or shipping amount based on the selected address. You can do this using the JS SDK's onShippingChange callback.
Firstly, though, it appears you may be using a deprecated SDK and deprecated v1/payments API, and also a redirect away from your site to PayPal, all of which is old. Don't do any of this.
Instead: follow the PayPal Checkout integration guide and make 2 routes on your server, one for 'Create Order' and one for 'Capture Order' (see the optional step 5 in 'Add and modify the code'). Both of these routes should return only JSON data (no HTML or text). There is a Checkout-Java-SDK you can use, or integrate with your own direct HTTPS API calls (obtain an access_token first, it can be cached but expires in 9 hours).
Inside the 2nd capture route on your server, when the capture API call is successful you should store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding your return JSON to the frontend caller.
Pair those 2 routes with the frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

Notify User of New Signin with Firebase - Swift/Xcode

I have looked all over and can't figure out how to notify a user when a new device signs into their account. I'm using Firebase Authentication. I'd like to setup some kind of notification system or email that tells the user someone has signed into their account from a different device so they can know for security reasons.
Ideas?
Also, how could you monitor information about what device (s) are signed into a specific account? For example, maybe the user is curious how many devices he has signed into his account, what type of device they are, what the name of those devices are, and where they are located (example: San Antonio, Texas).
Ideas?
yes, you can use keychain for this situation. First you should create an uniqueId. After you can save to the db uniqueId. This value will change if the user uses another device. I'm using third party framework for Keychain services. You can use framework. It's perfect.
https://github.com/kishikawakatsumi/KeychainAccess
final class DeviceManager {
static let shared = DeviceManager()
private init() { }
var uniqueDeviceId: String {
get {
let keychain = Keychain(service: KeyManager.keychainServiceName).accessibility(.always)
if let deviceId = keychain[KeyManager.keychainDeviceIdKey] {
return deviceId
}
let vendorId = UIDevice.current.identifierForVendor!.uuidString
keychain[KeyManager.keychainDeviceIdKey] = vendorId
return vendorId
}
}
}
Super simple; have a field in your user document that stores a device name along with its status.
You app will be observing this users document and when something changes, all of the users devices will be notified of that change.
Let me set this up; here's a basic Firestore structure
users
uid_0
userName: "Jay"
devices:
device_0: offline
device_1: offline
When the app starts, it will add an observer to this users document (using the uid as the documentId)
func observeUser() {
let usersCollection = self.db.collection("users")
usersCollection.document("uid_0").addSnapshotListener { (documentSnapshot, err) in
guard let document = documentSnapshot else {
print("Error fetching document: \(err!)")
return
}
let device = document.get("devices") as! [String: String]
print(device)
}
}
Now in the Firestore closure shown above, if a users device changes status, offline to online for example, it outputs all of the devices to console. You would take whatever action is needed when the device changes status.
Keep in mind that if a NEW device is added, that event will also fire so you could present a message in the UI "A new device was added!"
So then some testing code that toggles the device_0 status from offline to online. I have a button click that does self.status = !self.status and then calls the toggleStatus function
var status = false
func toggleStatus() {
var isOnline = ""
if self.status == false {
isOnline = "online"
} else {
isOnline = "offline"
}
let userCollection = self.db.collection("users")
let thisDevice = "device_0"
let devicesDict = [
"devices":
[thisDevice: isOnline] //sets device_0 to offline or online
]
let document = usersCollection.document("uid_0").setData(devicesDict, merge: true)
}
In a nutshell, when a user authenticates with a device for the first time, it would perhaps ask for a device name, or craft one from the devices mac address or something under the hood. That device name is stored in the users document/devices with it's online status.
The device name would be stored locally as well, in user defaults for example so it's automatically sent up to Firestore upon login.
The end result here is that if any users devices change status; offline to online or vice versa, or any device is added or removed all of the devices are notified of that event.

Send Money to Paypal Account ASP.Net Server Side Code

I am having a difficult time finding halfway descent documentation or examples on how to send money to another Paypal account.
I have installed the Nuget package PaypalSDK version 1.0.4. I have read the documentation at https://developer.paypal.com/home. I have browsed and tried to implement the sample code at https://github.com/paypal/Checkout-NET-SDK.
The problem I am having is that I am having is that I am not seeing notifications of payments sent or received in my sandbox account. I can successfully execute a checkout with the Javascript button in my shopping cart view. But eventually I want to add the capability to send money from my Paypal business account to another Paypal business account, without the other Paypal Business Account owner having to be logged in to my website.
Does the money recipient have to authorize the money I send, or should it just get deposited into their account once I send it?
Here is my code:
namespace MyShoppingCart.Helpers.Paypal
{
public class CaptureOrderSample
{
static string PayPalClientID = Startup.StaticConfig.GetValue<string>("Paypal:ClientID");
static string PayPalClientSecret = Startup.StaticConfig.GetValue<string>("Paypal:ClientSecret");
public static HttpClient client()
{
// Creating a sandbox environment
PayPalEnvironment environment = new SandboxEnvironment(PayPalClientID, PayPalClientSecret);
// Creating a client for the environment
PayPalHttpClient client = new PayPalHttpClient(environment);
return client;
}
public async static Task<HttpResponse> createOrder(string Email)
{
HttpResponse response;
// Construct a request object and set desired parameters
// Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
var order = new OrderRequest()
{
CheckoutPaymentIntent = "CAPTURE",
PurchaseUnits = new List<PurchaseUnitRequest>()
{
new PurchaseUnitRequest()
{
AmountWithBreakdown = new AmountWithBreakdown()
{
CurrencyCode = "USD",
Value = "100.00"
},
Payee = new Payee
{
Email = Email // "payee#email.com"
}
}
}
//,
//ApplicationContext = new ApplicationContext()
//{
// ReturnUrl = "https://www.example.com",
// CancelUrl = "https://www.example.com"
//}
};
// Call API with your client and get a response for your call
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(order);
response = await client().Execute(request);
var statusCode = response.StatusCode;
Order result = response.Result<Order>();
Debug.WriteLine($"Status: {result.Status}");
Debug.WriteLine($"Order Id: {result.Id}");
Debug.WriteLine($"Intent: {result.CheckoutPaymentIntent}");
Debug.WriteLine("Links:");
foreach (LinkDescription link in result.Links)
{
Debug.WriteLine($"\t{link.Rel}: {link.Href}\tCall Type: { link.Method}");
}
return response;
}
}
}
And this is currently called from my Orders controller when an order is completed. This is just for testing purposes.
[Authorize]
public async Task<IActionResult> CompleteOrder()
{
var items = _shoppingCart.GetShoppingCartItems();
Models.Order order = await _ordersService.StoreOrderAsync(items);
PrepareSellerEmail(items, order, "You Have a New Order!");
PrepareBuyerEmail(items, order, "Thank You for Your Order!");
await _shoppingCart.ClearShoppingCartAsync(_serviceProvider);
DeleteCartIDCookie();
//OrderRequest request = Helpers.CreateOrderSample.BuildRequestBody("USD", "100.00", "sb-r43z1e9186231#business.example.com");
//var client = Helpers.Paypal.CaptureOrderSample.client();
var result = Helpers.Paypal.CaptureOrderSample.createOrder("sb-r43z1e9186231#business.example.com");
//var response = await PayPalClient.client().execute.(request);
return View("OrderCompleted");
}
The output of the result is:
Status: CREATED
Order Id: 51577255GE4475222
Intent: CAPTURE
Links:
self: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222 Call Type: GET
approve: https://www.sandbox.paypal.com/checkoutnow?token=51577255GE4475222 Call Type: GET
update: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222 Call Type: PATCH
capture: https://api.sandbox.paypal.com/v2/checkout/orders/51577255GE4475222/capture Call Type: POST
This is a screen capture from my sandbox account:
Am I supposed to do something else to actually execute the transfer?
Edit: I figured out how to use the Paypal Payouts API.
First I installed the Nuget Package. It's simply called PayoutsSdk. I'm using version 1.1.1.
For the payout to execute, you need the client() method that is listed above in this post, and this CreatePayout() method listed below.
public async static Task<HttpResponse> CreatePayout()
{
var body = new CreatePayoutRequest()
{
SenderBatchHeader = new SenderBatchHeader()
{
EmailMessage = "Congrats on recieving 1$",
EmailSubject = "You recieved a payout!!"
},
Items = new List<PayoutItem>()
{
new PayoutItem()
{
RecipientType="EMAIL",
Amount=new Currency()
{
CurrencyCode="USD",
Value="1",
},
Receiver="sb-r43z1e9186231#business.example.com",
}
}
};
PayoutsPostRequest request = new PayoutsPostRequest();
request.RequestBody(body);
var response = await client().Execute(request);
var result = response.Result<CreatePayoutResponse>();
Debug.WriteLine($"Status: {result.BatchHeader.BatchStatus}");
Debug.WriteLine($"Batch Id: {result.BatchHeader.PayoutBatchId}");
Debug.WriteLine("Links:");
foreach (PayoutsSdk.Payouts.LinkDescription link in result.Links)
{
Debug.WriteLine($"\t{link.Rel}: {link.Href}\tCall Type: {link.Method}");
}
return response;
}
Of course I'll add parameters to the method for email, amount, currency code, email message, and subject.
Right now, I am calling this method from the controller method like this: var result = Helpers.Paypal.CaptureOrderSample.CreatePayout(); where Helpers.Paypal are folders that contain a class called CaptureOrderSample, which I will probably rename.
To send money from your account to another account, there are several different options:
Automate the sending with the Payouts API or Payouts Web (spreadsheet upload). For live, payouts can only be used if the live account sending the payment is approved for payouts.
Log into the account that is going to send the money in https://www.paypal.com or https://www.sandbox.paypal.com and click on the menu for Pay & Get Paid -> Send Money .
Use a PayPal Checkout integration, with or without the Orders API, and specify a payee that is to receive the money. You must log in with the paying (sending) account to approve the sending, and finally the order must be captured (via API or client side actions.order.capture()) which is what results in a PayPal transaction. If the final capture step is not performed, no money will be sent and the order will merely remain created or approved and eventually expire (72 hours after creation or 3 hours after approval)
In the sandbox, no actual emails are sent with notifications. Instead, the developer.paypal.com dashboard has a "Notifications" tab on the left, and of course activity will also be visible in each sandbox account by logging into the account. Only captured activity is likely to be visible.

Get product list from Huawei In app purchase

So, i want to get my products list from my Huawei developer account, i integrate the sdk and all it's fine. the issue here in this code :
Future<List<ProductInfo>> getConsumableProducts() async {
try {
ProductInfoReq req = ProductInfoReq(); // The named parameter 'skuIds' is required, but there's no corresponding argument.Try adding the required argument.dartmissing_required_argumentThe named parameter 'priceType' is required, but there's no corresponding argument.Try adding the required argument.dartmissing_required_argument
req.priceType = IapClient.IN_APP_CONSUMABLE;
req.skuIds = ["prod_01", "prod_02"];
ProductInfoResult res = await IapClient.obtainProductInfo(req);
return res.productInfoList; // A value of type 'List<ProductInfo>?' can't be returned from the method 'getConsumableProducts' because it has a return type of 'Future<List<ProductInfo>>'
} on PlatformException catch (e) {
log(e.toString());
return null;
}
}
i mention the issues in the code. P.S : i made a search and verify that this code is the same code which public
If you want to obtain purchase information, you could follow the code below:
List<String> productIdList = new ArrayList<>();
// Only those products already configured in AppGallery Connect can be queried.
productIdList.add("ConsumeProduct1001");
ProductInfoReq req = new ProductInfoReq();
// priceType: 0: consumable; 1: non-consumable; 2: subscription
req.setPriceType(0);
req.setProductIds(productIdList);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the obtainProductInfo API to obtain the details of the product configured in AppGallery Connect.
Task<ProductInfoResult> task = Iap.getIapClient(activity).obtainProductInfo(req);
Check here for more info.