Now, in the last lesson, we've written the code that's required to trigger a paymentRequest in the payment 1
queue to the Apple App Store, 2
and we specified the product that we want to purchase or the user wants to purchase. And we set up 3
this paymentQueue 4
updated transaction delegate method which observes for any changes in the paymentQueue and lets us know 5
when a transaction is purchased and successful or when it's failed. 6
So, now we are going to create a sandbox user in order to test this without having to pay every single 7
time we test it. 8
So let's go into, again, 9
Apo Store Connect and we're going to go to Users and Roles here. 10
So just click on this dropdown list to head over there. 11
And if you head over to sandbox testers, then you can create some new testers by clicking on this plus 12
button. Now, rather awkwardly, 13
Apple actually enforces very strong data validation in the sandbox user creation and I really wish they 14
didn't, 15
because often, I'm forced to create 10 or 20 of these users. And for every single one of them, you have 16
to fill out pretty much every single field. 17
So it doesn't really matter what you give the first name or the last name. 18
I usually just write something like Test Test. But the email has to be unique and it has to be a real 19
email, which is quite difficult, if you're not a professional spammer. 20
How would you have access to even 10 emails that you own? 21
So here's a trick. 22
If you have a Gmail address that is, say, Angela@gmail.com, 23
this, unfortunately, is not my email because I wasn't there fast enough, need to be an earlier adopter. 24
But if, say, this was your e-mail, then Gmail actually has a really neat thing called address alias, and 25
what allows you to do is just before the at sign, 26
you can add a plus and you can write some things like news letter, 27
right? 28
And you can use this e-mail for all of the news letters that you sign up to, 29
and you can filter on this incoming e-mail address in your Gmail inbox. 30
But even though it looks a little bit weird, it still goes to your e-mail address, your inbox. 31
So Angela@gmail.com's inbox in this case. 32
Now, what I often like to do is sometimes when I sign up to something I'm not really that sure about, 33
you know, say, if I sign up to American Express, right? I will actually sign up with my e-mail, plus the 34
address that I signed up to at gmail.com. 35
And this is a really easy way to know which businesses are selling your e-mails for money and to just 36
a protest. But this is a really neat trick. And this is helpful in our case because now you have an infinite 37
variety of unique email addresses, so you can just keep creating new aliases for every single one of 38
your testers. 39
Now, there's just one caveat. 40
The main e-mail for your e-mail alias cannot already belong to an Apple ID. 41
So, say, if you sign up to your Apple ID with this e-mail address, you can't use an alias on it. 42
It doesn't work. 43
But if you haven't used this e-mail, this Gmail address of any Apple IDs, then you can do this quite easily. 44
Now, another option, if you don't have many e-mails that you can use for your sandbox testers is use 45
something like maildrop.cc, and this allows you to just generate a random mailbox, and it'll be able 46
to receive messages, and you'll be able to verify the account. 47
But you, obviously, don't monitor it and it doesn't have a painful creation process. 48
But what it does mean though is that it is a public account. 49
So if you go to some of the ones that somebody might have already used, say, John@maildrop.cc, 50
then you can see that this has all of the stuff that they've previously signed up to, 51
and there's a lot of spam in here, 52
right? 53
So if you use any of these account on maildrop.cc, remember that it's completely public. Do not 54
sign up for anything that has your private details in it using the service. 55
But if you're using it for just testing, then it doesn't really matter because there's nothing sensitive 56
that's going to be sent to it. 57
So in my case, I'm just going to generate a new e-mail address on our website domain 58
that is called tester134@londonappbrewery.com. 59
And the password, rather awkwardly, also has very strict security rules, 60
so you need one capital letter and some numbers and it needs to be quite long, so come up with some sort 61
of combination for that. 62
And the secret question and secret answer, you can take it very seriously if you want to, 63
but I tend to just write 64
TesterA. 65
TesterQ 66
because it has to be more than six characters or something like that. And date of birth, of course, just 67
some random numbers. And App Store territory, 68
you should choose the App Store that you're testing device is currently signed into. 69
So, for example, I live in the UK, so my App Store, my native App Store, that my testing device is normally 70
set to is the UK territory, so I'll go and find United Kingdom and add that in there. 71
If your device is normally signed into United States territory, then be sure to select that. And once 72
you're done, go ahead and click save, and you'll have this brand-new sandbox user ready to go. 73
So, now we are ready to run our app onto our physical device which is the phone, and we're going to make 74
sure that the team is selected so that it will side load, and I'm going to plug in my phone through USB, 75
and I'm going to select my device as the destination and run this app. 76
So, now while the app is running, I'm going to click on the home button and I'm going to go into Settings. 77
And inside Settings, I'm going to scroll down to iTunes and App Store, and I'm going to scroll all the 78
way down to the bottom where it says sandbox account. 79
And here I'm going to click on it and a sign out of any previous sandbox accounts you've got going on 80
and I'm going to sign in with that new user that I just created. 81
He was called tester134@londonappbrewery.com and I'm going to enter the password of that 82
user that I set up just now, and click Sign In. 83
So now my sandbox account is showing that my tester134 is now signed in, 84
and I'm going to go ahead and relaunch that app InspoQuotes. 85
Now, you can rerun it from Xcode as long as you haven't force quit the app. 86
This is still a active session. 87
So, now if I click on Get More Quotes, then you can see I get this pop-up "Enter your password 88
to confirm your In-App Purchase of 1 Premium Quotes for 0.99p.. 89
So I'm going to enter my password for my test user and I'm going to click Buy. And now, we wait for a 90
little bit, and you can see that I get a pop-up that says, "You are all set. Your purchase was successful" in 91
the app. And in the debug console, 92
I get that print statement "Transaction successful" which means that if you look inside quoteTableViewController, 93
the part where that transaction successful gets printed is inside this if statement, 94
if transaction.transactionState == .purchased. 95
So a successful transaction, then print this. 96
So,now you can see that we can change the quote inside this block to give the user whatever it is that 97
they paid for: extra coins, extra fish, extra quotes 98
in this case. Then we can implement that right here. 99
So way, it's actually working. 100
Now, there's just one thing to remember that if you make a purchase that's been successful with a sandbox 101
user, that user won't be able to make the same purchase again if your in-app purchase is a non-consumable product. 102
So remember that when we created our in-app purchase Premium Quotes, we created it as a non-consumable 103
product because the users shouldn't need to buy it multiple times. 104
They should only pay once to get rid of the ads or pay wants to get the extra quotes and never again. 105
And the way that Apple implements it is that your sandbox users can only purchase a non-consumable 106
product once in their whole lifetime. 107
They can restore purchases that they bought before, but they can't keep buying the same non-consumable 108
product. 109
So this is why you end up creating loads and loads of test users and sandbox accounts, 110
and this is also why you need some alternative emails in order to do this. 111
So, now we have written the code that implements the basic functionality of creating a new paymentRequest 112
and adding it to the payment queue to ask the user for their password and to purchase that in-app purchase. 113
And we've got this delegate method that listens for when that successful and when it's not. 114
And we created a sandbox user that can allow us to test these payments without having to cover and 115
we can activate blocks of our code when the transaction is successful or when the transaction has failed. 116
Now, once the payments have been successful or they have failed, we need to end our transaction. 117
So we're not still holding onto the same transaction. 118
So inside the if statement when our transaction has completed, we're going to write SKPaymentQueue 119
.default() 120
.finishTransaction, and the transaction that we want to terminate is the current transaction 121
that is in question. And we're going to copy that code over to here as well. 122
And finally, even though we are checking to see if there was an error, we're not actually being very explicit 123
with what the error was. 124
And with SKPaymentQueue and StoreKit it, we can actually get a really accurate description of what the 125
error was. 126
So it can guide our debugging if we do get any problems. 127
So let's use optional binding to say if let error = transaction.error. 128
So if there was an error, if it's not nil, then let errorDescription = error.localizedDescription. 129
So instead of just printing "Transaction failed!" I'm going to move that down here and we're going to print 130
"Transaction failed due to error." 131
And I'm going to insert that errorDescription into here. 132
So, now that's a little bit more comprehensive. 133
And that will be really useful when we do have some payment issues that we're testing. 134
So that, in a nutshell, is how you implement in-app purchases. In the next lesson, 135
we're going to enable the user to be able to access the rest of the premiumQuotes in addition to the 136
free quotes after the purchase has been successful. 137
So for all of that and more, I'll see you on the next lesson.