Thinkphp integration adds paypal sdk for payment test

To add a payment extension, the first step is to find the documentation. It is best if there is a related SDK.

Paypal document and sdk address:

https://github.com/paypal/PayPal-PHP-SDK

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

Sample code: https://github.com/paypal/PayPal-PHP-SDK/tree/master/sample

Payment process: https://developer.paypal.com/docs/api/quickstart/payments/

In the second step, we need a developer account.

https://www.paypal.com/ Register an account, then go to https://developer.paypal.com to log in

Next we need a sandbox account, one is the buyer and the other is the seller. (Note, do not create two accounts in China, because Paypal stipulates that accounts between China cannot be paid, which is said to be a matter of foreign exchange supervision)

With an account, we now create an application to generate Client ID and Secret

 

------------------------

Then we download a thinkphp framework, use composer to install the PHP SDK, and run it in the root directory of the framework

composer require paypal/rest-api-sdk-php:*

After the installation is successful, how do we use it next.

Read the document https://github.com/paypal/PayPal-PHP-SDK/wiki

He basically has two things to do,

1. Generate a redirected payment link.

2. Make a callback address to receive data.

Payment process:

---------------------------------------------------

We build a pay application in thinkphp

Add controller Index.php

<?php
namespace app\pay\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        $apiContext = new \PayPal\Rest\ApiContext(
		        new \PayPal\Auth\OAuthTokenCredential(
		            'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS',     // ClientID
		            'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL'      // ClientSecret
		        )
		);
		/*sandbox 模式*/
		$apiContext->setConfig(array('mode' => 'sandbox'));


		$payer = new \PayPal\Api\Payer();
		$payer->setPaymentMethod('paypal');

		$amount = new \PayPal\Api\Amount();
		$amount->setTotal('1.00');
		$amount->setCurrency('USD');

		$transaction = new \PayPal\Api\Transaction();
		$transaction->setAmount($amount);

		$redirectUrls = new \PayPal\Api\RedirectUrls();
		$sn=time();
		$redirectUrls->setReturnUrl("http://t1.com/pay/Index/returnUrl/sn/".$sn)
		    ->setCancelUrl("http://t1.com/pay/Index/cancelUrl/sn/".$sn);

		$payment = new \PayPal\Api\Payment();
		$payment->setIntent('sale')
		    ->setPayer($payer)
		    ->setTransactions(array($transaction))
		    ->setRedirectUrls($redirectUrls);

		try {
		    $payment->create($apiContext);
		    echo $url=$payment->getApprovalLink();
		    echo "<br><a href='".$url."'>点击支付</a>\n";
		}
		catch (\PayPal\Exception\PayPalConnectionException $ex) {
		    echo $ex->getData();
		}
    }

    public function returnUrl(){
    	$apiContext = new \PayPal\Rest\ApiContext(
		        new \PayPal\Auth\OAuthTokenCredential(
		            'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS',     // ClientID
		            'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL'      // ClientSecret
		        )
		);
		/*sandbox 模式*/
		$apiContext->setConfig(array('mode' => 'sandbox'));


        // Get payment object by passing paymentId
		$paymentId = $this->request->param('paymentId');
		$payment = new \PayPal\Api\Payment();
		$payment = $payment->get($paymentId, $apiContext);
		$payerId = $this->request->param('PayerID');

		// Execute payment with payer ID
		$execution = new \PayPal\Api\PaymentExecution();
		$execution->setPayerId($payerId);

		try {
			// Execute payment
			$result = $payment->execute($execution, $apiContext);
			if ($result && isset ( $result->state ) && $result->state == 'approved') {
				echo "支付成功";
			}else{
				echo "支付失败";
				var_dump($result);
			}
		} catch (PayPal\Exception\PayPalConnectionException $ex) {
		  echo $ex->getCode();
		  echo $ex->getData();
		  die($ex);
		} catch (Exception $ex) {
		  die($ex);
		}
    }
    public function cancelUrl(){
    	echo '取消支付';
    }
}

Run http://t1.com/pay to get a redirect link for payment, click

Sign in to pay

 

 

After the payment is successful, jump to the url we originally set.

 

Here we walked through the process of paypal in thinkphp.

 

Guess you like

Origin blog.csdn.net/weixin_43932088/article/details/87879755