我已经将此Paypal Payment与Omnipay集成到了我的php应用程序中
在我的index.php
中,
<form action="process.php" method="POST">
<button type="submit">Pay with Paypal</button>
</form>
在我的process.php
中,我已经完成了
<?php
require_once 'config.php';
try {
$response = $gateway->purchase(array(
'amount' => 100,
'currency' => PAYPAL_CURRENCY,
'returnUrl' => PAYPAL_RETURN_URL,
'cancelUrl' => PAYPAL_CANCEL_URL,
))->send();
if ($response->isRedirect()) {
$response->redirect(); // this will automatically forward the customer
} else {
// not successful
dd($response->getMessage());
}
} catch(Exception $e) {
dd($e);
}
function dd($content)
{
echo "<pre>";
var_dump($content);
echo "</pre>";
}
在我的config.php
中,我已经完成了
<?php
require_once "vendor/autoload.php";
use Omnipay\Omnipay;
define('CLIENT_ID', 'my-client-id');
define('CLIENT_SECRET', 'my-secret-id');
define('PAYPAL_RETURN_URL', 'localhost/paypalwp/success.php');
define('PAYPAL_CANCEL_URL', 'localhost/paypalwp/cancel.php');
define('PAYPAL_CURRENCY', 'USD'); // set your currency here
$gateway = Omnipay::create('PayPal_Rest');
$gateway->setClientId(CLIENT_ID);
$gateway->setSecret(CLIENT_SECRET);
$gateway->setTestMode(true); //set it to 'false' when go live
在我的success.php
中,我已经完成了
<?php
require_once 'config.php';
// Once the transaction has been approved, we need to complete it.
if (array_key_exists('paymentId', $_GET) && array_key_exists('PayerID', $_GET)) {
$transaction = $gateway->completePurchase(array(
'payer_id' => $_GET['PayerID'],
'transactionReference' => $_GET['paymentId'],
));
$response = $transaction->send();
if ($response->isSuccessful()) {
// The customer has successfully paid.
$arr_body = $response->getData();
$payment_id = $arr_body['id'];
$payer_id = $arr_body['payer']['payer_info']['payer_id'];
$payer_email = $arr_body['payer']['payer_info']['email'];
$amount = $arr_body['transactions'][0]['amount']['total'];
$currency = PAYPAL_CURRENCY;
$payment_status = $arr_body['state'];
echo "Payment is successful. Your transaction id is: ". $payment_id;
} else {
echo $response->getMessage();
}
} else {
echo 'Transaction is declined';
}
并且在cancel.php
中,我有一个简单的html布局,表示用户已取消付款
但是当我尝试这段代码时,它给了我这个错误"Invalid request - see details"
知道为什么要这么做吗?