[英]PayPal payment using credit card stored in vault PHP
I have an id of credit card details which is saved in paypal vault CARD-xxxxxxxxxxxxxxxxxx
.我有一个信用卡详细信息的 ID,它保存在贝宝保险库CARD-xxxxxxxxxxxxxxxxxx
。 I need to make a payment with this card using php-paypal-SDK.我需要使用 php-paypal-SDK 用这张卡付款。 For this purpose I have written bellow code.为此,我编写了波纹管代码。
<?php
require 'PayPal-PHP-SDK-master/vendor/autoload.php';
$clientId = 'xxxxxxxxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxx';
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Api\Payment;
use PayPal\Api\Payer;
use PayPal\Api\FundingInstrument;
use PayPal\Api\CreditCard;
use PayPal\Api\CreditCardToken;
use PayPal\Api\Transaction;
$sdkConfig = array(
"mode" => "sandbox"
);
$apiContext = $apiContext = new ApiContext(new OAuthTokenCredential(
$clientId,
$clientSecret
));
$credit_card_token = new CreditCardToken();
$credit_card_token->setCreditCardId('CARD-xxxxxxxxxxxxxxxxxx');
$fundinginstrument = new FundingInstrument();
$fundinginstrument->setCreditCardToken($credit_card_token);
$payer = new Payer();
$payer->setPaymentMethod('credit_card');
$payer->setFundingInstruments(array($fundinginstrument));
$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$transaction = new Transaction();
$transaction->setAmount('10.00');
$transaction->setDescription('Test payment');
$payment->setTransactions(array($transaction));
$request = clone $payment;
try {
$output = $payment->create($apiContext);
print_r($output);
} catch (PayPal\Exception\PayPalConnectionException $pce) {
echo '<pre>';print_r(json_decode($pce->getData()));exit;
exit(1);
}
But it throws error as follows但它抛出如下错误
MALFORMED_REQUEST - Incoming JSON request does not map to API request MALFORMED_REQUEST - 传入的 JSON 请求未映射到 API 请求
what was wrong with this code?This is the first time i am dealing with this SDK.这段代码有什么问题?这是我第一次处理这个 SDK。
If still not working then you can use curl
or any HTTP client.如果仍然无法正常工作,那么您可以使用curl
或任何 HTTP 客户端。 For the demonstration, I used GuzzleHttp为了演示,我使用了GuzzleHttp
$uri = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$clientId = 'CLIENT_ID';
$secret = 'CLIENT_SECRET';
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' =>
[
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => 'grant_type=client_credentials',
'auth' => [$clientId, $secret, 'basic'],
]
);
$data = json_decode($response->getBody(), true);
$access_token = $data['access_token'];
$payment_uri = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data = '{
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [
{
"credit_card_token": {
"credit_card_id": "CREDIT_CARD_ID"
}
}]
},
"transactions": [
{
"amount": {
"total": "10.00",
"currency": "USD"
},
"description": "Payment by vaulted credit card."
}]
}';
$payment = $client->request('POST', $payment_uri, [
'headers' =>
[
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
],
'body' => $data,
]
);
$transaction = json_decode($payment->getBody(), true);
echo "<pre>";
print_r($transaction);
Follow this link to read more details.按照此链接阅读更多详细信息。 If you need further help you can comment here or linkedin , github or instagram如果你需要进一步的帮助,你可以在这里发表评论或linkedin 、 github或instagram
Good luck祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.