-
Notifications
You must be signed in to change notification settings - Fork 15
/
PaypalController.php
157 lines (119 loc) · 4.73 KB
/
PaypalController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php namespace App\Http\Controllers\PaymentGateway;
use Illuminate\Auth\Authenticatable;
use \PayPal\Api\Details;
use \PayPal\Api\Item;
use \PayPal\Api\ItemList;
use \PayPal\Api\Payer;
use \PayPal\Api\Payment;
use \PayPal\Api\RedirectUrls;
use \PayPal\Api\Transaction;
use \PayPal\Rest\ApiContext;
use \PayPal\Api\CreditCard;
use \App\BlockchainLite\Blockchain;
use Illuminate\Http\Request;
class PaypalController extends \App\Http\Controllers\Controller {
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This controller renders your application's "dashboard" for users that
| are authenticated. Of course, you are free to change or remove the
| controller as you wish. It is just here to get your app started!
|
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
header("Content-type: application/json");
}
/**
* Show the application dashboard to the user.
*
* @return Response
*/
public function PurchaseUSD(Request $request)
{
$apiContext = null;
// Process PayPal API Payment
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'', // ClientID
'' // ClientSecret
)
);
$card = new \PayPal\Api\PaymentCard();
$card->setType("visa")
->setNumber("4669424246660779")
->setExpireMonth("11")
->setExpireYear("2019")
->setCvv2("012")
->setFirstName("Joe")
->setBillingCountry("US")
->setLastName("Shopper");
$fi = new \PayPal\Api\FundingInstrument();
$fi->setPaymentCard($card);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));
$item1 = new \PayPal\Api\Item();
$item1->setName('USD')
->setDescription('USD-VIRTUAL, ZENX TRADING')
->setCurrency('USD')
->setQuantity(1)
->setTax(0.0)
->setPrice($request->USD);
$itemList = new \PayPal\Api\ItemList();
$itemList->setItems(array($item1));
$details = new \PayPal\Api\Details();
$details->setShipping(0)
->setTax(0)
->setSubtotal($request->USD);
$amount = new \PayPal\Api\Amount();
$amount->setCurrency("USD")
->setTotal($request->USD)
->setDetails($details);
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("USD, ZENX TRADING - VCASH")
->setInvoiceNumber(uniqid());
$payment = new \PayPal\Api\Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
} catch (Exception $ex) {
die($ex->getData());
//ResultPrinter::printError('Create Payment Using Credit Card. If 500 Exception, try creating a new Credit Card using <a href="https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1413">Step 4, on this link</a>, and using it.', 'Payment', null, $request, $ex);
exit(1);
}
if($payment->state == "approved"){ // If payment was approved
$lastFunding = \App\Exchange\FundSource::where(array('uid' => \Auth::user()->id, 'currency' => 'usd'))->orderBy('id', 'desc')->take(1)->get()->first();
$newFunds = new \App\Exchange\FundSource();
$newFunds->uid = \Auth::user()->id;
$newFunds->currency = "usd";
$newFunds->total_funds = +$request->USD;
$newFunds->funds_remaining = $request->USD + ($lastFunding != null ? $lastFunding->funds_remaining : 0);
$newFunds->previous_hash = ($lastFunding != null ? $lastFunding->hash : str_repeat("0",60));
$newFunds->hash = hash('sha256', json_encode($newFunds));
$newFunds->save();
$blkdata = (array) $newFunds;
$blkexists = file_exists("/var/www/blockchain/user_".$newFunds->uid.".dat");
$te = json_encode( $blkdata );
// Add to Blockchain
\App\BlockchainLite\Blockchain::addblock("/var/www/blockchain/user_".$newFunds->uid.".dat", $te, !$blkexists);
}
return [];
}
public static function transactions()
{
return \App\Exchange\FundSource::where('uid', \Auth::user()->id)->get();
}
}