gpt4 book ai didi

php - 使用 Laravel 的 PayPal API - 更新数据

转载 作者:可可西里 更新时间:2023-10-31 22:52:15 27 4
gpt4 key购买 nike

我正在尝试使用 Laravel 5.1 实现 PayPal 支付API。但是当我登录 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.');
}

}

我还评论了this link但它似乎无法回答我的问题。此外,如果该国家/地区有一个省会怎样?我们如何添加它?

更新 1

  1. 添加了新的 Patch() 类。
  2. Try Catch 中编辑代码。

注意:接受的答案也会收到赏金up

更新 2 教程

  1. 对于 PHP/Laravel(我目前使用的是 v5.1),安装这个包 paypal/rest-api-sdk-php

  2. Create Sandbox account in PayPal .选择Buy with Paypal .

  3. 继续,直到看到选项,选择 Shop the world .

  4. 登录developer.paypal.com .

  5. 点击Accounts .点击Create Account .

  6. 选择您想要的国家。在账户类型中选择个人(买家账户)。

  7. 添加邮箱,避免使用-。请改用 _

  8. 输入您想要的 PayPal 余额。

  9. 点击创建账户。

让它上线?

https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live

最佳答案

创建付款后,尝试更新地址,如 this example 所示。 .

$paymentId = $createdPayment->getId();

$patch = new \PayPal\Api\Patch();
$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 \PayPal\Api\PatchRequest();
$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?

使用州代码 listed here .

关于php - 使用 Laravel 的 PayPal API - 更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com