我正在尝试实施API
of PayPal payment
with Laravel 5.1
。但是当我登录到PayPal (sandbox)
,它使用我在帐户中使用的地址,并且还使用 PayPal 帐户中的名称而不是我网站中的数据。这是我的问题。
我想使用我网站上的数据,因为如果我从我的网站输入送货地址(例如)而不使用它,那就没有意义了。请参阅下面我的代码以供参考(或在下面评论我的一些详细信息)。
class PaypalPaymentController extends BaseController
{
private $_api_context;
public function __construct(){
$paypal_conf = \Config::get('paypal');
$this->_api_context = new ApiContext(new OAuthTokenCredential(
$paypal_conf['client_id'],
$paypal_conf['secret']
));
$this->_api_context->setConfig($paypal_conf['settings']);
}
public function payWithPaypal(Request $request){
$payer = new Payer;
$payer->setPaymentMethod('paypal');
$price = 0;
switch($request->get('amount')) {
case '10 books':
$price = 6200;
break;
case '20 books':
$price = 12200;
break;
case '50 books':
$price = 25200;
break;
default:
return redirect()
->route('bookstore.shipping')
->with('danger', 'Please select the right amount of book/s.');
break;
}
$item1 = new Item();
$item1->setName($request->get('amount'))
->setCurrency('PHP')
->setQuantity(1)
->setPrice($price);
$item_list = new ItemList();
$item_list->setItems([$item1]);
$amount = new Amount();
$amount->setCurrency('PHP')
->setTotal($price);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Books transaction');
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(route('bookstore.payment-status'))
->setCancelUrl(route('bookstore.payment-status'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions([$transaction]);
$patchReplace = new Patch();
$patchReplace->setOp('add')
->setPath('/transactions/0/item_list/shipping_address')
->setValue(json_decode('{
"line1": "345 Lark Ave",
"city": "Montreal",
"state": "QC",
"postal_code": "H1A4K2",
"country_code": "CA"
}'));
$patchRequest = (new PatchRequest())->setPatches([$patchReplace]);
try{
$payment->create($this->_api_context);
$payment->update($patchRequest, $this->_api_context);
} catch(\Palpal\Exception\PPConnectionException $e){
if(\Config::get('app.debug')){
return redirect()
->route('bookstore.shipping')
->with('danger', 'Connection Timeout.');
}
return redirect()
->route('bookstore.shipping')
->with('danger', 'Some error occured, sorry for the inconvenience.');
}
foreach($payment->getLinks() as $link){
if($link->getRel() == 'approval_url'){
$redirect_url = $link->getHref();
break;
}
}
Session::put('paypal_payment_id', $payment->getId());
if(isset($redirect_url)){
return Redirect::away($redirect_url);
}
return redirect()
->route('bookstore.shipping')
->with('danger', 'Unknown error occured.');
}
public function getPaymentStatus(){
$payment_id = Session::get('paypal_payment_id');
Session::forget('paypal_payment_id');
if(empty(Input::get('PayerID')) || empty(Input::get('token'))){
return redirect()
->route('bookstore.shipping')
->with('danger', 'Payment failed.');
}
$payment = Payment::get($payment_id, $this->_api_context);
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
$result = $payment->execute($execution, $this->_api_context);
if($result->getState() == 'approved'){
// Send Email
$email_data = [
'number_of_books' => $payment->transactions[0]->item_list->items[0]->name,
'shipping' => [
'street' => $payment->payer->payer_info->shipping_address->line1,
'city' => $payment->payer->payer_info->shipping_address->city,
'state' => $payment->payer->payer_info->shipping_address->state,
'country' => $payment->payer->payer_info->shipping_address->country_code,
]
];
// Send email function here ...
return redirect()
->route('bookstore.shipping')
->with('success', 'Transaction payment success!');
}
return redirect()
->route('bookstore.shipping')
->with('danger', 'Payment failed.');
}
}
我还回顾了这个链接 https://stackoverflow.com/questions/39927124/paypal-php-set-no-shipping但它似乎无法回答我的问题。还,what if
这个国家有一个省吗?我们怎样才能添加它呢?
Update 1
- 新增
Patch()
class.
- 编辑后的代码在
Try Catch
.
注:accepted
答案也会收到bounty
加上up
.
更新 2 带教程
对于 PHP/Laravel(我目前使用的是 v5.1),安装这个包贝宝/rest-api-sdk-php https://github.com/paypal/PayPal-PHP-SDK
在 PayPal 中创建沙盒帐户 https://www.sandbox.paypal.com/ph/webapps/mpp/merchant。选择.
继续,直到看到选项,选择购物世界 https://www.paypal.com/ph/webapps/mpp/online-shopping.
登录到开发者.paypal.com https://developer.paypal.com/.
Click Accounts https://developer.paypal.com/developer/accounts/. Click 创建账户 https://developer.paypal.com/developer/accounts/create.
选择您想要的国家。在账户类型中选择个人(买家账户)。
添加电子邮件地址,避免使用-
. Use _
反而。
输入您想要的 PayPal 余额。
单击创建帐户。
让它活起来吗?
https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live