PayPal API with Laravel - Updating of data(带有 Laravel 的 PayPal API - 数据更新)
问题描述
我正在尝试使用 Laravel 5.1
实现 PayPal 支付
的 API
.但是当我登录到 PayPal (sandbox)
时,它使用我在我的帐户中使用的地址,并且它使用来自 PayPal 帐户的名称而不是来自我网站的数据.那是我的问题.
我想使用我网站上的数据,因为如果我从我的网站输入送货地址(例如)而不使用它是没有意义的.请参阅下面的代码以供参考(或在下面评论我的一些详细信息).
class PaypalPaymentController 扩展 BaseController{私人 $_api_context;公共函数 __construct(){$paypal_conf = Config::get('paypal');$this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'],$paypal_conf['秘密']));$this->_api_context->setConfig($paypal_conf['settings']);}公共函数 payWithPaypal(Request $request){$payer = 新付款人;$payer->setPaymentMethod('paypal');$价格 = 0;switch($request->get('amount')) {案例10本书":$价格 = 6200;休息;案例20本书":$价格 = 12200;休息;案例50本书":$价格 = 25200;休息;默认:返回重定向()->route('bookstore.shipping')->with('danger', '请选择合适数量的书/秒.');休息;}$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('图书交易');$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 = 新补丁();$patchReplace->setOp('add')->setPath('/transactions/0/item_list/shipping_address')->setValue(json_decode('{"line1": "345 Lark Ave","city": "蒙特利尔","状态": "QC","postal_code": "H1A4K2","country_code": "CA"}'));$patchRequest = (new PatchRequest())->setPatches([$patchReplace]);尝试{$payment->create($this->_api_context);$payment->update($patchRequest, $this->_api_context);} catch(PalpalExceptionPPConnectionException $e){if(Config::get('app.debug')){返回重定向()->route('bookstore.shipping')->with('danger', 'Connection Timeout.');}返回重定向()->route('bookstore.shipping')->with('danger', '出现错误,给您带来不便敬请谅解.');}foreach($payment->getLinks() as $link){if($link->getRel() == 'approval_url'){$redirect_url = $link->getHref();休息;}}Session::put('paypal_payment_id', $payment->getId());if(isset($redirect_url)){返回重定向::离开($redirect_url);}返回重定向()->route('bookstore.shipping')->with('danger', '发生未知错误.');}公共函数 getPaymentStatus(){$payment_id = Session::get('paypal_payment_id');会话::忘记('paypal_payment_id');if(empty(Input::get('PayerID')) || empty(Input::get('token'))){返回重定向()->route('bookstore.shipping')->with('danger', '付款失败.');}$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'){//发送电子邮件$email_data = ['number_of_books' =>$payment->transactions[0]->item_list->items[0]->name,'运输' =>['街道' =>$payment->payer->payer_info->shipping_address->line1,'城市' =>$payment->payer->payer_info->shipping_address->city,'状态' =>$payment->payer->payer_info->shipping_address->state,'国家' =>$payment->payer->payer_info->shipping_address->country_code,]];//在这里发送电子邮件函数...返回重定向()->route('bookstore.shipping')->with('success', '交易支付成功!');}返回重定向()->route('bookstore.shipping')->with('danger', '付款失败.');}}
我还查看了此链接,但它似乎无法回答我的问题.另外,如果
国家有一个省怎么办?我们如何添加?
更新 1
- 添加了新的
Patch()
类. - 编辑了
Try Catch
中的代码.
<小时>
注意:accepted
答案也将收到 bounty
加上 up
.
使用教程更新 2
对于 PHP/Laravel(我目前使用的是 v5.1),安装这个包paypal/rest-api-sdk-php
在 PayPal 中创建沙盒帐户.选择使用 Paypal 购买.
继续直到看到选项,选择购物世界.
登录developer.paypal.com.
点击账户.点击创建账户.
选择你想要的国家.在帐户类型中选择个人(买方帐户).
添加邮箱地址,避免使用
-
.改用_
.输入您想要的 PayPal 余额.
点击创建帐户.
让它成为现实?
https://github.com/paypal/PayPal-PHP-SDK/wiki/上线
创建付款后,尝试更新地址,如这个例子.
$paymentId = $createdPayment->getId();$patch = new PayPalApiPatch();$patch->setOp('add')->setPath('/transactions/0/item_list/shipping_address')->setValue([收件人姓名" =>格鲁内贝格,安娜",第 1 行" =>52 N Main St",城市" =>圣荷西",状态" =>"CA",邮政编码" =>"95112",国家代码" =>我们"]);$patchRequest = new PayPalApiPatchRequest();$patchRequest->setPatches([$patch]);$result = $createdPayment->update($patchRequest, $apiContext);
<块引用>
我也查看了此链接,但它似乎无法回答我的问题.另外,如果国家有一个省呢?我们如何添加?
使用此处列出的州代码.
I'm trying to implement the API
of PayPal payment
with Laravel 5.1
. But when I log in to PayPal (sandbox)
, it uses the address I used in my account, and also it uses the name from PayPal account not the data from my website. That's my problem.
I want to use the data from my website because it doesn't make sense if I enter the shipping address (for example) from my website and not using it. Please see my code below for reference (Or comment down below for some details from me).
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(PalpalExceptionPPConnectionException $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.');
}
}
I also reviewed this link but it seems like it cannot answer my problem. Also, what if
the country has a province? How can we add that?
Update 1
- Added new
Patch()
class. - Edited Code in
Try Catch
.
Note: The accepted
answer will also receive the bounty
plus the up
.
Update 2 with Tutorial
For PHP/Laravel (I'm currently using v5.1), install this package paypal/rest-api-sdk-php
Create Sandbox account in PayPal. Choose Buy with Paypal.
Continue until you see options, choose Shop the world.
Login to developer.paypal.com.
Click Accounts. Click Create Account.
Choose what country you want. Choose Personal (Buyer Account) in Account Type.
Add email address, avoid to use
-
. Use_
instead.Enter how much PayPal Balance you want.
Click Create Account.
Make it live?
https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live
After you have created the payment, try updating the address as shown in this example.
$paymentId = $createdPayment->getId();
$patch = new PayPalApiPatch();
$patch->setOp('add')
->setPath('/transactions/0/item_list/shipping_address')
->setValue([
"recipient_name" => "Gruneberg, Anna",
"line1" => "52 N Main St",
"city" => "San Jose",
"state" => "CA",
"postal_code" => "95112",
"country_code" => "US"
]);
$patchRequest = new PayPalApiPatchRequest();
$patchRequest->setPatches([$patch]);
$result = $createdPayment->update($patchRequest, $apiContext);
I also reviewed this link but it seems like it cannot answer my problem. Also, what if the country has a province? How can we add that?
Use the state codes listed here.
这篇关于带有 Laravel 的 PayPal API - 数据更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 Laravel 的 PayPal API - 数据更新
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01