我正在关注this指南以整合paypal。我不希望用户重定向到paypal进行付款。所以我需要遵循直接付款方式。
我从该指南中了解到,首先我需要创建两个沙盒帐户(买方和商家)。然后在课堂上使用商家帐户的详细信息(USR,'PWD','SIGNATURE')。所以我创建了一个类(paypal.php),然后通过该类处理付款。我已经下载了该课程所需的cacert.pem
。
这是我的班级
<?php
class Paypal {
/**
* Last error message(s)
* @var array
*/
protected $_errors = array();
/**
* API Credentials
* Use the correct credentials for the environment in use (Live / Sandbox)
* @var array
*/
protected $_credentials = array(
'USER' => 'kanavk-facilitator_api1.ocodewire.com',
'PWD' => '1404460510',
'SIGNATURE' => 'A4sylwT.LsGOlR5e0Qos27RoSta5AKLvXCCjXXHcGN8Tor8.JxNZxIAs',
);
/**
* API endpoint
* Live - https://api-3t.paypal.com/nvp
* Sandbox - https://api-3t.sandbox.paypal.com/nvp
* @var string
*/
protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';
/**
* API Version
* @var string
*/
protected $_version = '74.0';
/**
* Make API request
*
* @param string $method string API method to request
* @param array $params Additional request parameters
* @return array / boolean Response array / boolean false on failure
*/
public function request($method,$params = array()) {
$this -> _errors = array();
if( empty($method) ) { //Check if API method is not empty
$this -> _errors = array('API method is missing');
return false;
}
//Our request parameters
$requestParams = array(
'METHOD' => $method,
'VERSION' => $this -> _version
) + $this -> _credentials;
//Building our NVP string
$request = http_build_query($requestParams + $params);
//cURL settings
$curlOptions = array (
CURLOPT_URL => $this -> _endPoint,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $request
);
$ch = curl_init();
curl_setopt_array($ch,$curlOptions);
//Sending our request - $response will hold the API response
$response = curl_exec($ch);
//Checking for cURL errors
if (curl_errno($ch)) {
$this -> _errors = curl_error($ch);
curl_close($ch);
return false;
//Handle errors
} else {
curl_close($ch);
$responseArray = array();
parse_str($response,$responseArray); // Break the NVP string to an array
return $responseArray;
}
}
}
?>
我创建了一个处理表单的脚本。包括该类并尝试使用虚拟输入处理付款。但是当我执行脚本时没有任何事情发生。
这是我用于处理表单的脚本
<?php
include("includes/config.php");
include("includes/paypal.php");
@session_start();
include("steps.php");
error_reporting(0);
$requestParams = array(
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
'PAYMENTACTION' => 'Sale'
);
$creditCardDetails = array(
'CREDITCARDTYPE' => 'Visa',
'ACCT' => '4929802607281663',
'EXPDATE' => '062012',
'CVV2' => '984'
);
$payerDetails = array(
'FIRSTNAME' => 'John',
'LASTNAME' => 'Doe',
'COUNTRYCODE' => 'US',
'STATE' => 'NY',
'CITY' => 'New York',
'STREET' => '14 Argyle Rd.',
'ZIP' => '10010'
);
$orderParams = array(
'AMT' => '500',
'ITEMAMT' => '496',
'SHIPPINGAMT' => '4',
'CURRENCYCODE' => 'GBP'
);
$item = array(
'L_NAME0' => 'iPhone',
'L_DESC0' => 'White iPhone, 16GB',
'L_AMT0' => '496',
'L_QTY0' => '1'
);
$paypal = new Paypal();
$response = $paypal -> request('DoDirectPayment',
$requestParams + $creditCardDetails + $payerDetails + $orderParams + $item
);
if( is_array($response) && $response['ACK'] == 'Success') { // Payment successful
// We'll fetch the transaction ID for internal bookkeeping
$transactionId = $response['TRANSACTIONID'];
}else echo "failed";
?>
我在过去的四个小时里摸不着头脑,按照各种指南和教程,但不知道什么是错的。我需要关注/下载任何其他步骤/文件吗?
P.S。我是第一次尝试paypal集成。
答案 0 :(得分:0)
您可以使用PayPal SDK来轻松完成 只需将其集成到您的代码中并使用此类
即可 /* Created By RAJA */
require realpath(dirname(__FILE__) . '/' . 'paymentconfig.php');
require realpath(dirname(__FILE__) . '/' . '/PayPalSDK/vendor/autoload.php');
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\CreditCard;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Transaction;
use PayPal\Exception\PPConnectionException;
class PaymentProcessor {
public static function proceedCreditCardTransaction($cardNumber, $cardType, $cardExpMonth, $cardExpYear, $firstName, $lastName, $amountToPay, $description, $currency = CURRENCY) {
if (!isset($cardNumber) || strlen($cardNumber) <= 10) {
throw new Exception("Invalid CardNumber : " . $cardNumber);
}if (!isset($cardType) || strlen($cardType) <= 0) {
throw new Exception("Invalid CardType : " . $cardType);
}if (!isset($cardExpMonth) || intval($cardExpMonth) <= 0 || intval($cardExpMonth) > 12) {
throw new Exception("Invalid Card Expire Month : " . $cardExpMonth);
}if (!isset($cardExpYear) || strlen($cardExpYear) <= 2) {
throw new Exception("Invalid Card Expire Year : " . $cardExpYear);
}if (!isset($firstName) || strlen($firstName) <= 0) {
throw new Exception("Invalid First Name : " . $firstName);
}if (!isset($lastName)) {
throw new Exception("Invalid Last Name : " . $lastName);
}if (!isset($amountToPay) || strlen($amountToPay) <= 0 || intval($amountToPay) <= 0) {
throw new Exception("Invalid Amount : " . $amountToPay);
}if (!isset($currency) || strlen($currency) <= 0) {
throw new Exception("Invalid Currency : " . $currency);
}if (!isset($description) || strlen($description) <= 0) {
throw new Exception("Invalid Description : " . $description);
}
$sdkConfig = array(
"mode" => "sandbox"
);
$cred = new OAuthTokenCredential(CLIENT_ID, SECRET, $sdkConfig);
$apiContext = new ApiContext($cred, 'Request' . time());
$apiContext->setConfig($sdkConfig);
$card = new CreditCard();
$card->setType($cardType);
$card->setNumber($cardNumber);
$card->setExpire_month($cardExpMonth);
$card->setExpire_year($cardExpYear);
$card->setFirst_name($firstName);
$card->setLast_name($lastName);
$fundingInstrument = new FundingInstrument();
$fundingInstrument->setCredit_card($card);
$payer = new Payer();
$payer->setPayment_method("credit_card");
$payer->setFunding_instruments(array($fundingInstrument));
$amount = new Amount();
$amount->setCurrency($currency);
$amount->setTotal($amountToPay);
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription($description);
$payment = new Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
try {
$payment->create($apiContext);
return new PaymentParser($payment->toArray());
} catch (PPConnectionException $ex) {
throw new Exception($ex->getData());
}
//echo json_encode($payment->toArray());
}
public static function proceedPaypalTransaction($amountToPay, $itemName, $description, $currency = CURRENCY) {
$sdkConfig = array(
"mode" => "sandbox"
);
$cred = new OAuthTokenCredential(CLIENT_ID, SECRET, $sdkConfig);
$apiContext = new ApiContext($cred, 'Request' . time());
$apiContext->setConfig($sdkConfig);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item1 = new Item();
$item1->setName($itemName)
->setCurrency($currency)
->setQuantity(1)
->setPrice($amountToPay);
$itemList = new ItemList();
$itemList->setItems(array($item1));
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($amountToPay);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($description);
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
exit(1);
}
}
}
PAYMENTCONFIG.PHP
define("CURRENCY","USD");
if (!file_exists(__DIR__ . '/PayPalSDK/vendor/autoload.php')) {
echo "The 'vendor' folder is missing. You must run 'composer update' to resolve application dependencies.\nPlease see the README for more information.\n";
exit(1);
}
define('PP_CONFIG_PATH', realpath(dirname(__FILE__) . '/' . '/PayPalSDK/sdk_config.ini'));
define('CLIENT_ID', 'CLIENT_ID');
define('SECRET', 'SECRET');
答案 1 :(得分:0)
首先,您在流程表单中输入错误的过期日期$creditCardDetails
,(将'EXPDATE'=&gt;'062020')
并在流程表格中执行以下更改
这
if( is_array($response) && $response['ACK'] == 'Success') { // Payment successful
// We'll fetch the transaction ID for internal bookkeeping
$transactionId = $response['TRANSACTIONID'];
}else echo "Failed";
到
if( is_array($response) && $response['ACK'] == 'Success') { // Payment successful
// We'll fetch the transaction ID for internal bookkeeping
$transactionId = $response['TRANSACTIONID'];
}else{
echo "<pre>";
print_r($response);
}
之后您将收到以下错误,
错误:10501配置无效。 由于商家配置无效,无法处理此交易。
要解决此错误,您已在PayPal PayPal Payments Pro批准后,在您的PayPal帐户中接受了“ PayPal Payments Pro ”协议。如果您已经接受了他们的协议但仍然收到此错误,PayPal也会偶尔承认该协议并激活虚拟终端,但不知何故错过了激活PayPal Payments Pro本身;如果您的PayPal“开始使用”摘要屏幕仅显示虚拟终端而没有关于PayPal Payments Pro的任何信息,请联系PayPal支持以激活您的PayPal Payments Pro服务。
如果您仅使用PayPal付款标准与常规PayPal个人,商家或高级帐户(即,如果您尚未升级到PayPal Payments Pro),请转至卖家管理员&gt;付款首选项并确保您已选中PayPal付款标准(而不是PayPal Payments Pro),然后点击提交以保存您所做的任何更改。
不接受 PayPal Payments Pro ,您无法使用Do Direct Method。