Share 2Checkout Payment Gateway Integration in PHP

## Tích hợp cổng thanh toán 2Checkout trong PHP

2Checkout là một cổng thanh toán phổ biến cho phép các doanh nghiệp chấp nhận thanh toán trực tuyến.Đây là một giải pháp an toàn và đáng tin cậy cung cấp nhiều tính năng khác nhau, bao gồm nhiều phương thức thanh toán, bảo vệ gian lận và báo cáo chi tiết.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tích hợp 2Checkout với trang web PHP.Chúng tôi sẽ đề cập đến các chủ đề sau:

* Tạo tài khoản 2Checkout
* Tạo khóa API 2Checkout
* Tích hợp 2Checkout với PHP
* Xử lý thanh toán

### Tạo tài khoản 2checkout

Để bắt đầu, bạn sẽ cần tạo một tài khoản 2Checkout.Bạn có thể làm điều này bằng cách truy cập [trang web 2Checkout] (https://www.2checkout.com/) và nhấp vào nút "Đăng ký".

Khi bạn đã tạo tài khoản của mình, bạn sẽ cần tạo khóa API 2Checkout.Bạn có thể làm điều này bằng cách nhấp vào tab "Cài đặt" và sau đó nhấp vào liên kết "Phím API".

### Tạo khóa API 2Checkout

Để tích hợp 2Checkout với PHP, bạn sẽ cần tạo khóa API 2Checkout.Bạn có thể làm điều này bằng cách nhấp vào tab "Cài đặt" và sau đó nhấp vào liên kết "Phím API".

Trong phần "Phím API", nhấp vào nút "Tạo khóa API".

Điều này sẽ tạo ra một khóa API mới.Sao chép khóa này và lưu nó cho sau.

### Tích hợp 2checkout với PHP

Để tích hợp 2checkout với PHP, bạn sẽ cần thêm mã sau vào trang web của mình:

`` `PHP
<? PHP

// Bao gồm thư viện PHP 2Checkout
Yêu cầu_once ('2Checkout.php');

// Đặt khóa API 2Checkout của bạn
$ api_key = 'your_api_key';

// Đặt ID thương gia 2Checkout của bạn
$ merchant_id = 'your_merchant_id';

// Đặt mã tiền tệ
$ currence_code = 'USD';

// Đặt giá sản phẩm
$ sản phẩm_price = 10,00;

// Đặt mô tả sản phẩm
$ sản phẩm_descrip = 'một sản phẩm mẫu';

// Tạo đơn đặt hàng 2Checkout mới
$ order = new Checkoutorder ($ api_key, $ merchant_id, $ currency_code, $ product_price, $ sản phẩm_descrip);

// Đặt địa chỉ email của khách hàng
$ order-> setEmail ('customer@example.com ');

// Đặt địa chỉ thanh toán của khách hàng
$ order-> setBillingAddress ('123 Main Street', 'Anytown', 'CA', '91234', 'USA');

// Đặt địa chỉ vận chuyển của khách hàng
$ order-> setShippingAddress ('123 Main Street', 'Anytown', 'CA', '91234', 'USA');

// Đặt số điện thoại của khách hàng
$ order-> setphone ('123-456-7890');

// Đặt phương thức thanh toán của khách hàng
$ order-> setPaymentMethod ('credit_card');

// Đặt số thẻ tín dụng của khách hàng
$ order-> setCardNumber ('411111111111111');

// Đặt ngày hết hạn thẻ tín dụng của khách hàng
$ order-> setCardExPirationDate ('12/2023 ');

// Đặt mã bảo mật thẻ tín dụng của khách hàng
$ order-> setCardsecurityCode ('123');

// Gửi đơn đặt hàng đến 2Checkout
$ trả lời = $ order-> gửi ();

// Kiểm tra mã phản hồi
if ($ answers-> getResponsecode () == 200) {

// Lệnh đã thành công

// Nhận ID đặt hàng
$ order_id = $ answer-> getOrderId ();

// Nhận thông tin thanh toán
$ payment_information = $ answer-> getPaymentInformation ();

} khác {

// thứ tự không thành công

// Nhận thông báo lỗi
$ error_message = $ answer-> geterrorMessage ();

}

?>
`` `

### Thanh toán xử lý

Khi khách hàng đã hoàn thành thanh toán, 2Checkout sẽ gửi thông báo đến trang web của bạn.Bạn có thể sử dụng thông báo này để xử lý thanh toán và cập nhật trạng thái đơn đặt hàng của bạn.

Để xử lý các khoản thanh toán, bạn sẽ cần thêm mã sau vào trang web của mình:

`` `PHP
<? PHP

// Bao gồm thư viện PHP 2Checkout
Yêu cầu_once ('2Checkout.php');

// Đặt khóa API 2Checkout của bạn
$ api_key = 'your_
=======================================
## 2Checkout Payment Gateway Integration in PHP

2Checkout is a popular payment gateway that allows businesses to accept payments online. It is a secure and reliable solution that offers a variety of features, including multiple payment methods, fraud protection, and detailed reporting.

In this tutorial, we will show you how to integrate 2Checkout with a PHP website. We will cover the following topics:

* Creating a 2Checkout account
* Generating a 2Checkout API key
* Integrating 2Checkout with PHP
* Handling payments

### Creating a 2Checkout account

To get started, you will need to create a 2Checkout account. You can do this by visiting the [2Checkout website](https://www.2checkout.com/) and clicking on the "Sign Up" button.

Once you have created your account, you will need to generate a 2Checkout API key. You can do this by clicking on the "Settings" tab and then clicking on the "API Keys" link.

### Generating a 2Checkout API key

To integrate 2Checkout with PHP, you will need to generate a 2Checkout API key. You can do this by clicking on the "Settings" tab and then clicking on the "API Keys" link.

In the "API Keys" section, click on the "Generate API Key" button.

This will generate a new API key. Copy this key and save it for later.

### Integrating 2Checkout with PHP

To integrate 2Checkout with PHP, you will need to add the following code to your website:

```php
<?php

// Include the 2Checkout PHP library
require_once('2checkout.php');

// Set your 2Checkout API key
$api_key = 'YOUR_API_KEY';

// Set your 2Checkout merchant ID
$merchant_id = 'YOUR_MERCHANT_ID';

// Set the currency code
$currency_code = 'USD';

// Set the product price
$product_price = 10.00;

// Set the product description
$product_description = 'A sample product';

// Create a new 2Checkout order
$order = new CheckoutOrder($api_key, $merchant_id, $currency_code, $product_price, $product_description);

// Set the customer's email address
$order->setEmail('customer@example.com');

// Set the customer's billing address
$order->setBillingAddress('123 Main Street', 'Anytown', 'CA', '91234', 'USA');

// Set the customer's shipping address
$order->setShippingAddress('123 Main Street', 'Anytown', 'CA', '91234', 'USA');

// Set the customer's phone number
$order->setPhone('123-456-7890');

// Set the customer's payment method
$order->setPaymentMethod('credit_card');

// Set the customer's credit card number
$order->setCardNumber('4111111111111111');

// Set the customer's credit card expiration date
$order->setCardExpirationDate('12/2023');

// Set the customer's credit card security code
$order->setCardSecurityCode('123');

// Submit the order to 2Checkout
$response = $order->submit();

// Check the response code
if ($response->getResponseCode() == 200) {

// The order was successful

// Get the order ID
$order_id = $response->getOrderId();

// Get the payment information
$payment_information = $response->getPaymentInformation();

} else {

// The order failed

// Get the error message
$error_message = $response->getErrorMessage();

}

?>
```

### Handling payments

Once a customer has completed a payment, 2Checkout will send a notification to your website. You can use this notification to process the payment and update your order status.

To handle payments, you will need to add the following code to your website:

```php
<?php

// Include the 2Checkout PHP library
require_once('2checkout.php');

// Set your 2Checkout API key
$api_key = 'YOUR_
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock