1
00:00:05,950 --> 00:00:06,970
Hey what's up everyone.

2
00:00:06,970 --> 00:00:14,130
Mark Price here at DEF slopes dot com and let's go ahead and purchase some product.

3
00:00:14,320 --> 00:00:14,620
OK.

4
00:00:14,620 --> 00:00:19,020
So what we're going to do is here in our purchased MANAGER We're going to create a function that our

5
00:00:19,020 --> 00:00:22,990
view controller can call to actually start or initiate a purchase process.

6
00:00:23,050 --> 00:00:29,280
So let's create a function here called purchase remove ads.

7
00:00:29,290 --> 00:00:30,230
All right.

8
00:00:30,880 --> 00:00:35,290
And what we're going to do is make sure first that a user can actually make payment.

9
00:00:35,290 --> 00:00:40,590
So if Eskay payment queue that can make payments OK.

10
00:00:40,640 --> 00:00:45,190
So to keep in mind is there maybe multiple reasons why someone cannot make a payment.

11
00:00:45,190 --> 00:00:47,260
Maybe they haven't updated their credit card information.

12
00:00:47,260 --> 00:00:52,450
Maybe there's something wrong with their account maybe it's a child on a phone that's had a parent lock

13
00:00:52,450 --> 00:00:53,070
on it.

14
00:00:53,080 --> 00:00:58,510
There's multiple reasons so you always need to make sure that a payment can be made before you actually

15
00:00:58,510 --> 00:01:01,930
make the payment it will save time and headache because it goes back to server you won't get a failure.

16
00:01:01,930 --> 00:01:07,450
So just make sure that they can make the payment first and let's make sure that products that count

17
00:01:07,450 --> 00:01:08,410
is greater than zero.

18
00:01:08,410 --> 00:01:13,270
So you can make a purchase unless you have the products that you received when you fetched him.

19
00:01:13,270 --> 00:01:15,490
Remember you can't make a purchase unless you've already fetched them.

20
00:01:15,490 --> 00:01:19,360
So we have to make sure we have one there just to be extra safe.

21
00:01:19,360 --> 00:01:19,900
All right.

22
00:01:20,230 --> 00:01:27,280
So let's grab the product that we want so remove ads product equals products zero.

23
00:01:27,700 --> 00:01:30,420
OK so we're grabbing the first item out of the products array.

24
00:01:30,440 --> 00:01:36,300
And again there's only one item here but you do receive your products in a particular order.

25
00:01:36,370 --> 00:01:43,240
So when you receive products as they're listed in iTunes so they are sent down to you here in your app

26
00:01:43,240 --> 00:01:49,030
so you can always be sure that the index and the rate that you're grabbing from is relative to what

27
00:01:49,030 --> 00:01:50,100
is on iTunes.

28
00:01:50,170 --> 00:01:51,120
OK.

29
00:01:51,770 --> 00:01:57,630
OK so let payment equals Eskay payment.

30
00:01:59,060 --> 00:02:05,260
What we're going to do is we're going to create a payment now and we're going to use the Move ads product

31
00:02:05,260 --> 00:02:09,610
so the product just has meta data about your product but a payment is what's actually going to process

32
00:02:09,610 --> 00:02:10,160
your payment.

33
00:02:10,210 --> 00:02:11,560
OK.

34
00:02:11,800 --> 00:02:14,470
And then what we're going to do is S.K. payment.

35
00:02:14,490 --> 00:02:17,090
Q default.

36
00:02:17,270 --> 00:02:21,350
AD So we're going to add an observer ourself as an observer.

37
00:02:21,460 --> 00:02:22,320
OK.

38
00:02:22,770 --> 00:02:34,510
And what we're going to do is as payment scale payment queue default add and we're going to add a payment

39
00:02:34,540 --> 00:02:34,950
now.

40
00:02:35,080 --> 00:02:39,850
So two different add phunk even though it's the same ad we're adding different things here.

41
00:02:39,850 --> 00:02:44,920
So the first thing we're doing is adding this the this class purchased manager as an observer is going

42
00:02:44,920 --> 00:02:49,840
to listen okay and then what's going to happen is we're actually going to add the payment to start that

43
00:02:49,840 --> 00:02:50,260
request.

44
00:02:50,260 --> 00:02:53,680
And the moment that you add this year it's going to start processing that payment request.

