1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elimuswift\Core\Traits; |
4
|
|
|
|
5
|
|
|
trait VerifiesPurchase |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Customer Entity. |
9
|
|
|
* |
10
|
|
|
* @var mixed |
11
|
|
|
**/ |
12
|
|
|
protected $customer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Verify validity of the purchase key and if valid create customer. |
16
|
|
|
**/ |
17
|
|
|
public function verifyPurchase() |
18
|
|
|
{ |
19
|
|
|
$this->getCustomer(); |
20
|
|
|
if (null != $this->customer) { |
21
|
|
|
return $this->validKeyResponse(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$response = app()->envatoapi->verifyPurchase(request('code')); |
25
|
|
|
if ('error' == $response['status']) { |
26
|
|
|
return $this->invalidKeyResponse(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// If we are here then the key is valid |
30
|
|
|
// We now create a new customer record from the envato api response |
31
|
|
|
$this->customer = $this->customers->createCustomer($response['response']); |
|
|
|
|
32
|
|
|
// After creation of customer details return the response back to the user |
33
|
|
|
return $this->validKeyResponse(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
//end verifyPurchase() |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return invalid key response. |
40
|
|
|
* |
41
|
|
|
* @return Illuminate\Http\Response |
42
|
|
|
**/ |
43
|
|
|
protected function invalidKeyResponse() |
44
|
|
|
{ |
45
|
|
|
return response()->json(['status' => 'error', 'description' => 'Invalid purchase key'], 403); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//end invalidKeyResponse() |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the customer object. |
52
|
|
|
**/ |
53
|
|
|
protected function getCustomer() |
54
|
|
|
{ |
55
|
|
|
$this->customer = $this->customers->findCustomer('purchaseKey', request()->get('code')); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//end getCustomer() |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return a success response if the purchase key is valid. |
62
|
|
|
* |
63
|
|
|
* @return Illuminate\Http\Response json object response |
64
|
|
|
**/ |
65
|
|
|
protected function validKeyResponse() |
66
|
|
|
{ |
67
|
|
|
return response()->json(['status' => 'success', 'description' => $this->customer->tojson()]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//end validKeyResponse() |
71
|
|
|
} |
72
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.