2.1.1. 使用条件
• WechatPay merchant account
• Java 1.8 or later
• Maven or Gradle;
• 0.4.6 or later version of the Java httpClient SDK
2.1.2. 安装
Gradle:在你的 build.gradle 文件中加入如下的依赖
implementation 'com.github.wechatpay-apiv3:wechatpay-apache-httpclient:0.4.7'
Maven:在 pom.xml 文件中加入以下依赖
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.4.7</version>
</dependency>
2.1.3. 初始化
用WechatPayHttpClientBuilder初始化创建一个httpClient,httpClient将被用于发送请求调用微信支付接口。 用CertificatesManager初始化一个verifier实例,用于验证签名。
private CloseableHttpClient httpClient;
private CertificatesManager certificatesManager;
private Verifier verifier;
@Before
//initialize verifier and build httpClient
public void setup() throws GeneralSecurityException, IOException, HttpCodeException, NotFoundException {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
certificatesManager = CertificatesManager.getInstance();
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8));
verifier = certificatesManager.getVerifier(merchantId);
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
httpClient = builder.build();
}
2.1.4. 通用功能
@Test
public void encryptedTest() throws IllegalBlockSizeException {
String text = "helloWorld";
String transformation = "RSA/ECB/PKCS1Padding";
String encryptedText = RsaCryptoUtil.encrypt(text, verifier.getValidCertificate(), transformation);
System.out.println(encryptedText);
}
@Test
public void decryptedTest() throws BadPaddingException {
String encryptedText = OBTgun4jiN13n3nmSg8v0MNDMy3mbs380yq4GrgO8vwCqXnvrWxwo3jUCNY2UY7MIOtv/SD8ke64MxcSB0hn5EzKv1LOLprI3NvvmNLS4C3SBulxpZG62RYp1+8FgwAI4M//icXvjtZWVH4KVDg==";
String transformation = "RSA/ECB/PKCS1Padding";
String decryptedText = RsaCryptoUtil.decrypt(encryptedText, PemUtil.loadPrivateKey(privateKey), transformation);
assert("helloWorld".equals(decryptedText));
}
2.2.1. 使用条件
• WechatPay merchant account
• GO SDK 0.2.13 or later version
2.2.2. 安装
1. 使用 Go Modules 管理你的项目
如果你的项目还不是使用 Go Modules 做依赖管理,在项目根目录下执行:
go mod init
2. 无需 clone 仓库中的代码,直接在项目目录中执行:
go get -u github.com/wechatpay-apiv3/wechatpay-go
来添加依赖,完成 go.mod 修改与 SDK 下载
2.2.3. 初始化
初始化Client并向Client中加载商户号,私钥,API V3 key,商户证书和序列号, 用于之后发送接口请求。
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"fmt"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"log"
"net/http"
)
var (
mchID = "" // merchant id
mchCertificateSerialNumber = "" // merchant certificate serial number
mchAPIv3Key = "" // merchant api v3 key
header = make(http.Header)
ctx = context.Background()
cert *x509.Certificate
mchPrivateKey *rsa.PrivateKey
client *core.Client
err error
)
func setup() {
//Load platform certificate
cert, err = utils.LoadCertificateWithPath("Path/To/Platform/Cert")
if err != nil {
log.Fatal(err)
}
// Load private key
mchPrivateKey, err = utils.LoadPrivateKeyWithPath("Path/To/Private/Key")
if err != nil {
log.Fatal("load merchant private key error")
}
// Initialize client which is capable to update platform certificate periodically
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err = core.NewClient(ctx, opts...)
if err != nil {
log.Fatalf("new wechat pay client err:%s", err)
}
}
2.2.4. 通用功能
func Encryption(t *testing.T) {
var testRSACryptoUtilMchCertificateStr = `-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----`
testRSACryptoUtilCertificate, _ = LoadCertificate(testRSACryptoUtilMchCertificateStr)
const message = "hello world"
// 使用OAEP padding方式对证书加密
ciphertext, _ := EncryptOAEPWithCertificate(message, testRSACryptoUtilCertificate)
// 使用PKCS1 padding对证书加密
ciphertext, _ := EncryptPKCS1v15WithCertificate(message, testRSACryptoUtilCertificate)
}
func TestDecryption(t *testing.T) {
var testRSACryptoUtilPrivateKeyStr = `-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----`
testRSACryptoUtilPrivateKey, _ = LoadPrivateKey(testingKey(testRSACryptoUtilPrivateKeyStr))
const ciphertext = ""
// 使用PKCS1 padding进行私钥解密
decryptMessage, _ := DecryptPKCS1v15(ciphertext, testRSACryptoUtilPrivateKey)
// 使用OAEP padding方式私钥解密
decryptMessage, _ := DecryptOAEP(ciphertext, testRSACryptoUtilPrivateKey)
}
2.3.1. 使用条件
• Guzzle 7.0,PHP >= 7.2.5
• Guzzle 6.5,PHP >= 7.1.2
• 项目已支持 PHP 8。
2.3.2. 安装
推荐使用 PHP 包管理工具 Composer 安装 SDK:
composer require wechatpay/wechatpay
2.3.3. 初始化
初始化Client并向Client中加载商户号,私钥,API V3 key,商户证书和序列号, 用于之后发送接口请求。
require_once('vendor/autoload.php');
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
$merchantId = '190000****';
$merchantPrivateKeyFilePath = 'file:///path/to/merchant/apiclient_key.pem';
$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
$merchantCertificateSerial = '3775B6A45ACD588826D15E583A95F5DD********';
$platformCertificateFilePath = 'file:///path/to/wechatpay/cert.pem';
$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
// init a API V3 instance
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformCertificateSerial => $platformPublicKeyInstance,
],
]);
2.3.4. 通用功能
$encryptor = static function(string $msg) use ($platformPublicKeyInstance): string {
return Rsa::encrypt($msg, $platformPublicKeyInstance, OPENSSL_PKCS1_PADDING);
};
$encryptedMsg = $encryptor('HelloWorld');
$decryptor = static function(string $msg) use ($merchantPrivateKeyInstance): string {
return Rsa::decrypt($msg, $merchantPrivateKeyInstance, OPENSSL_PKCS1_PADDING);
};
$decryptedMsg = $decryptor('ciphertext');