Our WeChat Pay iOS Component renders WeChat Pay in your payment form and triggers the app switch from your app to the WeChat app on the shopper's device.
Before you begin
This page explains how to add WeChat Pay to your existing iOS Components integration. The iOS Components integration works the same way for all payment methods. If you haven't done this integration yet, refer to our Components integration guide.
Before starting your WeChat Pay integration:
- Make sure that you have set up your back end implementation for making API requests.
- Add WeChat Pay in your test Customer Area.
- Create a developer account on the WeChat Open Platform.
- Register your mobile app with WeChat.
- In Xcode, go to File > Project Settings > App Target > Info, and:
a. Under URL Types, go to URL Schemes, and add your registered WeChatPay app ID with identifier weixin .
b. Go to LSApplicationQueriesSchemes, and add weixin. -
Add WeChat Pay to CocoaPods or Carthage.
CocoaPods
Add
pod 'Adyen/WeChatPay'
to yourPodfile
.Carthage
Add
AdyenWeChatPay.framework
to:app "build settings"/"Build Phases"/"Embed Frameworks"
.app "build settings"/"Build Phases"/"Link Binary to Libraries"
.app "build settings"/"Build Phases"/"Dependencies"
.
Show WeChat Pay in your payment form
Include WeChat Pay in the list of available payment methods. You don't need to collect any information from the shopper in your payment form.
-
Specify in your /paymentMethods request:
- countryCode: CN
- amount.currency: Any supported currency. For example, CNY. See Processing currencies for ecommerce for more information.
- channel: Specify iOS.
The response contains
paymentMethod.type
: wechatpaySDK. Serialize it and put it into an object. -
Decode the
/paymentMethods
response with thePaymentMethods
structure. Find the payment method object for the WeChat Pay Component.let paymentMethods = try JSONDecoder().decode(PaymentMethods.self, from: paymentMethodsResponse) // Check if paymentMethod type is not null and if the device is supported. if let paymentMethod = paymentMethods.paymentMethod(ofType: WeChatPayPaymentMethod.self), WeChatPaySDKActionComponent.isDeviceSupported(){ // If the two conditions are true, show payment UI. }
-
Create an instance of
APIContext
with the following parameters:Parameter name Required Description clientKey
Your client key. environment
Use test. When you're ready to accept live payments, change the value to one of our live environments. // When you're ready to go live, change environment to Environment.live // You can also use other environment values described in https://adyen.github.io/adyen-ios/Docs/Structs/Environment.html let apiContext = APIContext(environment: Environment.test, clientKey: clientKey)
- Initialize the WeChat Pay payment UI.
Make a payment
- Pass the
paymentComponentState.data.paymentMethod
to your server. -
From your server, make a /payments request, specifying:
paymentMethod.type
: ThepaymentComponentState.data.paymentMethod
from your client app.
curl https://checkout-test.adyen.com/v68/payments \ -H "x-API-key: YOUR_X-API-KEY" \ -H "content-type: application/json" \ -d '{ "merchantAccount":"YOUR_MERCHANT_ACCOUNT", "reference":"YOUR_ORDER_NUMBER", "amount":{ "currency":"CNY", "value":1000 }, "{hint:state.data.paymentMethod from onSubmit}paymentMethod{/hint}":{ "type":"wechatpaySDK" } }'
# Set your X-API-KEY with the API key from the Customer Area. adyen = Adyen::Client.new adyen.api_key = "YOUR_X-API-KEY" response = adyen.checkout.payments({ :amount => { :currency => "CNY", :value => 1000 }, :reference => "YOUR_ORDER_NUMBER", :paymentMethod => { :type => "wechatpaySDK" }, :merchantAccount => "YOUR_MERCHANT_ACCOUNT" })
// Set YOUR_X-API-KEY with the API key from the Customer Area. // Change to Environment.LIVE and add the Live URL prefix when you're ready to accept live payments. Client client = new Client("YOUR_X-API-KEY", Environment.TEST); Checkout checkout = new Checkout(client); PaymentsRequest paymentsRequest = new PaymentsRequest(); String merchantAccount = "YOUR_MERCHANT_ACCOUNT"; paymentsRequest.setMerchantAccount(merchantAccount); Amount amount = new Amount(); amount.setCurrency("CNY"); amount.setValue(15000L); paymentsRequest.setAmount(amount); DefaultPaymentMethodDetails paymentMethodDetails = new DefaultPaymentMethodDetails(); paymentMethodDetails.setType("wechatpaySDK"); paymentsRequest.setPaymentMethod(paymentMethodDetails); paymentsRequest.setReference("YOUR_ORDER_NUMBER"); PaymentsResponse paymentsResponse = checkout.payments(paymentsRequest);
// Set your X-API-KEY with the API key from the Customer Area. $client = new \Adyen\Client(); $client->setXApiKey("YOUR_X-API-KEY"); $service = new \Adyen\Service\Checkout($client); $params = array( "amount" => array( "currency" => "CNY", "value" => 1000 ), "reference" => "YOUR_ORDER_NUMBER", "paymentMethod" => array( "type" => "wechatpaySDK" ), "merchantAccount" => "YOUR_MERCHANT_ACCOUNT" ); $result = $service->payments($params);
#Set your X-API-KEY with the API key from the Customer Area. adyen = Adyen.Adyen() adyen.client.xapikey = 'YOUR_X-API-KEY' result = adyen.checkout.payments({ 'amount': { 'value': 1000, 'currency': 'CNY' }, 'reference': 'YOUR_ORDER_NUMBER', 'paymentMethod': { 'type': 'wechatpaySDK' }, 'merchantAccount': 'YOUR_MERCHANT_ACCOUNT' })
// Set your X-API-KEY with the API key from the Customer Area. var client = new Client ("YOUR_X-API-KEY", Environment.Test); var checkout = new Checkout(client); var amount = new Model.Checkout.Amount("CNY", 1000); var details = new Model.Checkout.DefaultPaymentMethodDetails{ Type = "wechatpaySDK" }; var paymentRequest = new Adyen.Model.Checkout.PaymentRequest { Reference = "YOUR_ORDER_NUMBER", Amount = amount, MerchantAccount = "YOUR_MERCHANT_ACCOUNT", PaymentMethod = details }; var paymentResponse = checkout.Payments(paymentsRequest);
// Set your X-API-KEY with the API key from the Customer Area. const {Client, Config, CheckoutAPI} = require('@adyen/api-library'); const config = new Config(); // Set your X-API-KEY with the API key from the Customer Area. config.apiKey = '[API_KEY]'; config.merchantAccount = '[YOUR_MERCHANT_ACCOUNT]'; const client = new Client({ config }); client.setEnvironment("TEST"); const checkout = new CheckoutAPI(client); checkout.payments({ amount: { currency: "CNY", value: 1000 }, paymentMethod: { type: 'wechatpaySDK' }, reference: "YOUR_ORDER_NUMBER", merchantAccount: config.merchantAccount }).then(res => res);
The /payments response includes an
action
object that contains information about the app switch. -
Store the
action.paymentData
on your server, and pass theaction
object to your client app.{ "resultCode": "Pending", "action": { "{hint:Store this on your server}paymentData{/hint}": "Ab02b4c0!BQABAgAgx==...", "paymentMethodType": "wechatpaySDK", "type": "wechatpaySDK", "sdkData": { "appid": "wx3aed7fe146f6a57a", "noncestr": "cPY0e83ny4hWyf5O", "packageValue": "Sign=WXPay", "partnerid": "205287714", "prepayid": "wx015678064827111da2e4f0b11005864100", "sign": "169FD3F1E193446D90C45573EBDD4020", "timestamp": "1573033086" } }, "details": [ { "key": "resultCode", "type": "text" } ], ... }
Handle the app switch
To switch the shopper to the WeChat app, initialize the weChatPayActionComponent
, and pass the action
object.
if case let .sdk(sdkAction) = action, case let .weChatPay(weChatPaySDKAction) = sdkAction {
weChatPayActionComponent.handle(weChatPaySDKAction)
}
After the shopper has completed the payment, the WeChat app redirects them back to your app.
Check the payment result
After the shopper returns to your app, make a POST /payments/details request from your server and provide the data
from the didProvide
method from your client app.
curl https://checkout-test.adyen.com/v68/payments/details \
-H "x-API-key: YOUR_X-API-KEY" \
-H "content-type: application/json" \
-d '{
"{hint:actionComponentData from app}details{/hint}": {
"resultCode": "0"
},
"{hint:Value that you stored on your server}paymentData{/hint}":"Ab02b4c0!BQABAgAKspbjN8+5..."
}'
# Set your X-API-KEY with the API key from the Customer Area.
adyen = Adyen::Client.new
adyen.env = :test
adyen.api_key = "YOUR_X-API-KEY"
request = DROPIN_ACTION_COMPONENT_DATA
# Data object passed from actionComponentData from the client app, parsed from JSON to a Hash.
response = adyen.checkout.payments.details(request)
# Check if further action is needed.
if response.body.has_key(:action)
# Pass the action object to your frontend
puts response.body[:action]
else
# No further action needed, pass the resultCode to your frontend
puts response.body[:resultCode]
end
// Set your X-API-KEY with the API key from the Customer Area.
Client client = new Client(xApiKey,Environment.TEST);
Checkout checkout = new Checkout(client);
PaymentsDetailsRequest paymentsDetailsRequest = new PaymentsDetailsRequest();
HashMap<String, String> details = new HashMap<>();
details.put("resultCode", "0");
paymentsDetailsRequest.setDetails(details);
paymentsDetailsRequest.setPaymentData("Ab02b4c0!BQABAgAKspbjN8+5...");
PaymentsResponse paymentsResponse = checkout.paymentsDetails(paymentsDetailsRequest);
// Set your X-API-KEY with the API key from the Customer Area.
$client = new \Adyen\Client();
$client->setXApiKey("YOUR_X-API-KEY");
$service = new \Adyen\Service\Checkout($client);
$params = DROPIN_ACTION_COMPONENT_DATA;
// Data object passed from actionComponentData from the client app, parsed from JSON to an array
$result = $service->paymentsDetails($params);
// Check if further action is needed
if (array_key_exists("action", $result)){
// Pass the action object to your frontend.
// $result["action"]
}
else {
// No further action needed, pass the resultCode to your front end
// $result['resultCode']
}
#Set your X-API-KEY with the API key from the Customer Area.
adyen = Adyen.Adyen()
adyen.client.xapikey = 'YOUR_X-API-KEY'
request = DROPIN_ACTION_COMPONENT_DATA
# Data object passed from actionComponentData from the client app, parsed from JSON to a dictionary
result = adyen.checkout.payments_details(request)
# Check if further action is needed.
if 'action' in result.message:
# Pass the action object to your front end
# result.message['action']
else:
# No further action needed, pass the resultCode to your front end
# result.message['resultCode']
// Set your X-API-KEY with the API key from the Customer Area.
var client = new Client ("YOUR_X-API-KEY", Environment.Test);
var checkout = new Checkout(client);
string paymentData = "Ab02b4c0!BQABAgAKspbjN8+5...";
var details = new Dictionary<string, string>
{
{ "resultCode", "0" }
};
var paymentsDetailsRequest = new Model.Checkout.PaymentsDetailsRequest(Details: details);
var paymentResponse = checkout.PaymentDetails(paymentsDetailsRequest);
// Set your X-API-KEY with the API key from the Customer Area.
const {Client, Config, CheckoutAPI} = require('@adyen/api-library');
const config = new Config();
// Set your X-API-KEY with the API key from the Customer Area.
config.apiKey = '[API_KEY]';
config.merchantAccount = '[YOUR_MERCHANT_ACCOUNT]';
const client = new Client({ config });
client.setEnvironment("TEST");
const checkout = new CheckoutAPI(client);
checkout.paymentsDetails({
details: {DROPIN_ACTION_COMPONENT_DATA} // Data object passed from the client app.
}).then(res => res);
Present the payment result
Use the resultCode
that you received in the /payments/details response to present the payment result to your shopper.
The resultCode
values you can receive for WeChat Pay are:
resultCode | Description | Action to take |
---|---|---|
Authorised | The payment was successful. | Inform the shopper that the payment has been successful. You will receive the funds in 2-3 days. |
Cancelled | The shopper cancelled the payment while on the WeChat app. | Ask the shopper whether they want to continue with the order, or ask them to select a different payment method. |
Error | There was an error when the payment was being processed. | Inform the shopper that there was an error processing their payment. The response contains a refusalReason , indicating the cause of the error. |
Pending or Received |
The payment order was successfully received. | Inform the shopper that you've received their order, and are waiting for the payment to clear. You will receive the final result of the payment in an AUTHORISATION notification. If successful, you will receive the funds in 2 days. |
Refused | The payment was refused by the shopper's bank. | Ask the shopper to try the payment again using a different payment method. |
Test and go live
WeChat Pay currently has no test platform. If you have a personal WeChat account you can test your integration by either:
- Cancelling the transaction when you are asked to verify the payment (recommended).
- Making live WeChat Pay payments with a low value.
You can check the status of a WeChat Pay payment in your Customer Area > Transactions > Payments.
Before you can accept live WeChat Pay payments, you need to submit a request for WeChat Pay in your live Customer Area.