45
00:02:53,690 --> 00:02:56,390
OK you with me so far.

46
00:02:56,500 --> 00:03:01,150
So it's going up to the magical server and doing some magical things and then it's going to come back

47
00:03:01,150 --> 00:03:03,220
to your app and give you a response.

48
00:03:03,220 --> 00:03:03,540
Okay.

49
00:03:03,550 --> 00:03:09,940
And that we do have a way to handle that and that's here in the updated transactions function that we

50
00:03:09,940 --> 00:03:15,640
implemented earlier which is part of the S-K payment transaction observer protocol.

51
00:03:16,050 --> 00:03:23,200
Okay let's go ahead and just get this going for each transaction in Tran's actions.

52
00:03:23,410 --> 00:03:23,880
Okay.

53
00:03:24,040 --> 00:03:28,990
So when the payment goes through there's one or more transactions that happened so you know you can

54
00:03:28,990 --> 00:03:33,370
make multiple payments at the same time and it will have one or more transactions in there.

55
00:03:33,370 --> 00:03:33,910
Okay.

56
00:03:34,030 --> 00:03:38,320
And so what we're going to do is iterate through all the transactions have actively been actively been

57
00:03:38,320 --> 00:03:42,250
going through when the callback is called here and then we're going to do something with it.

58
00:03:42,250 --> 00:03:49,210
So let's go ahead and do a switch statement transaction trends action state.

59
00:03:49,290 --> 00:03:55,030
So let's see what the state of the transaction is and what we're going to do is say if the case is purchased.

60
00:03:55,060 --> 00:03:59,010
So if the item was successfully purchased what do we want to do.

61
00:03:59,020 --> 00:04:03,700
All right and let's get the other let's get the other cases handled here as well too.

62
00:04:03,730 --> 00:04:08,500
We're going to break if the case is not felt.

63
00:04:08,590 --> 00:04:09,830
What do we want to do.

64
00:04:10,140 --> 00:04:10,490
Okay.

65
00:04:10,510 --> 00:04:17,720
And if the case is restored like when someone's restoring an EP purchase what do we want to do.

66
00:04:17,780 --> 00:04:24,310
And then let's just put in a default here case default or not a case just the word default and we're

67
00:04:24,310 --> 00:04:25,940
just going to break for now.

68
00:04:26,370 --> 00:04:27,160
OK.

69
00:04:27,490 --> 00:04:28,890
Restored with a D.

70
00:04:28,960 --> 00:04:30,630
That's why that's yelling at us here.

71
00:04:31,030 --> 00:04:31,390
OK.

72
00:04:31,420 --> 00:04:35,600
So it's handled the good one first which is something that was purchased.

73
00:04:35,620 --> 00:04:35,920
All right.

74
00:04:35,920 --> 00:04:39,550
So first things first is we're going to

75
00:04:42,190 --> 00:04:43,230
finish our transaction.

76
00:04:43,240 --> 00:04:45,150
So we need to let the service know that we're done.

77
00:04:45,340 --> 00:04:47,290
So we're going to S.K. payment.

78
00:04:47,290 --> 00:04:55,750
Q default finish transaction completes a pending transaction and we're going to pass in the transaction.

79
00:04:56,010 --> 00:04:56,800
Okay.

80
00:04:57,460 --> 00:05:02,900
Maybe you're thinking well why are we manually finishing this doesn't Apple know when it's done.

81
00:05:05,810 --> 00:05:12,020
Well Apple knows when it's done but what the important thing to know is let's say it failed and we didn't

82
00:05:12,020 --> 00:05:16,520
want to finish it quite that we could actually reprocess it again and maybe even do two or three attempts

83
00:05:16,520 --> 00:05:19,500
before we actually consider it done.

84
00:05:19,520 --> 00:05:21,130
And we say hey users nothing we can do.

85
00:05:21,140 --> 00:05:24,830
So in this case we're just going to finish it because it was successful which is great

86
00:05:27,830 --> 00:05:33,480
if let Prod. ID the product ID equals.

87
00:05:33,920 --> 00:05:34,970
Interesting actually.

88
00:05:35,150 --> 00:05:38,520
So how do we know which product to actually do.

89
00:05:38,700 --> 00:05:49,390
So what we should actually probably do here is say if transaction payment product identifier there it

