-
Notifications
You must be signed in to change notification settings - Fork 38
/
Paypal.php
175 lines (149 loc) · 5.06 KB
/
Paypal.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* File Paypal.php.
*
* @author Marcio Camello <[email protected]>
* @see https://github.com/paypal/rest-api-sdk-php/blob/master/sample/
* @see https://developer.paypal.com/webapps/developer/applications/accounts
*/
namespace marciocamello;
define('PP_CONFIG_PATH', __DIR__);
use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
use yii\base\Component;
use PayPal\Api\Address;
use PayPal\Api\CreditCard;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Api\FundingInstrument;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\RedirectUrls;
use PayPal\Rest\ApiContext;
class Paypal extends Component
{
//region Mode (production/development)
const MODE_SANDBOX = 'sandbox';
const MODE_LIVE = 'live';
//endregion
//region Log levels
/*
* Logging level can be one of FINE, INFO, WARN or ERROR.
* Logging is most verbose in the 'FINE' level and decreases as you proceed towards ERROR.
*/
const LOG_LEVEL_FINE = 'FINE';
const LOG_LEVEL_INFO = 'INFO';
const LOG_LEVEL_WARN = 'WARN';
const LOG_LEVEL_ERROR = 'ERROR';
//endregion
//region API settings
public $clientId;
public $clientSecret;
public $isProduction = false;
public $currency = 'USD';
public $config = [];
/** @var ApiContext */
private $_apiContext = null;
/**
* @setConfig
* _apiContext in init() method
*/
public function init()
{
$this->setConfig();
}
/**
* @inheritdoc
*/
private function setConfig()
{
// ### Api context
// Use an ApiContext object to authenticate
// API calls. The clientId and clientSecret for the
// OAuthTokenCredential class can be retrieved from
// developer.paypal.com
$this->_apiContext = new ApiContext(
new OAuthTokenCredential(
$this->clientId,
$this->clientSecret
)
);
// #### SDK configuration
// Comment this line out and uncomment the PP_CONFIG_PATH
// 'define' block if you want to use static file
// based configuration
$this->_apiContext->setConfig(ArrayHelper::merge(
[
'mode' => self::MODE_SANDBOX, // development (sandbox) or production (live) mode
'http.ConnectionTimeOut' => 30,
'http.Retry' => 1,
'log.LogEnabled' => YII_DEBUG ? 1 : 0,
'log.FileName' => Yii::getAlias('@runtime/logs/paypal.log'),
'log.LogLevel' => self::LOG_LEVEL_FINE,
'validation.level' => 'log',
'cache.enabled' => 'true'
],$this->config)
);
// Set file name of the log if present
if (isset($this->config['log.FileName'])
&& isset($this->config['log.LogEnabled'])
&& ((bool)$this->config['log.LogEnabled'] == true)
) {
$logFileName = \Yii::getAlias($this->config['log.FileName']);
if ($logFileName) {
if (!file_exists($logFileName)) {
if (!touch($logFileName)) {
throw new ErrorException('Can\'t create paypal.log file at: ' . $logFileName);
}
}
}
$this->config['log.FileName'] = $logFileName;
}
return $this->_apiContext;
}
//Demo
public function payDemo()
{
$addr = new Address();
$addr->setLine1('52 N Main ST');
$addr->setCity('Johnstown');
$addr->setCountryCode('US');
$addr->setPostalCode('43210');
$addr->setState('OH');
$card = new CreditCard();
$card->setNumber('4417119669820331');
$card->setType('visa');
$card->setExpireMonth('11');
$card->setExpireYear('2018');
$card->setCvv2('874');
$card->setFirstName('Joe');
$card->setLastName('Shopper');
$card->setBillingAddress($addr);
$fi = new FundingInstrument();
$fi->setCreditCard($card);
$payer = new Payer();
$payer->setPaymentMethod('credit_card');
$payer->setFundingInstruments(array($fi));
$amountDetails = new Details();
$amountDetails->setSubtotal('15.99');
$amountDetails->setTax('0.03');
$amountDetails->setShipping('0.03');
$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal('7.47');
$amount->setDetails($amountDetails);
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('This is the payment transaction description.');
$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
return $payment->create($this->_apiContext);
}
}