“如何在PHP中集成支付网关?”

时间:2019-06-02 15:54:17

标签: php payment-gateway payment-integration

我想知道,如何以正确的方式集成付款网关(如PayPal,PAYU Money或其他产品)。因此,请提出建议。

1 个答案:

答案 0 :(得分:1)

Paypal还可以,但不是我的最爱。对于初学者,甚至对于高级程序员,我建议使用Stripe。他们的文档非常容易理解,可以与任何语言远程集成。他们的客户支持也非常友好,他们将为您遇到的任何编码问题提供远程帮助。多年来,我在所有项目(无论大小)中都使用他们的PHP库。

条纹元素文档:https://stripe.com/docs/stripe-js

使用元素示例进行自定义结帐

    <!--INSERT  STRIPE JS -->
    <script src="https://js.stripe.com/v3/"></script>


     // Create a Stripe client.
    // Note: this merchant has been set up for demo purposes.
    var stripe = Stripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh');

    // Create an instance of Elements.
    var elements = stripe.elements();

    // Handle form submission.
    var form = document.getElementById('payment-form');
    form.addEventListener('submit', function(event) {
    event.preventDefault();

    stripe.createToken(card).then(function(result) {
    if (result.error) {
    // Inform the user if there was an error.
    var errorElement = document.getElementById('card-errors');
    errorElement.textContent = result.error.message;
    } else {
    // Send the token to your server.
    stripeTokenHandler(result.token);
            }
          });
        });

        // Submit the form with the token ID.
        function stripeTokenHandler(token) {
          // Insert the token ID into the form so it gets submitted to the server
          var form = document.getElementById('payment-form');
          var hiddenInput = document.createElement('input');
          hiddenInput.setAttribute('type', 'hidden');
          hiddenInput.setAttribute('name', 'stripeToken');
          hiddenInput.setAttribute('value', token.id);
          form.appendChild(hiddenInput);

          // Submit the form
          form.submit();
        }

charge.php

//send the form here
/*
 * 
 * STRIPE
 * AUTOLOAD
 * 
 */
require_once '../vendor/autoload.php';
// Be sure to replace this with your actual test API key
// (switch to the live key later)
Stripe\Stripe::setApiKey($stripe_sk);

    $stripeToken = $_POST['stripeToken'];
    $stripePlanAmount = $_POST['stripePlanAmount'];
    $customerEmail = $_POST['customerEmail'];

    //convert amount to cents for Stripe to understand $1 = 100cents
    $convertedAmount = $stripePlanAmount * 100;

//create a stripe product 
    \Stripe\Product::create(array(
        "name" => 'My plan',
        "type" => "service"
    ));

//create a stripe plan
    \Stripe\Plan::create(array(
        "amount" => $convertedAmount,
        "product" => array(
            "name" => "My Plan"
        ),
        "currency" => $myCurrency,
        "id" => $planid
    ));

  // Create a Customer
        $customer = \Stripe\Customer::create(array(
                    "email" => $customerEmail,
                    "source" => $stripeToken
        ));

全条纹文档:https://stripe.com/docs