90
00:05:49,390 --> 00:05:49,580
is.

91
00:05:49,590 --> 00:05:56,950
So if the product identifier that's coming through this transaction equals the remove ads product identifier.

92
00:05:56,950 --> 00:05:57,910
Then what do we want to do.

93
00:05:57,910 --> 00:06:00,420
We want to make the purchase right.

94
00:06:00,700 --> 00:06:05,620
So if payment dot product identifier goes in a purchase ads what do we want to do.

95
00:06:05,620 --> 00:06:14,080
Well we don't have the functionality yet in place for removing the ads and getting them off the screen

96
00:06:14,080 --> 00:06:15,390
etc. but we'll leave this here.

97
00:06:15,400 --> 00:06:20,700
This is our success block here so let's go actually write that code now.

98
00:06:22,500 --> 00:06:27,750
So let's think about this what we want to do is store something in the user defaults that lets the app

99
00:06:27,750 --> 00:06:34,670
know that we should not show ads so every time it loads we can remove those ads accordingly at runtime.

100
00:06:34,710 --> 00:06:39,330
So let's just set a boolean here or a value of some kind.

101
00:06:39,330 --> 00:06:45,080
I mean so user defaults not standard set.

102
00:06:45,210 --> 00:06:48,900
And what do we want to do we want to set a value for a key.

103
00:06:48,900 --> 00:06:56,590
Let's go ahead and set true for key and let's just do the in a purchase ID.

104
00:06:56,610 --> 00:07:00,210
So what we're doing here is saying if this key exists.

105
00:07:00,230 --> 00:07:00,700
OK.

106
00:07:00,870 --> 00:07:05,940
So if it's true for a specific name it means it's there it means we should not show the advertisements

107
00:07:05,960 --> 00:07:06,350
that's all.

108
00:07:06,360 --> 00:07:11,700
We're just setting a boolean here and it's really as simple as that how you work with in-app purchases

109
00:07:12,000 --> 00:07:16,300
is completely up to you how you manage them in your app and how you work with things move things around

110
00:07:16,300 --> 00:07:19,260
and show and not show it's all up to you 100 percent.

111
00:07:19,260 --> 00:07:21,570
You've got to use the logic to figure things out.

112
00:07:21,850 --> 00:07:22,940
OK so it's cool.

113
00:07:22,950 --> 00:07:28,500
So let's go ahead and just finish the transaction actually in both these cases here the felt and the

114
00:07:28,500 --> 00:07:30,490
restored.

115
00:07:30,870 --> 00:07:35,680
We're not going to do any retries or anything like that.

116
00:07:36,520 --> 00:07:36,890
Cool.

117
00:07:36,900 --> 00:07:39,650
So we finished the transaction make the purchase.

118
00:07:39,660 --> 00:07:42,770
Okay great problem.

119
00:07:42,870 --> 00:07:47,860
How do we how does our view controller know first off when this is done.

120
00:07:47,940 --> 00:07:52,560
And we haven't even started an actual payment to be processed yet we haven't clicked on a button and

121
00:07:52,560 --> 00:07:57,830
said start the process or purchase the purchase or remove the ads.

122
00:07:57,830 --> 00:07:59,720
We haven't even written that code yet.

123
00:07:59,760 --> 00:08:03,090
And also then there's the notion of when you do press that and has to go to the server How does the

124
00:08:03,090 --> 00:08:05,180
view controller know when it's succeeded or failed.

125
00:08:05,190 --> 00:08:07,360
We don't have any way of letting the controller know.

126
00:08:07,680 --> 00:08:10,120
So we're going to need to do that as well too.

127
00:08:10,170 --> 00:08:12,840
And so let's call this video done and then we'll do that in the next video.

128
00:08:12,840 --> 00:08:18,070
So in this video we what we done was we actually made it so you could purchase the product.

129
00:08:18,090 --> 00:08:25,200
And then we handle what happens when it is purchased which is storing a boolean that has been purchased

130
00:08:25,200 --> 00:08:29,970
so when the app loads the next time we can remove that advertisement.

131
00:08:29,970 --> 00:08:31,850
So good stuff here.

132
00:08:31,860 --> 00:08:32,910
Let's move on to Ford.

133
00:08:32,910 --> 00:08:34,690
Mark Price of slopes dot.com.

